如何注册通用服务

时间:2016-02-05 19:23:02

标签: c# asp.net generics dependency-injection asp.net-core

我打算将我的系统转移到通用服务层。

public interface IAbcService<TEntity> where TEntity : class

public class AbcService<TEntity> : IAbcService<TEntity> where TEntity : class

我尝试了一些东西,但它不起作用。

services.AddTransient<typeof(IAbcService<MyEntity>), AbcService();
....
....
....

我正在使用我的一些实体,并且我正在尝试将其添加到属性中。

如何在asp.net核心中注册通用服务?

2 个答案:

答案 0 :(得分:13)

之前我想尝试完全相同的事情,我已经让它像这样工作:

services.AddTransient(typeof(IAbcService<>), typeof(AbcService<>));
services.AddTransient(typeof(IAbcService<Person>), typeof(AbcService<Person>));

请注意不同的结构,这是通过方法中的参数进行注册。

详细说明,正如您在上面的示例中所看到的,使用新的DNX框架包,我们现在可以注册打开和关闭的泛型。 尝试一下,两条线都可以使用。

答案 1 :(得分:-3)

你试过吗

  

services.AddTransient&lt; IAbcService&lt;&gt ;, AbcService&gt;()