AutoFac注册代码:
var configuration = GlobalConfiguration.Configuration; var builder = new ContainerBuilder(); Assembly assembly = Assembly.Load("JenToo.DataConter.WebHost"); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterControllers(assembly); builder.RegisterTypes(Assembly.Load("JenToo.DataConter.Adapter").GetTypes()).AsImplementedInterfaces(); builder.RegisterTypes(Assembly.Load("JenToo.DataCenter.Repository").GetTypes()).AsImplementedInterfaces(); builder.RegisterTypes(Assembly.Load("JenToo.DataCenter.Service").GetTypes()).AsImplementedInterfaces(); IContainer container = builder.Build(); CacheHelper.SetData(CacheKeys.containerKey, container); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); var resolver = new AutofacWebApiDependencyResolver(container); configuration.DependencyResolver = resolver;
错误信息:
<Error> <Message>发生错误。</Message> <ExceptionMessage> None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'JenToo.DataConter.WebHost.ApiControllers.RegisterUserController' can be invoked with the available services and parameters: Cannot resolve parameter 'JenToo.DataConter.Adapter.Definition.ICoreMessageRecordAdapter _coreMessageRecordAdapter' of constructor 'Void .ctor(JenToo.DataConter.Adapter.Definition.ISysPersonAdapter, JenToo.DataConter.Adapter.Definition.ICoreMessageRecordAdapter)'. </ExceptionMessage> <ExceptionType>Autofac.Core.DependencyResolutionException</ExceptionType> <StackTrace> 在 Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) 在 Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) 在 Autofac.Core.Resolving.InstanceLookup.Execute() 在 Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) 在 Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) 在 Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) 在 Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) 在 Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) 在 Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) 在 Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType, IEnumerable`1 parameters) 在 Autofac.ResolutionExtensions.ResolveOptional(IComponentContext context, Type serviceType) 在 Autofac.Integration.WebApi.AutofacWebApiDependencyScope.GetService(Type serviceType) 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) 在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) </StackTrace> </Error>
用其他的替代了
哥们解决了吗?我也面临同样问题。一个接口多个实现、一个类实现多个接口;
我如果这样写可以:builder.RegisterType(serverType).As<IServer<string>>().InstancePerLifetimeScope();
但是如果我是通过反射获取到实现类的程序集合接口程序集,再进行IOC注册就会有问题,如:
Type serverType = serverAssembly.GetType(_reg.ServerDll + "." + _reg.ServerName, false, true);
Type interfaceType = interAssembly.GetTypes().FirstOrDefault(p => p.Name.Contains(_reg.InterfaceName));
builder.RegisterType(serverType).As(interfaceType).InstancePerLifetimeScope();
这么写就有问题了,我就是想动态的实现才反射,如果能明确注册哪个实现类就没必要反射了