Udostępnij za pośrednictwem


Stos komunikacji oparty na programie WCF dla usług Reliable Services

Platforma Reliable Services umożliwia autorom usług wybór stosu komunikacji, którego chcą użyć dla swojej usługi. Mogą podłączyć wybrany stos komunikacji za pośrednictwem elementu ICommunicationListener zwróconego z metod CreateServiceReplicaListeners lub CreateServiceInstanceListeners . Struktura zapewnia implementację stosu komunikacyjnego opartego na programie Windows Communication Foundation (WCF) dla autorów usług, którzy chcą korzystać z komunikacji opartej na programie WCF.

Odbiornik komunikacji WCF

Implementacja elementu ICommunicationListener specyficzna dla programu WCF jest dostarczana przez klasę Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener .

Nie załóżmy, że mamy umowę serwisową typu ICalculator

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

W usłudze można utworzyć odbiornik komunikacji WCF w następujący sposób.


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()
        )
    )};
}

Pisanie klientów dla stosu komunikacji WCF

Aby zapisywać klientów do komunikowania się z usługami przy użyciu programu WCF, platforma udostępnia element WcfClientCommunicationFactory, który jest implementacją specyficzną dla programu WCF clientCommunicationFactoryBase.


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

Dostęp do kanału komunikacyjnego programu WCF można uzyskać z poziomu obiektu WcfCommunicationClient utworzonego przez element 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)
       {
       }
   }

Kod klienta może używać elementu WcfCommunicationClientFactory wraz z elementem WcfCommunicationClient , który implementuje element ServicePartitionClient w celu określenia punktu końcowego usługi i komunikacji z usługą.

// 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;

Uwaga

Domyślny element ServicePartitionResolver zakłada, że klient jest uruchomiony w tym samym klastrze co usługa. Jeśli tak nie jest, utwórz obiekt ServicePartitionResolver i przekaż punkty końcowe połączenia klastra.

Następne kroki