Autofac解决通用控制器 [英] Autofac to resolve generic controller

查看:110
本文介绍了Autofac解决通用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用的主数据服务.示例:IMasterDataService<T>

I am having a master data service which is a generic one. Example: IMasterDataService<T>

我正在尝试以类似于MasterDataController<T>的方式为此构建一个Api控制器.我想使用autofac解析控制器,但是找不到相同的任何文档.

I am trying to build an Api controller for this in a similar fashion like MasterDataController<T>. I want to resolve the controller using autofac, but I can't find any documentation for the same.

尝试时,我收到错误消息,提示无法解析类型.

When I tried, I'm getting the error message as unable to resolve the type.

我正在像builder.RegisterGeneric这样注册该服务,但是控制器我没有实例,我想知道这是否完全可能.

I am registering like builder.RegisterGeneric for the service, but controller I'm not getting instance and I would like to know if this is possible at all.

推荐答案

好吧,无论如何,您无法在运行时解析通用类.您需要对其进行参数化.这意味着在解析通用控制器之前,您必须具有关闭该通用控制器的类型.我想到的这种类型的唯一合理来源是请求本身,否则控制器将不是通用的,对我而言这根本没有意义.因此,您将需要使用传入请求以某种方式设计您的类型,然后使用某种自定义控制器工厂或激活器或所使用的框架中的任何名称,根据该类型来解析您的控制器.例如,在.NET Core中,该名称为IControllerActivator,并且您需要使用.AddControllersAsServices()扩展名在DI容器中注册控制器.

Well, anyways, you cannot resolve generic class at runtime. You need to parameterize it. This means that before resolving generic controller you must have a type to close this generic. The only reasonable source of said type that comes to my mind is the request itself, otherwise controller would not be generic, it just doesn't make sense to me. So, you'll need to craft your type using incoming request somehow, and then resolve your controller based on that type using some kind of custom controller factory or activator or whatever it is called in the framework you're using. In the .NET Core, for example, that would be IControllerActivator, and you'll need to register controllers in the DI container using .AddControllersAsServices() extension before doing that.

编辑实际上,在

EDIT There's actually such an implementation for dotnet core right in the Microsoft's documentation. But they use slightly different technique: they do not actually register an open generic controller. They make all predefined variants of closed generic controllers and register them instead. I think that this is due to the fact that Microsoft's IoC container cannot resolve closed generic types from open generic registrations - at least it could not do that in .NET Core 1.x, I haven't tried it in 2.0 yet. However, it is possible with Autofac, so I think such an approach should work.

这里的另一种可能性是将IComponentContext注入非通用控制器,并在控制器本身中动态"解析主数据服务,从而设计出在控制器中直接关闭通用的类型.

Another possibility here could be to inject IComponentContext into non-generic controller and resolve master data service "on the fly" in the controller itself crafting the type needed to close generic right there in the controller.

无论如何,这是一个通用问题的通用答案.如果您需要更详细的答案,则需要对正在做的事情有更深入的了解.

Anyways, this is a generic answer to a generic question. If you need more detailed answer you'll need to provide more insight into what you're doing.

这篇关于Autofac解决通用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆