加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何注册通用服务

发布时间:2020-12-15 08:19:28 所属栏目:百科 来源:网络整理
导读:参见英文答案 vNext Dependency Injection Generic Interface1个 我打算将我的系统移动到通用服务层. public interface IAbcServiceTEntity where TEntity : classpublic class AbcServiceTEntity : IAbcServiceTEntity where TEntity : class 我尝试了一些
参见英文答案 > 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框架包,我们现在可以注册开放和封闭的泛型.尝试一下,这两行都行.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读