访问有状态服务

时间:2016-04-13 05:35:19

标签: azure azure-service-fabric

我一直在研究Azure的Service Fabric Reliable Services并查看样本。

我现在正在使用推荐的设置构建一个简单的概念验证应用程序:一个无状态Web API服务,后面有一个有状态服务(1个分区)。

我一直在寻找最简单的方法让API服务与后面的有状态服务进行对话。看起来状态服务也必须使用Web API公开(如示例WordCount应用程序中所示)。

我是否正确地认为,为了使用有状态服务,它需要使用类似OwinCommunicationListener : ICommunicationListener之类的内容通过HTTP / WCF等公开自己?

2 个答案:

答案 0 :(得分:2)

如果有状态和无状态服务在同一个集群中(在您的情况下,我假设两个服务属于同一应用程序类型(相同的解决方案)),您可以从无状态服务调用有状态服务使用'服务代理'< / p>

如果要从群集外部使用有状态服务,则需要打开API通信侦听器。

ServiceProxy 类可从“Microsoft.ServiceFabric.Services”获得。

服务代理仅适用于同一群集。

使用

IServiceClass proxy = ServiceProxy.Create<IServiceClass >(new Uri(fabric:/your_service));

为了代理工作,需要使用接口公开有状态服务中的方法。

答案 1 :(得分:2)

当服务存在于同一个应用程序中时,您可以访问服务实例,例如:

    public static MyServices.Interfaces.IMyStatefulService GetMyStatefulService()
    {
        var proxyLocation = new ServiceUriBuilder("MyStatefulService");
        var partition = new ServicePartitionKey(1); //provide the partitionKey for stateful services. for stateless services, you can just comment this out
        return ServiceProxy.Create<MyServices.Interfaces.IMyStatefulService>(proxyLocation.ToUri(), partition);
    }

ServiceProxy来自Microsoft.ServiceFabric.Services.Remoting.Client命名空间。

您的界面代码如下所示:

public interface IMyStatefulService : IService
{
    Task<MyResponseResult> DoSomething(MyRequest request);
}