为了账号安全,请及时绑定邮箱和手机立即绑定

netcore2自动添加依赖的方法

netcore2自动添加依赖的方法

C#
慕的地6264312 2022-12-24 10:01:54
我有多对接口并像那样实现ICategoryService -> CategoryServiceICategoryTypeService -> CategoryTypeServiceIOrderService -> OrderServiceILoggingService -> 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>();请帮忙。
查看完整描述

1 回答

?
MM们

TA贡献1886条经验 获得超2个赞

这是一个工作演示:


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";

    }

}

将Data库引用添加到 asp.net core 项目


配置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);

        }

    }

}

用例:


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"));



查看完整回答
反对 回复 2022-12-24
  • 1 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号