Sdílet prostřednictvím


Komunikační zásobník založený na WCF pro Reliable Services

Architektura Reliable Services umožňuje autorům služeb zvolit komunikační zásobník, který chtějí použít pro svou službu. Mohou připojit komunikační zásobník podle svého výběru prostřednictvím ICommunicationListener vrácené z CreateServiceReplicaListeners nebo CreateServiceInstanceListeners metody. Architektura poskytuje implementaci komunikačního zásobníku založeného na technologii Windows Communication Foundation (WCF) pro autory služeb, kteří chtějí používat komunikaci založenou na WCF.

Naslouchací proces komunikace WCF

Implementace ICommunicationListener specifické pro WCF je poskytována Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener třída.

Neříkejme, že máme servisní zakázku typu ICalculator

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

Naslouchací proces komunikace WCF můžeme ve službě vytvořit následujícím způsobem.


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

Psaní klientů pro komunikační zásobník WCF

Pro zápis klientů ke komunikaci se službami pomocí WCF poskytuje architektura WcfClientCommunicationFactory, což je implementace ClientCommunicationFactoryBase specifická pro WCF.


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

Komunikační kanál WCF je přístupný z WcfCommunicationClient vytvořené 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)
       {
       }
   }

Kód klienta může použít WcfCommunicationClientFactory spolu s WcfCommunicationClient , který implementuje ServicePartitionClient k určení koncového bodu služby a komunikaci se službou.

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

Poznámka:

Výchozí ServicePartitionResolver předpokládá, že klient běží ve stejném clusteru jako služba. Pokud tomu tak není, vytvořte objekt ServicePartitionResolver a předejte koncové body připojení clusteru.

Další kroky