在调试Autofac依赖注入的作品,但在发行崩溃 [英] Autofac Dependency Injection works in Debug but crashes in Release

查看:8162
本文介绍了在调试Autofac依赖注入的作品,但在发行崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么会导致我的DI在发布模式崩溃时,在调试模式下的罚款?这是一个Xamarin.Forms的应用程序,并正在运行在Android

  20 07-23:56:53.002 I / MonoDroid的(15451):未处理的异常:Autofac.Core.DependencyResolutionException:执行解析操作出现异常。详情请参阅的InnerException。 --->对象引用不设置到对象的实例(详情请参阅内部异常。)---> System.NullReferenceException:对象引用未设置到对象
的一个实例07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Expressions.Expression.CreateLambda(System.Type的delegateType,系统.Linq.Expressions.Expression体,System.String名,布尔尾调用,System.Collections.ObjectModel.ReadOnlyCollection`1参数)[0x00000]上述<文件名未知> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,System.Linq.Expressions.Expression身体,System.String名,布尔尾调用,IEnumerable`1参数)[0x00000]上述< ;文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,System.Linq.Expressions.Expression体, IEnumerable`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Features.GeneratedFactories.FactoryGenerator.CreateGenerator(System.Func` 3 makeResolveCall,System.Type的delegateType,ParameterMapping下午)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Features.GeneratedFactories.FactoryGenerator。 .ctor(System.Type的delegateType,IComponentRegistration productRegistration,ParameterMapping parameterMapping)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Features.GeneratedFactories .GeneratedFactoryRegistrationSource +<> c__DisplayClass2< RegistrationsFor> 0x00000] b__0(IComponentRegistration R)上述<文件名不明>:0
07-23 20:56:53.002 I / MonoDroid的(15451):在系统.Linq.Enumerable + LT; CreateSelectIterator> c__Iterator10`2 [Autofac.Core.IComponentRegistration,Autofac.Core.IComponentRegistration] .MoveNext()[0x00000]上述<文件名不明> 0
07-23 20:56 :53.002 I / MonoDroid的(15451):在Autofac.Core.Registration.ComponentRegistry.GetInitializedServiceInfo(Autofac.Core.Service服务)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Registration.ComponentRegistry.TryGetRegistration(Autofac.Core.Service服务,IComponentRegistration&安培;注册)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Activators.Reflection.AutowiringParameter.CanSupplyValue(System.Reflection.ParameterInfo PI,IComponentContext背景下,System.Func`1&安培; valueProvider)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Activators。 Reflection.ConstructorParameterBinding..ctor(System.Reflection.ConstructorInfo CI,IEnumerable`1 availableParameters,IComponentContext上下文)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451 ):在Autofac.Core.Activators.Reflection.ReflectionActivator + LT;> c__DisplayClass5< GetConstructorBindings> b__4(System.Reflection.ConstructorInfo CI)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Enumerable + LT; CreateSelectIterator> c__Iterator10`2 [System.Reflection.ConstructorInfo,Autofac.Core.Activators.Reflection.ConstructorParameterBinding] .MoveNext()[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Enumerable + LT; CreateWhereIterator> c__Iterator1E`1 [Autofac.Core.Activators.Reflection。 ConstructorParameterBinding] .MoveNext()[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在System.Linq.Enumerable.ToArray [ConstructorParameterBinding](IEnumerable` 1源)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext背景下,IEnumerable的`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1参数) [0x00000]上述<文件名未知> 0
07-23 20:56:53.002的I / MonoDroid的(15451):上述<在Autofac.Core.Resolving.InstanceLookup.Execute()[0x00000];文件名未知> ; 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration注册,IEnumerable`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration注册,IEnumerable`1参数)[0x00000]上述<文件名未知> 0
07-23 20:56:53.002 I / MonoDroid的(15451):在Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration注册,IEnumerable`1参数)[0x00000]上述<文件名不明> ; 0
07-23 20:56:53.002 I / MonoDroid的(15451): -
未处理的异常:

Autofac.Core.DependencyResolutionException:执行出现异常一个解析操作。详情请参阅的InnerException。 --->对象引用不设置到对象的实例(详情请参见内部异常。)

07-23 20:56:54.003 E /单声道(15451):
07-23 20: 56:54.003 E /单声道(15451):未处理的异常:
07-23 20:56:54.003 E /单声道(15451):Autofac.Core.DependencyResolutionException:执行解析操作出现异常。详情请参阅的InnerException。 --->对象引用不设置到对象的实例(详情请参阅内部异常。)---> System.NullReferenceException:对象引用未设置到对象
在mgmain JNI_OnLoad
的一个实例07-23 20:56:54.003 E /单声道(15451):在System.Linq.Expressions.Expression.CreateLambda (System.Type的delegateType,System.Linq.Expressions.Expression身体,System.String名,布尔尾调用,System.Collections.ObjectModel.ReadOnlyCollection`1参数)[0x00000]上述<文件名不明> 0
07 -23 20:56:54.003 E /单声道(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,System.Linq.Expressions.Expression身体,System.String名,布尔尾调用,IEnumerable`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:54.003 E /单声道(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,系统。 Linq.Expressions.Expression身体,IEnumerable`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:54.003 E /单声道(15451):在Autofac.Features.Generate
07-23 20:56:54.003 E /单-RT(15451):[错误] FATAL未处理的异常:Autofac.Core.DependencyResolutionException:执行解析操作出现异常。详情请参阅的InnerException。 --->对象引用不设置到对象的实例(详情请参阅内部异常。)---> System.NullReferenceException:对象引用未设置到对象
的一个实例07-23 20:56:54.003 E /单-RT(15451):在System.Linq.Expressions.Expression.CreateLambda(System.Type的delegateType ,System.Linq.Expressions.Expression身体,System.String名,布尔尾调用,System.Collections.ObjectModel.ReadOnlyCollection`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56 :54.003 E /单-RT(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,System.Linq.Expressions.Expression身体,System.String名,布尔尾调用,IEnumerable`1参数) 0x00000]上述<文件名不明> 0
07-23 20:56:54.003 E /单-RT(15451):在System.Linq.Expressions.Expression.Lambda(System.Type的delegateType,System.Linq的.Expressions.Expression身体,IEnumerable`1参数)[0x00000]上述<文件名不明> 0
07-23 20:56:54.003 E /单-RT(15451):在Autofac.Feat
中的项目单声道已与代码0(为0x0)退出。



我构造的一个例子是这样的。



 公开的MainPage(Func键<作用,动作<布尔型>中的MainMenu> MAINMENU,
ModulePage modulePage,
loadingPage loadingPage,
Func键<作用, authPage> authPage,
IUserDialogService userDialogService,
IModuleService moduleService,
ISyncCommand syncCommand)
{
_loadingPage = loadingPage;
_authPage = authPage;
_userDialogService = userDialogService;
_moduleService = moduleService;
_syncCommand = syncCommand;

标题= Strings.app_title;
VAR主= MAINMENU(OnAfterDeauthenticated,OnToggleRequest());
变种细节=新NavigationPage(modulePage);

如果(App.Navigation == NULL)
{
App.Navigation = detail.Navigation;
}

=法师高手;
细节=细节;
}



ContainerBuilder获取Android应用程序初始化,然后传递给PCL。



的Android

 保护覆盖无效的OnCreate(束束)
{
base.OnCreate(包);

Forms.Init(这一点,捆绑);

VAR容器= RegisterDependencies();
VAR页= App.GetMainPage(容器);

SetPage(页);
}

私有静态的IContainer RegisterDependencies()
{
变种containerBuilder =新ContainerBuilder();
RegisterAndroidSpecific(containerBuilder);

变种coreIoc =新CoreIoC(containerBuilder);
返回coreIoc.Load();
}

私有静态无效RegisterAndroidSpecific(ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType< AudioPlayer>()SingleInstance()为< IAudioPlayer>();
containerBuilder.RegisterType< SettingsService>()为<&ISettingsService GT;();
containerBuilder.RegisterType<网络服务>()为<&INetworkService GT;();
containerBuilder.RegisterType< NativeVideoPlayer>()为<&IVideoPlayer GT;();
containerBuilder.RegisterType< MarkdownService>()为<&IMarkdownService GT;();
containerBuilder.RegisterType< UserDialogService>()为<&IUserDialogService GT;();

containerBuilder.RegisterType< DbProvider>()
.SingleInstance()
.WithParameters(新名单<参数> {
新TypedParameter(typeof运算(字符串), FSM-breathingroom),
新TypedParameter(typeof运算(SqliteSettings),新SqliteSettings {
CacheSize的= 16777216,
同步模式= SynchronizationModes.Off,
JournalMode = SQLiteJournalModeEnum.Off,
每页= 65536
})
})
。至于<&IDbProvider类型GT;();
}



PCL



 公共静态页面GetMainPage(集装箱的IContainer)
{
集装箱货柜=;

RegisterCurrentUser(Container.Resolve<&的usermodel GT;());
RunMigrations();
RegisterCurrentUser();

VAR rootPage = Container.Resolve<&的MainPage GT;();
返回rootPage;
}


解决方案

好了,解决的办法很简单。但不必要IMO



我打开所有的 Func键<> 注射用实际的工厂

 私人FooPageFactory _fooPage 
公共SomePage的(FooPageFactory fooPage)
{
_fooPage = fooPage;在需要的时候
//
_fooPage.Init(SomeActionDependency);
}


What would cause my DI to crash in Release mode when it's fine in Debug mode? This is a Xamarin.Forms app, and is being run on Android.

07-23 20:56:53.002 I/MonoDroid(15451): UNHANDLED EXCEPTION: Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Object reference not set to an instance of an object (See inner exception for details.) ---> System.NullReferenceException: Object reference not set to an instance of an object
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Expressions.Expression.CreateLambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, System.Collections.ObjectModel.ReadOnlyCollection`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Features.GeneratedFactories.FactoryGenerator.CreateGenerator (System.Func`3 makeResolveCall, System.Type delegateType, ParameterMapping pm) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Features.GeneratedFactories.FactoryGenerator..ctor (System.Type delegateType, IComponentRegistration productRegistration, ParameterMapping parameterMapping) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Features.GeneratedFactories.GeneratedFactoryRegistrationSource+<>c__DisplayClass2.<RegistrationsFor>b__0 (IComponentRegistration r) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[Autofac.Core.IComponentRegistration,Autofac.Core.IComponentRegistration].MoveNext () [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Registration.ComponentRegistry.GetInitializedServiceInfo (Autofac.Core.Service service) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Registration.ComponentRegistry.TryGetRegistration (Autofac.Core.Service service, IComponentRegistration& registration) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Activators.Reflection.AutowiringParameter.CanSupplyValue (System.Reflection.ParameterInfo pi, IComponentContext context, System.Func`1& valueProvider) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding..ctor (System.Reflection.ConstructorInfo ci, IEnumerable`1 availableParameters, IComponentContext context) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Activators.Reflection.ReflectionActivator+<>c__DisplayClass5.<GetConstructorBindings>b__4 (System.Reflection.ConstructorInfo ci) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator10`2[System.Reflection.ConstructorInfo,Autofac.Core.Activators.Reflection.ConstructorParameterBinding].MoveNext () [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator1E`1[Autofac.Core.Activators.Reflection.ConstructorParameterBinding].MoveNext () [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at System.Linq.Enumerable.ToArray[ConstructorParameterBinding] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance (IComponentContext context, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Resolving.InstanceLookup.Activate (IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Resolving.InstanceLookup.Execute () [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance (ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Resolving.ResolveOperation.ResolveComponent (IComponentRegistration registration, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   at Autofac.Core.Resolving.ResolveOperation.Execute (IComponentRegistration registration, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:53.002 I/MonoDroid(15451):   -
Unhandled Exception:

Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Object reference not set to an instance of an object (See inner exception for details.)

07-23 20:56:54.003 E/mono    (15451): 
07-23 20:56:54.003 E/mono    (15451): Unhandled Exception:
07-23 20:56:54.003 E/mono    (15451): Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Object reference not set to an instance of an object (See inner exception for details.) ---> System.NullReferenceException: Object reference not set to an instance of an object
In mgmain JNI_OnLoad
07-23 20:56:54.003 E/mono    (15451):   at System.Linq.Expressions.Expression.CreateLambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, System.Collections.ObjectModel.ReadOnlyCollection`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono    (15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono    (15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono    (15451):   at Autofac.Features.Generate
07-23 20:56:54.003 E/mono-rt (15451): [ERROR] FATAL UNHANDLED EXCEPTION: Autofac.Core.DependencyResolutionException: An exception was thrown while executing a resolve operation. See the InnerException for details. ---> Object reference not set to an instance of an object (See inner exception for details.) ---> System.NullReferenceException: Object reference not set to an instance of an object
07-23 20:56:54.003 E/mono-rt (15451):   at System.Linq.Expressions.Expression.CreateLambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, System.Collections.ObjectModel.ReadOnlyCollection`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono-rt (15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, System.String name, Boolean tailCall, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono-rt (15451):   at System.Linq.Expressions.Expression.Lambda (System.Type delegateType, System.Linq.Expressions.Expression body, IEnumerable`1 parameters) [0x00000] in <filename unknown>:0 
07-23 20:56:54.003 E/mono-rt (15451):   at Autofac.Feat
The program 'Mono' has exited with code 0 (0x0).

An example of my constructor is this

    public MainPage(Func<Action, Action<bool?>, MainMenu> mainMenu, 
                    ModulePage modulePage, 
                    LoadingPage loadingPage, 
                    Func<Action, AuthPage> authPage,
                    IUserDialogService userDialogService, 
                    IModuleService moduleService, 
                    ISyncCommand syncCommand)
    {
        _loadingPage = loadingPage;
        _authPage = authPage;
        _userDialogService = userDialogService;
        _moduleService = moduleService;
        _syncCommand = syncCommand;

        Title = Strings.app_title;
        var master = mainMenu(OnAfterDeauthenticated, OnToggleRequest());
        var detail = new NavigationPage(modulePage);

        if (App.Navigation == null)
        {
            App.Navigation = detail.Navigation;
        }

        Master = master;
        Detail = detail;
    }

The ContainerBuilder gets initialized in the Android App, and then passed to the PCL.

Android

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        Forms.Init(this, bundle);

        var container = RegisterDependencies();
        var page = App.GetMainPage(container);

        SetPage(page);
    }

    private static IContainer RegisterDependencies()
    {
        var containerBuilder = new ContainerBuilder();
        RegisterAndroidSpecific(containerBuilder);

        var coreIoc = new CoreIoC(containerBuilder);
        return coreIoc.Load();
    }

    private static void RegisterAndroidSpecific(ContainerBuilder containerBuilder)
    {
        containerBuilder.RegisterType<AudioPlayer>().SingleInstance().As<IAudioPlayer>();
        containerBuilder.RegisterType<SettingsService>().As<ISettingsService>();
        containerBuilder.RegisterType<NetworkService>().As<INetworkService>();
        containerBuilder.RegisterType<NativeVideoPlayer>().As<IVideoPlayer>();
        containerBuilder.RegisterType<MarkdownService>().As<IMarkdownService>();
        containerBuilder.RegisterType<UserDialogService>().As<IUserDialogService>();

        containerBuilder.RegisterType<DbProvider>()
            .SingleInstance()
            .WithParameters(new List<Parameter> {
                new TypedParameter(typeof(string), "fsm-breathingroom"),
                new TypedParameter(typeof(SqliteSettings), new SqliteSettings {
                    CacheSize = 16777216,
                    SyncMode = SynchronizationModes.Off,
                    JournalMode = SQLiteJournalModeEnum.Off,
                    PageSize = 65536
                })
            })
            .As<IDbProvider>();
    }

PCL

    public static Page GetMainPage(IContainer container)
    {
        Container = container;

        RegisterCurrentUser(Container.Resolve<UserModel>());
        RunMigrations();
        RegisterCurrentUser();

        var rootPage = Container.Resolve<MainPage>();
        return rootPage;
    }

解决方案

Well, the solution was easy but unnecessary IMO.

I switched all the Func<> injections with actual factories.

private FooPageFactory _fooPage
public SomePage(FooPageFactory fooPage)
{
    _fooPage = fooPage;
    // when needed
    _fooPage.Init(SomeActionDependency);
}

这篇关于在调试Autofac依赖注入的作品,但在发行崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆