在Program.cs Azure Service Fabric中访问容器/ DI

时间:2018-02-05 05:28:03

标签: .net-core azure-service-fabric

使用默认的.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补充,因此需要读取动态配置值。

提前致谢。

0 个答案:

没有答案