为什么在ServiceFactory函数完成之前ServiceRuntime.RegisterServiceAsync返回? [英] Why does ServiceRuntime.RegisterServiceAsync return before the serviceFactory func completes?

查看:91
本文介绍了为什么在ServiceFactory函数完成之前ServiceRuntime.RegisterServiceAsync返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 ServiceRuntime.RegisterServiceAsync 在实际注册服务和作为参数传递的工厂Func完成之前返回.

I find it very unintuitive that the ServiceRuntime.RegisterServiceAsync returns before the service is actually registered and the factory Func that is passed as a parameter completes.

考虑需要解析正在注册的相同ServiceName的情况.您只能从在serviceFactory Func中获得的ServiceContext中获得ServiceName.您认为一旦获得上下文,就可以在serviceFactory Func中进行解析.但是,然后您输入一个死锁",因为它等待您注册的服务完成,所以解析调用将永远卡住.

Think about the scenario where you need to resolve the same ServiceName you're registering. You can only get the ServiceName from the ServiceContext you get in the serviceFactory Func. You'd think you could resolve within the serviceFactory Func once you get the context. But then you enter a "deadlock" where the resolve call would get stuck forever since it waits for the service you're registering to finish.

这就是为什么我希望RegisterServiceAsync仅在serviceFactory Func也完成之后才能完成.

This is why I'd expect for the RegisterServiceAsync to complete only after serviceFactory Func is completed too.

推荐答案

由于

It returns before the method runs because the ServiceRuntime.RegisterServiceAsync() is a factory registration, it is not a constructor building the service.

签名:

public Task RegisterStatefulServiceFactoryAsync(string serviceTypeName, IStatefulServiceFactory factory, TimeSpan timeout, CancellationToken cancellationToken)

public Task RegisterStatefulServiceFactoryAsync(string serviceTypeName, IStatefulServiceFactory factory, TimeSpan timeout, CancellationToken cancellationToken)

这里的概念是告诉运行时,只要运行时需要启动serviceTypeName的实例,就应该调用factory Func<System.Fabric.StatefulServiceContext,Microsoft.ServiceFabric.Services.Runtime.StatefulServiceBase>.该工厂负责设置该服务所需的依赖项,如果需要进行依赖项注入,则可以在此时进行.

The concept here is to tell the Runtime that whenever the runtime need to start an instance of the serviceTypeName it should call the factory Func<System.Fabric.StatefulServiceContext,Microsoft.ServiceFabric.Services.Runtime.StatefulServiceBase>. This factory is the function responsible to setup the dependencies required for that service, and if you need to do dependency injection, you would do at this point.

运行时首先需要注册依赖项,然后开始构建服务.与添加依赖项注册并注册所有依赖项后,在ASPNET上执行的操作非常相似,然后调用Start().

The runtime need first to have the dependencies registered before it start building the services. Very similar to what you do on ASPNET when you add the dependency registrations and once all dependencies are registered, you call Start().

稍后,运行时将准备就绪,并且将从SF接收指令以从特定的服务类型启动实际的服务实例,这时将调用这些工厂并进行上下文构建.如果您在真正接到电话之前需要知道 serviceTypeName ,那就是鸡肉和鸡蛋的困境.

At a later point, the runtime will be ready and will receive the instructions from SF to start the actual service instance from a specific service type, at this point these factories will be called and the context build. If you need to know the serviceTypeName before you actually receive a call for it, it would be the chicken and the egg dilemma.

它以这种方式工作,因为您可以在同一主机上运行多个副本,因此每个实例将具有自己的上下文.而且,您还可以在同一主机上具有多个serviceType.

It works this way, because you can have multiple replicas running on same host, so each instance will have it's own context. And you could also have multiple serviceTypes on same host.

这篇关于为什么在ServiceFactory函数完成之前ServiceRuntime.RegisterServiceAsync返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆