如何注册通用服务

本文关键字:服务 注册 何注册 | 更新日期: 2023-09-27 18:33:22

我计划将我的系统移动到通用服务层。

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

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

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

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

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

如何注册通用服务

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

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

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

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

你试过吗

服务业。AddTransient<>, abcService>()