Azure Service Fabric服务之间的Http通信

时间:2016-12-23 20:51:12

标签: c# azure-service-fabric service-fabric-stateful

我的情况:

问题:

我试图将我的状态服务和无状态WebAPI之间的通信从RPC更改为HTTP通信,因为我想避免使用接口在服务之间进行通信。这甚至可以通过HTTP通信吗?如果是这样,我的无状态WebAPI如何在不使用接口的情况下调用有状态应用程序中的特定方法?

更新

感谢alltej,我开始阅读有关HttpCommunicationListeners的更多信息。本教程(https://docs.microsoft.com/nl-nl/azure/service-fabric/service-fabric-concepts-partitioning)很好地解释了Http Communication。

在下面的代码片段中:" CreateServiceReplicaListeners()"调用CreateInternalListener()然后调用" ProcessInternalRequest()"然后最终调用" AddUserAsync()"。

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new[] { new ServiceReplicaListener(context => this.CreateInternalListener(context))};
    }

    private ICommunicationListener CreateInternalListener(ServiceContext context)
    {
        EndpointResourceDescription internalEndpoint = context.CodePackageActivationContext.GetEndpoint("ProcessingServiceEndpoint");

        string uriPrefix = String.Format(
            "{0}://+:{1}/{2}/{3}-{4}/",
            internalEndpoint.Protocol,
            internalEndpoint.Port,
            context.PartitionId,
            context.ReplicaOrInstanceId,
            Guid.NewGuid());

        string nodeIP = FabricRuntime.GetNodeContext().IPAddressOrFQDN;

        string uriPublished = uriPrefix.Replace("+", nodeIP);
        return new HttpCommunicationListener(uriPrefix, uriPublished, this.ProcessInternalRequest);
    }

    private async Task ProcessInternalRequest(HttpListenerContext context, CancellationToken cancelRequest)
    {
        string output = null;
        string user = context.Request.QueryString["lastname"].ToString();

        try
        {
            output = await this.AddUserAsync(user);
        }
        catch (Exception ex)
        {
            output = ex.Message;
        }

        using (HttpListenerResponse response = context.Response)
        {
            if (output != null)
            {
                byte[] outBytes = Encoding.UTF8.GetBytes(output);
                response.OutputStream.Write(outBytes, 0, outBytes.Length);
            }
        }
    }

    private async Task<string> AddUserAsync(string user)
    {
        IReliableDictionary<String, String> dictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<String, String>>("dictionary");

        using (ITransaction tx = this.StateManager.CreateTransaction())
        {
            bool addResult = await dictionary.TryAddAsync(tx, user.ToUpperInvariant(), user);

            await tx.CommitAsync();

            return String.Format(
                "User {0} {1}",
                user,
                addResult ? "sucessfully added" : "already exists");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

MyCustomHttpListener是一个必须创建的类,它是您自己的实现ICommunicationListener的自定义侦听器。它包含您需要覆盖的三种方法。有关OwinCommunicationListener,请参阅此处的示例:https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-webapi