Reliable Services 基于 WCF 的通信堆栈WCF-based communication stack for Reliable Services

Reliable services 框架使服务创作者能够选择他们要用于其服务的通信堆栈。The Reliable Services framework allows service authors to choose the communication stack that they want to use for their service. 他们可以通过从 CreateServiceReplicaListeners or CreateServiceInstanceListeners 方法返回的 ICommunicationListener 来插入所选的通信堆栈。They can plug in the communication stack of their choice via the ICommunicationListener returned from the CreateServiceReplicaListeners or CreateServiceInstanceListeners methods. 对于想要使用基于 Windows Communication Foundation (WCF) 的通信的服务创作者,该框架提供了基于 WCF 的通信堆栈实现。The framework provides an implementation of the communication stack based on the Windows Communication Foundation (WCF) for service authors who want to use WCF-based communication.

WCF 通信侦听器WCF Communication Listener

特定于 WCF 的 ICommunicationListener 实现由 Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener 类提供。The WCF-specific implementation of ICommunicationListener is provided by the Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener class.

假设我们有 ICalculator 类型的服务协定Lest say we have a service contract of type ICalculator

public interface ICalculator
    Task<int> Add(int value1, int value2);

我们可以通过下列方式在服务中创建 WCF 通信侦听器。We can create a WCF communication listener in the service the following manner.

protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    return new[] { new ServiceReplicaListener((context) =>
        new WcfCommunicationListener<ICalculator>(
            // The name of the endpoint configured in the ServiceManifest under the Endpoints section
            // that identifies the endpoint that the WCF ServiceHost should listen on.
            endpointResourceName: "WcfServiceEndpoint",

            // Populate the binding information that you want the service to use.
            listenerBinding: WcfUtility.CreateTcpListenerBinding()

为 WCF 通信堆栈编写客户端Writing clients for the WCF communication stack

为编写客户端以便使用 WCF 与服务进行通信,该框架提供了WcfClientCommunicationFactory,这是特定于 WCF 的 ClientCommunicationFactoryBase 实现。For writing clients to communicate with services by using WCF, the framework provides WcfClientCommunicationFactory, which is the WCF-specific implementation of ClientCommunicationFactoryBase.

public WcfCommunicationClientFactory(
    Binding clientBinding = null,
    IEnumerable<IExceptionHandler> exceptionHandlers = null,
    IServicePartitionResolver servicePartitionResolver = null,
    string traceId = null,
    object callback = null);

可以从 WcfCommunicationClientFactory 创建的 WcfCommunicationClient 访问 WCF 通信通道。The WCF communication channel can be accessed from the WcfCommunicationClient created by the WcfCommunicationClientFactory.

public class WcfCommunicationClient : ServicePartitionClient<WcfCommunicationClient<ICalculator>>
       public WcfCommunicationClient(ICommunicationClientFactory<WcfCommunicationClient<ICalculator>> communicationClientFactory, Uri serviceUri, ServicePartitionKey partitionKey = null, TargetReplicaSelector targetReplicaSelector = TargetReplicaSelector.Default, string listenerName = null, OperationRetrySettings retrySettings = null)
           : base(communicationClientFactory, serviceUri, partitionKey, targetReplicaSelector, listenerName, retrySettings)

客户端代码可以使用 WcfCommunicationClientFactory 以及用于实现 ServicePartitionClientWcfCommunicationClient 来确定服务终结点,并与服务通信。Client code can use the WcfCommunicationClientFactory along with the WcfCommunicationClient which implements ServicePartitionClient to determine the service endpoint and communicate with the service.

// Create binding
Binding binding = WcfUtility.CreateTcpClientBinding();
// Create a partition resolver
IServicePartitionResolver partitionResolver = ServicePartitionResolver.GetDefault();
// create a  WcfCommunicationClientFactory object.
var wcfClientFactory = new WcfCommunicationClientFactory<ICalculator>
    (clientBinding: binding, servicePartitionResolver: partitionResolver);

// Create a client for communicating with the ICalculator service that has been created with the
// Singleton partition scheme.
var calculatorServiceCommunicationClient =  new WcfCommunicationClient(

// Call the service to perform the operation.
var result = calculatorServiceCommunicationClient.InvokeWithRetryAsync(
                client => client.Channel.Add(2, 3)).Result;


默认 ServicePartitionResolver 假设客户端正在与服务相同的群集中运行。The default ServicePartitionResolver assumes that the client is running in same cluster as the service. 如果不是这样,请创建 ServicePartitionResolver 对象,并传入群集连接终结点。If that is not the case, create a ServicePartitionResolver object and pass in the cluster connection endpoints.

后续步骤Next steps