c# – 如何注册通用服务

前端之家收集整理的这篇文章主要介绍了c# – 如何注册通用服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > vNext Dependency Injection Generic Interface1个
我打算将我的系统移动到通用服务层.
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框架包,我们现在可以注册开放和封闭的泛型.尝试一下,这两行都行.

原文链接:https://www.f2er.com/csharp/243166.html

猜你在找的C#相关文章