使用默认的.Net Core DI容器,是否可以将依赖项注入Program.cs Main()中的无状态SF服务构造?
即:
private static void Main()
{
try
{
ServiceRuntime.RegisterServiceAsync("Acme.Notification.ServiceType",
context => new NotificationService(context, new DispatchConfig())).GetAwaiter().GetResult();
ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(NotificationService).Name);
Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{
ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
throw;
}
}
服务类 - 仅限构造函数
public sealed class NotificationService : StatelessService
{
public NotificationService(StatelessServiceContext context,
DispatchConfig config)
: this(context)
{
_config = config;
}
}
用容器分辨率替换命令new DispatchConfig()
- 此类由appsettings.json
补充,因此需要读取动态配置值。
提前致谢。