简体   繁体   English

Autofac解决通用控制器

[英]Autofac to resolve generic controller

I am having a master data service which is a generic one. 我有一个通用的主数据服务。 Example: IMasterDataService<T> 示例: IMasterDataService<T>

I am trying to build an Api controller for this in a similar fashion like MasterDataController<T> . 我正在尝试以类似MasterDataController<T>方式为此构建一个Api控制器。 I want to resolve the controller using autofac, but I can't find any documentation for the same. 我想使用autofac解析控制器,但找不到相同的任何文档。

When I tried, I'm getting the error message as unable to resolve the type. 尝试时,出现错误消息,提示无法解析类型。

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. 我正在像builder.RegisterGeneric这样注册该服务,但是控制器我没有实例,我想知道这是否有可能。

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. 例如,在.NET Core中,这将是IControllerActivator ,并且您需要使用.AddControllersAsServices()扩展名在DI容器中注册控制器。

EDIT There's actually such an implementation for dotnet core right in the Microsoft's documentation . 编辑 Microsoft的文档中实际上有一个针对dotnet core的实现。 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. 我认为这是由于Microsoft的IoC容器无法从开放的通用注册中解析封闭的通用类型的事实-至少它无法在.NET Core 1.x中做到这一点,我还没有在2.0中进行过尝试。 However, it is possible with Autofac, so I think such an approach should work. 但是,Autofac可以实现,因此我认为这种方法应该可行。

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. 这里的另一种可能性是将IComponentContext注入到非通用控制器中,并在控制器本身中“动态”解析主数据服务,从而设计出在控制器中关闭通用的类型。

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. 如果您需要更详细的答案,则需要对正在做的事情有更深入的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM