[英]How to inject a service to a Job and setup IOC container configuration?
[英]Where to put my IoC Container configuration in Service Fabric Service?
我当时在考虑将IoC容器依赖项配置放在Service类中的RunAsync方法下,但我感到不好,这不是正确的选择。
Azure Service Fabric服务中是否有任何约定可以放置这种类型的配置而不会引起任何类型的冲突?
另外,您将在哪里放置处置电话?
注意:我正在为此使用Simple Injector,但其他IoC容器的其他示例也应该这样做。
您可以在program.cs的启动代码中创建IoC容器。 注册服务时,可以向其传递通过ServiceContext
传递的回调方法。 拥有ServiceContext
,就可以使用它来访问应用程序配置中的连接字符串等。
这是我用来创建Ninject内核的一些代码。
namespace AccountCommandService
{
internal static class Program
{
private static void Main()
{
try
{
ServiceRuntime.RegisterServiceAsync("AccountCommandServiceType",
context =>
{
// Create IoC container
var kernel = new ServiceKernel(context);
// Create Service
return new AccountCommandService(context,
kernel.Get<IAccountDataContextFactory>(), // Pull a DBContext factory from the IoC
);
}).GetAwaiter().GetResult();
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{
ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
throw;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.