netcore2自动添加依赖的方法
How to add dependencies automatically in netcore2
我有多对接口并像那样实现
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: Unable to resolve service for type 'VietWebSite.Service.ILoggingService' while attempting to activate 'VietWebSite.Web.Areas.WebApi.Administrator.ValuesController'.
但是如果我将我的代码改成这样的话它会起作用:
services.AddTransient<ILoggingService, LoggingService>();
services.AddTransient<ICategoryService, CategoryService>();
services.AddTransient<ICategoryTypeService, CategoryTypeService>();
services.AddTransient<IOrderService, OrderService>();
请帮忙。
谢谢
您的操作方式是正确的手动操作方式。没有一种自动注入依赖项的方法我知道是内置的,但是有可用的包,例如 AutoFac https://autofac.org/.
这是一个工作演示:
使用 class 和接口创建 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 核心项目
像
那样配置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()
只会 return 加载程序集,请尝试以下代码:
//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"));
我有多对接口并像那样实现
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: Unable to resolve service for type 'VietWebSite.Service.ILoggingService' while attempting to activate 'VietWebSite.Web.Areas.WebApi.Administrator.ValuesController'.
但是如果我将我的代码改成这样的话它会起作用:
services.AddTransient<ILoggingService, LoggingService>();
services.AddTransient<ICategoryService, CategoryService>();
services.AddTransient<ICategoryTypeService, CategoryTypeService>();
services.AddTransient<IOrderService, OrderService>();
请帮忙。
谢谢
您的操作方式是正确的手动操作方式。没有一种自动注入依赖项的方法我知道是内置的,但是有可用的包,例如 AutoFac https://autofac.org/.
这是一个工作演示:
使用 class 和接口创建
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 核心项目像
那样配置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()
只会 return 加载程序集,请尝试以下代码:
//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"));