dee*_*ute 2 c# ioc-container simple-injector azure-service-fabric
我正在考虑在Service类中的RunAsync方法下放置IoC容器依赖项配置,但我感觉不是正确的地方.
Azure Service Fabric Services中是否有任何约定来放置此类配置而不会引起任何类型的冲突?
此外,您将在哪里进行处置呼叫?
注意:我正在使用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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
473 次 |
| 最近记录: |