合并程序集的Autofac解决问题

时间:2014-07-15 00:19:58

标签: .net autofac ilmerge

我们正在使用Autofac来构建我们的WPF应用程序,并且我们以这种方式对许多相关项目使用自动注册:

builder.RegisterAssemblyTypes(type).AsSelf().AsImplementedInterfaces().PreserveExistingDefaults();

除此之外,我们还有许多类型的明确注册,以便拥有单身生活方式以及许多Func工厂。工厂的注册通常如下:

   builder.Register<Func<ParameterType, ServiceType>>(
       c =>
       {
           var context = c.Resolve<IComponentContext>();

           return (parameter) => context.Resolve<ServiceImplementation>(
               new TypedParameter(typeof(ParameterType), parameter));
       });

所有这些注册都按逻辑分组在许多按顺序注册的模块中。

所有这些都可以正常工作,直到我们开始合并一些相关的程序集。我们需要这样做,因为我们必须对代码的一部分进行模糊处理,并且必须合并所有混淆的程序集以便仍然能够找到&#34;找到&#34;彼此的类型。出于同样的原因,我们也合并了Autofac程序集。

在合并的应用程序中,解析简单的实例注册工作正常。

但是,当解析应用程序的复杂主ViewModel时,会发生以下异常:

Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Autofac.Core.Registration.ScopeRestrictedRegistry' can be invoked with the available services and parameters:
Cannot resolve parameter 'System.Object scopeTag' of constructor 'Void .ctor(System.Object)'.
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context)
   at ApplicationModulesModule.<Load>b__1(IComponentContext c) in d:\Development\Production\Application\Application.Shell\AutofacSetup.cs:line 239
   at Autofac.RegistrationExtensions.<>c__DisplayClass10`1.<Register>b__f(IComponentContext c, IEnumerable`1 p)
   at Autofac.Builder.RegistrationBuilder.<>c__DisplayClass1`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p)
   at Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Activators.Reflection.AutowiringParameter.<>c__DisplayClass2.<CanSupplyValue>b__0()
   at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
   at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
   at Autofac.Core.Resolving.InstanceLookup.Execute()
   at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters)
   at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance)
   at Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Type serviceType, Object& instance)
   at CaliburnBootstrapper.GetInstance(Type serviceType, String key) in d:\Development\Production\Application\Application.Shell\CaliburnBootstrapper.cs:line 102
   at Caliburn.Micro.BootstrapperBase.DisplayRootViewFor(Type viewModelType, IDictionary`2 settings)
   at CaliburnBootstrapper.<StartShell>b__13() in d:\Development\Production\Application\Application.Shell\CaliburnBootstrapper.cs:line 254
   at CaliburnBootstrapper.StartShell() in d:\Development\Production\Application\Application.Shell\CaliburnBootstrapper.cs:line 254
   at CaliburnBootstrapper.<>c__DisplayClass21.<GetConfiguredApplicationUpdater>b__18(Object o, FailArgs args) in d:\Development\Production\Application\Application.Shell\CaliburnBootstrapper.cs:line 340
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

ApplicationModulesModule是一个Module,只需在其Load方法中注册所有其他模块。

从我可以做的堆栈跟踪和SmartAssembly(我们用于合并和混淆,虽然这已经发生在未经模糊处理的构建中)报告是错误显然在Autofac中发生,我可以&#39;跟进正在发生的事情。

我特别无法做出scopeTag这件事的任何事情;我们没有在任何地方使用标记范围;唯一明确创建的Lifetime Scope是用作主容器的{(1 {1}})。

有谁知道这里的问题是什么?如果必要/有帮助,我可以在堆栈跟踪中的给定点提供特定属性的类型/值。

谢谢!

0 个答案:

没有答案