简体   繁体   English

如何在 Service Fabric 中获取可靠服务的 ServiceContext?

[英]How to get the ServiceContext of a Reliable Service in Service Fabric?

The ServiceContext of a reliable service in Service Fabric is registered with the runtime (DI container) in the program.cs of the service: Service Fabric 中可靠服务的ServiceContext注册到服务的 program.cs 中的运行时(DI 容器):

ServiceRuntime.RegisterServiceAsync("RelDictQuerySvcType",
                    context => new RelDictQuerySvc(context)).GetAwaiter().GetResult();

How can I get that ServiceContext back from the DI container?如何从 DI 容器中取回 ServiceContext? There is no property on the ServiceRuntime to get it back. ServiceRuntime 上没有属性可以将其取回。 Also, I did not find it via the FabricClient .另外,我没有通过FabricClient找到它。 Do I need to put the context on an own static class in the service constructor to be able to get a reference to it somewhere else in my code?我是否需要将上下文放在服务构造函数中自己的静态类上,以便能够在我的代码中的其他地方获得对它的引用?

Service Fabric does not really has a build-in DI mechanism, at least it is a very simple one. Service Fabric 并没有真正内置 DI 机制,至少它是一个非常简单的机制。

If you want to inject dependencies into you services itself you can use a factory.如果您想将依赖项注入您的服务本身,您可以使用工厂。 for example:例如:

ServiceRuntime.RegisterServiceAsync("MyStatelessType",
                context =>
                {
                    var loggerFactory = new LoggerFactoryBuilder(context).CreateLoggerFactory(applicationInsightsKey);
                    ILogger logger = loggerFactory.CreateLogger<MyStateless>();

                    return new MyStateless(context, logger);
                }).GetAwaiter().GetResult();

this is a way to inject concrete implementations in your service.这是一种在您的服务中注入具体实现的方法。 This mechanism is used to inject the context as well.这种机制也用于注入上下文。 Unfortunately, since it is not a full fledged DI container you cannot get this context outside the service instance itself.不幸的是,由于它不是一个成熟的 DI 容器,因此您无法在服务实例本身之外获得此上下文。

So, you have to bring your own DI container to really use it, for example in a stateless web api you can do something like:因此,您必须携带自己的 DI 容器才能真正使用它,例如在无状态 Web api 中,您可以执行以下操作:

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new[]
        {
            new ServiceInstanceListener(serviceContext =>
                new WebListenerCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                {
                    logger.LogStatelessServiceStartedListening<WebApi>(url);

                    return new WebHostBuilder().UseWebListener()
                                .ConfigureServices(
                                    services => services
                                        .AddSingleton(serviceContext)
                                        .AddSingleton(logger)
                                        .AddTransient<IServiceRemoting, ServiceRemoting>())
                                .UseContentRoot(Directory.GetCurrentDirectory())
                                .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                .UseStartup<Startup>()
                                .UseUrls(url)
                                .Build();
                }))
        };
    }

Otherwise you have to do it yourself.否则你必须自己做。 There are some initiatives already, see this one for an AutoFac extensions and there is also a Unity extensions.已经有一些计划,请参阅计划以了解 AutoFac 扩展和Unity扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM