第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

netcore2自動(dòng)添加依賴的方法

netcore2自動(dòng)添加依賴的方法

C#
慕的地6264312 2022-12-24 10:01:54
我有多對(duì)接口并像那樣實(shí)現(xiàn)ICategoryService -> CategoryServiceICategoryTypeService -> CategoryTypeServiceIOrderService -> OrderServiceILoggingService -> LoggingService所有的類和接口都在Data.dll里面,我像這樣循環(huán)它。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);        }    }}我得到這個(gè)錯(cuò)誤InvalidOperationException:嘗試激活“VietWebSite.Web.Areas.WebApi.Administrator.ValuesController”時(shí)無法解析類型“VietWebSite.Service.ILoggingService”的服務(wù)。但如果我將代碼更改為這樣,它就會(huì)起作用:services.AddTransient<ILoggingService, LoggingService>();services.AddTransient<ICategoryService, CategoryService>();services.AddTransient<ICategoryTypeService, CategoryTypeService>();services.AddTransient<IOrderService, OrderService>();請(qǐng)幫忙。
查看完整描述

1 回答

?
MM們

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊

這是一個(gè)工作演示:


Data使用類和接口 創(chuàng)建庫:


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 項(xiàng)目


配置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()只會(huì)返回加載的程序集引起的,請(qǐng)嘗試以下代碼:


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



查看完整回答
反對(duì) 回復(fù) 2022-12-24
  • 1 回答
  • 0 關(guān)注
  • 119 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)