将我的 IoC 容器配置放在 Service Fabric 服务中的什么位置?
Where to put my IoC Container configuration in Service Fabric Service?
我正在考虑将 IoC 容器依赖项配置放在服务中的 RunAsync 方法下 class 但我有一种不好的感觉,这不是正确的地方..
Azure Service Fabric 服务中是否有任何约定来放置此类配置而不会引起任何类型的冲突?
另外,你会把 dispose 调用放在哪里?
注意:我为此使用了 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;
}
}
}
}
我正在考虑将 IoC 容器依赖项配置放在服务中的 RunAsync 方法下 class 但我有一种不好的感觉,这不是正确的地方..
Azure Service Fabric 服务中是否有任何约定来放置此类配置而不会引起任何类型的冲突?
另外,你会把 dispose 调用放在哪里?
注意:我为此使用了 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;
}
}
}
}