什么会导致我的DI在发布模式下崩溃,在调试模式下它没问题?这是一个Xamarin.Forms应用程序,正在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).
我的构造函数的一个例子就是这个
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;
}
ContainerBuilder在Android App中初始化,然后传递给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;
}
答案 0 :(得分:4)
这是Mono Linker中删除System.Core中的一些代码的错误,请参阅https://bugzilla.xamarin.com/show_bug.cgi?id=22342 Xamarin声称它已于2014年11月在master中修复,但我还无法验证它。应该与下一个稳定版本之一登陆。
作为一种变通方法,您可以从链接器Project-&gt; Options-&gt; Android Build-&gt; Linker-&gt; Ignore assembly中排除System.Core。
或者,直接注册Func
也可以:
builder.Register<Func<FooPage>>(c => () => new FooPage());
答案 1 :(得分:3)
嗯,解决方案很容易,但不必要的IMO。
我用实际工厂切换了所有Func<>
次注射。
private FooPageFactory _fooPage
public SomePage(FooPageFactory fooPage)
{
_fooPage = fooPage;
// when needed
_fooPage.Init(SomeActionDependency);
}