在.NET Core Service应用程序中使用依赖注入 [英] Using Dependency Injection in .NET Core Service application
问题描述
我们在.net核心中有一个基于服务的应用程序,它将在Linux环境中作为守护程序运行。一切都按预期工作,但我在处理依赖项注入时遇到问题。
以下是参考代码
We have a service based application in .net core which would run as a daemon in Linux environment. Everything is working as expected but i am having problem in handling dependency injection. Below is the code for reference
Program.cs
Program.cs
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("Starting PreProcessor Application ");
try
{
ConfigParameters.LoadSettings(args);
}
catch (Exception ex)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine($"Error in setting config parameters {ex.Message}");
return;
}
IHost host = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddLogging();
services.AddHostedService<MainService>();
services.AddTransient<IMessageQueue, ActiveMQHandler>(x =>
{
return new ActiveMQHandler(ConfigParameters.Settings.MessageQueueAddress);
});
services.AddTransient<IMessageQueue, ActiveMQHandler>(x =>
{
return new ActiveMQHandler(ConfigParameters.Settings.MessageQueueAddress);
});
services.AddTransient<IMessageQueue, ActiveMQHandler>(x =>
{
return new ActiveMQHandler(ConfigParameters.Settings.MessageQueueAddress);
});
})
.Build();
await host.RunAsync();
}
}
MainService的构造函数
看起来像这样
IApplicationLifetime appLifetime;
IConfiguration configuration;
PreProcessorQueueListener listener;
private string reason = "SHUTDOWN SIGNAL";
private IMessageQueue messageQueue;
private IMessageQueue messageQueueSL;
private IMessageQueue messageQueueSLProcess;
public MainService(IConfiguration configuration, IApplicationLifetime appLifetime, IMessageQueue messageQueue, IMessageQueue messageQueueSL, IMessageQueue messageQueueSLProcess)
{
this.configuration = configuration;
this.messageQueue = messageQueue;
this.messageQueueSL = messageQueueSL;
this.messageQueueSLProcess = messageQueueSLProcess;
this.appLifetime = appLifetime;
}
如果您在我的 MainService中看到代码我使用构造函数依赖注入为
IMessageQueue
接口传递了三个实例。我真正想要的是基于应用程序任何部分的需求,我可以通过传递 IMessageQueue $ c来获取
类。 ActiveMQHandler
类的新实例$ c>界面。由于我无法为此找到解决方案,因此我传递了三个实例(我对这种解决方案不满意) IMessageQueue
。如果我需要使用 ActiveMQHandler
类的另一个实例,那么我将不得不在我的<中将第四个参数作为 IMessageQueue
接口传递code> MainService
If you see in my MainService
code i am passing three instances for IMessageQueue
interface using constructor dependency injection. What i really want is based on a need in any part of the application i could grab a new instance of ActiveMQHandler
class by passing IMessageQueue
interface. Since i could not find a solution for this i am passing three instances (i am not happy with this solution) of IMessageQueue
. If i need to use another instance of ActiveMQHandler
class then i will have to pass fourth parameter as IMessageQueue
interface in my MainService
class.
我真正想要的是使用 ServiceProvider
(或更优雅的东西)并使用它来获得实现<的类的新实例/单例(基于 Program.cs
的定义) code> IMessageQueue 接口。
What i am really looking for is use ServiceProvider
(or something more elegant) and use that to get a new / singleton (based on how it is defined in Program.cs
) instance of the class which implements the IMessageQueue
interface.
一个建议者?
推荐答案
只需将构造函数更改为包含 IEnumerable< IMessageQueue>
。
应该给您所有已注册的IMessageQueue实现者的列表。
Just change the constructor to contain IEnumerable<IMessageQueue>
.
It should give you a list of all registered IMessageQueue implementers.
我个人不喜欢在类中依赖IApplicationLifetime或IServiceProvider。这有点像ServiceLocator反模式。
Personally I do not like taking on dependencies on IApplicationLifetime or IServiceProvider in my classes. This is a bit like the ServiceLocator anti-pattern.
这篇关于在.NET Core Service应用程序中使用依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!