我在下面的示例中使用autofac在运行时创建一个类实例。 http://autofaccn.readthedocs.io/en/latest/advanced/delegate-factories.html
但是在本地运行时,我发现异常。我是autofac的新手,如果我在这里做错了或者您需要更多信息,请告诉我。
代码结构:
public class ClassName
{
public delegate ClassName Factory(Type1 obj1, string obj2);
public ClassName(Type1 obj1, string obj2)
{
this.type1 = obj1;
this.type2= obj2;
}
/*Some methods of this class that use type1 and type2*/
}
// Registration
container.RegisterType<Type1>();
container.RegisterType<ClassName.Factory>();
container.RegisterType<ClassName>().AsSelf().InstancePerDependency();
// In some method to create an instance of ClassName
var factory = container.Resolve<Factory>(); // This is throwing the following exception.
var instance = factory(obj1Instance, "text"); // obj1Instance and "text" parameters cannot be determined at registration time.
异常stackTrace:
Logging handled exception: Autofac.Core.DependencyResolutionException: Autofac.Core.DependencyResolutionException: An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = Factory (ReflectionActivator), Services = [ClassName+Factory], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ClassName+Factory' can be invoked with the available services and parameters:
Cannot resolve parameter 'System.Object object' of constructor 'Void .ctor(System.Object, IntPtr)'. (See inner exception for details.) ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ClassName+Factory' can be invoked with the available services and parameters:
Cannot resolve parameter 'System.Object object' of constructor 'Void .ctor(System.Object, IntPtr)'.
at Autofac.Core.Activators.Reflection.ReflectionActivator.GetValidConstructorBindings(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters)
at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters)
--- End of inner exception stack trace ---
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.Execute(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[TService](IComponentContext context, IEnumerable`1 parameters)
答案 0 :(得分:1)
您遇到的问题是您正在尝试注册工厂。 Autofac了解如何解决委托,但它并不了解如何注册委托。
这应该有效:
public class ClassName
{
public delegate ClassName Factory(Type1 obj1, string obj2);
public ClassName(Type1 obj1, string obj2)
{
this.type1 = obj1;
this.type2= obj2;
}
}
// Registration
container.RegisterType<ClassName>().AsSelf().InstancePerDependency();
// In some method to create an instance of ClassName
var factory = container.Resolve<ClassName.Factory>();
var instance = factory.Invoke(obj1Instance, "text");
自己运行后,我得到了同样的例外,但很快意识到问题所在。引用autofac's entry on delegate factories我看到了这个:
默认情况下,Autofac将委托的参数与名称
的构造函数的参数相匹配
我的构造函数的参数名称和我的代表&#39;参数没有匹配导致autofac
尝试按类型解析对象,并且因为你没有(并且除非你知道你想要,否则不应该这样做,即便如此,你不应该注册string
autofac
&#39; Autofac.Core.Activators.Reflection.DefaultConstructorFinder
失败。