亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號