WCF-baserad kommunikationsstack för Reliable Services

Reliable Services-ramverket gör det möjligt för tjänstförfattare att välja den kommunikationsstack som de vill använda för sin tjänst. De kan ansluta den kommunikationsstack som de väljer via ICommunicationListener som returneras från metoderna CreateServiceReplicaListeners eller CreateServiceInstanceListeners . Ramverket tillhandahåller en implementering av kommunikationsstacken baserat på Windows Communication Foundation (WCF) för tjänstförfattare som vill använda WCF-baserad kommunikation.

WCF-kommunikationslyssnare

Den WCF-specifika implementeringen av ICommunicationListener tillhandahålls av klassen Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener .

Säg inte att vi har ett servicekontrakt av typen ICalculator

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

Vi kan skapa en WCF-kommunikationslyssnare i tjänsten på följande sätt.


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

Skriva klienter för WCF-kommunikationsstacken

För att skriva klienter för att kommunicera med tjänster med hjälp av WCF tillhandahåller ramverket WcfClientCommunicationFactory, som är den WCF-specifika implementeringen av ClientCommunicationFactoryBase.


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

WCF-kommunikationskanalen kan nås från WcfCommunicationClient som skapats av 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)
       {
       }
   }

Klientkoden kan använda WcfCommunicationClientFactory tillsammans med WcfCommunicationClient som implementerar ServicePartitionClient för att fastställa tjänstslutpunkten och kommunicera med tjänsten.

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

Anteckning

StandardtjänstenPartitionResolver förutsätter att klienten körs i samma kluster som tjänsten. Om så inte är fallet skapar du ett ServicePartitionResolver-objekt och skickar in klusteranslutningsslutpunkterna.

Nästa steg