如何在netcore2中自动添加依赖项

时间:2019-04-21 16:53:05

标签: c# asp.net-core-2.0

我有多对接口并实现了这样的

ICategoryService -> CategoryService
ICategoryTypeService -> CategoryTypeService
IOrderService -> OrderService
ILoggingService -> LoggingService

所有类和接口都在Data.dll中,我这样循环它。

foreach (var type in serviceAssembly.GetTypes())
{
    if (type.Name.Contains("Repository") && !type.IsInterface && !type.IsGenericType)
    {
        Type interfaceImplement = type.GetInterfaces().SingleOrDefault(t => t.IsGenericType == false);

        if (interfaceImplement != null)
        {
            System.Diagnostics.Debug.WriteLine($"{type.Name} is inherited by {interfaceImplement.Name}");
            services.AddTransient(interfaceImplement, type);
        }
    }
}

我收到此错误

  

InvalidOperationException:尝试激活“ VietWebSite.Web.Areas.WebApi.Administrator.ValuesController”时,无法解析类型为“ VietWebSite.Service.ILoggingService”的服务。

但是如果我将代码更改为这样就可以了:

services.AddTransient<ILoggingService, LoggingService>();
services.AddTransient<ICategoryService, CategoryService>();
services.AddTransient<ICategoryTypeService, CategoryTypeService>();
services.AddTransient<IOrderService, OrderService>();

请帮助。

谢谢

2 个答案:

答案 0 :(得分:0)

您的操作方式是正确的手动操作方式。我知道内置的并没有一种自动注入依赖项的方法,但是有可用的软件包,例如AutoFac https://autofac.org/

答案 1 :(得分:0)

这是一个工作示例:

  1. 使用类和接口创建Data库:

    public interface ICategoryService
    {
        string Output();
    }
    public class CategoryService : ICategoryService
    {
        public string Output()
        {
            return "CategoryService.Output";
        }
    }
    public interface ILoggingService
    {
        string Output();
    }
    public class LoggingService : ILoggingService
    {
        public string Output()
        {
            return "LoggingService.Output";
        }
    }
    
  2. Data库参考添加到asp.net核心项目

  3. 一样配置Startup.cs
    var serviceAssembly = Assembly.GetAssembly(typeof(CategoryService));
    foreach (var type in serviceAssembly.GetTypes())
    {
        if (type.Name.Contains("Service") && !type.IsInterface && !type.IsGenericType)
        {
            Type interfaceImplement = type.GetInterfaces().SingleOrDefault(t => t.IsGenericType == false);
    
            if (interfaceImplement != null)
            {
                System.Diagnostics.Debug.WriteLine($"{type.Name} is inherited by {interfaceImplement.Name}");
                services.AddTransient(interfaceImplement, type);
            }
        }
    }
    
  4. 用例:

    public class HomeController : Controller
    {
        private readonly ILoggingService _loggingService;
        public HomeController(ILoggingService loggingService)
        {
            _loggingService = loggingService;
        }
        public IActionResult Index()
        {
            var result = _loggingService.Output();
            return View();
        }        
    }
    

更新

您的问题是由AppDomain.CurrentDomain.GetAssemblies()仅返回已加载的程序集引起的,请尝试以下代码:

//Load Assemblies
//Get All assemblies.
var refAssembyNames = Assembly.GetExecutingAssembly()
    .GetReferencedAssemblies();
//Load referenced assemblies
foreach (var asslembyNames in refAssembyNames)
{
    Assembly.Load(asslembyNames);
}
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
var myAssemblies = assemblies.Where(assem => assem.GetName().Name.Contains("VietWebSite.Data") || assem.GetName().Name.Equals("VietWebSite.Service"));