Pilha de comunicações baseadas no WCF de Reliable ServicesWCF-based communication stack for Reliable Services

A estrutura de Reliable Services permite que os autores de serviços escolher a pilha de comunicação que pretende utilizar o seu serviço.The Reliable Services framework allows service authors to choose the communication stack that they want to use for their service. Eles podem de plug-in a pilha de comunicações de sua preferência por meio do ICommunicationListener devolvido do CreateServiceReplicaListeners ou CreateServiceInstanceListeners métodos.They can plug in the communication stack of their choice via the ICommunicationListener returned from the CreateServiceReplicaListeners or CreateServiceInstanceListeners methods. O framework fornece uma implementação da pilha de comunicações com base no Windows Communication Foundation (WCF) para autores de serviço que pretendem utilizar a comunicação baseada em 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.

Serviço de escuta de comunicação de WCFWCF Communication Listener

A implementação de específicas do WCF de ICommunicationListener fornecido pela Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener classe.The WCF-specific implementation of ICommunicationListener is provided by the Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener class.

Que acabam sendo Digamos que exista um contrato de serviço do tipo ICalculatorLest say we have a service contract of type ICalculator

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    Task<int> Add(int value1, int value2);
}

Podemos criar um serviço de escuta de comunicação do WCF no serviço da seguinte forma.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>(
            wcfServiceObject:this,
            serviceContext:context,
            //
            // 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()
        )
    )};
}

Escrita de clientes para a pilha de comunicações de WCFWriting clients for the WCF communication stack

Para escrever os clientes comuniquem com os serviços usando o WCF, o framework fornece WcfClientCommunicationFactory, que é a implementação de específicas do WCF de 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);

O canal de comunicação do WCF pode ser acedido a partir da WcfCommunicationClient criado pela WcfCommunicationClientFactory.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)
       {
       }
   }

Código de cliente pode utilizar o WcfCommunicationClientFactory juntamente com o WcfCommunicationClient quais implementa ServicePartitionClient para determinar o ponto final de serviço e comunicar com o serviço.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(
                wcfClientFactory,
                ServiceUri,
                ServicePartitionKey.Singleton);

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

Nota

A predefinição ServicePartitionResolver parte do princípio de que o cliente está em execução no mesmo cluster que o serviço.The default ServicePartitionResolver assumes that the client is running in same cluster as the service. Se não for o caso, crie um objeto de ServicePartitionResolver e passar os pontos de extremidade de ligação do cluster.If that is not the case, create a ServicePartitionResolver object and pass in the cluster connection endpoints.

Passos SeguintesNext steps