首页 新闻 搜索 专区 学院

如何运用AutoFac在WebAPI中注入多个接口

0
悬赏园豆:200 [已关闭问题] 关闭于 2015-01-09 17:53

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>

 

 
MVC_Wesley的主页 MVC_Wesley | 初学一级 | 园豆:16
提问于:2015-01-09 17:27
< >
分享
所有回答(3)
0
dudu | 园豆:37751 (高人七级) | 2015-01-09 17:42

注入一个我也可以实现,谢谢你,我再试试

支持(0) 反对(0) MVC_Wesley | 园豆:16 (初学一级) | 2015-01-09 17:44
0

用其他的替代了

MVC_Wesley | 园豆:16 (初学一级) | 2015-01-09 17:52
0

哥们解决了吗?我也面临同样问题。一个接口多个实现、一个类实现多个接口; 

我如果这样写可以: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();

这么写就有问题了,我就是想动态的实现才反射,如果能明确注册哪个实现类就没必要反射了

长沙大鹏 | 园豆:214 (菜鸟二级) | 2017-08-29 16:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册