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í pro svoji službu použít. Mohou připojit komunikační zásobník podle svého výběru prostřednictvím ICommunicationListener vrácené z Metody CreateServiceReplicaListeners nebo CreateServiceInstanceListeners . 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

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

Neříkejte, že máme servisní smlouvu 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 psaní klientů komunikovat se službami pomocí WCF, architektura poskytuje WcfClientCommunicationFactory, což je WCF specifické implementace ClientCommunicationFactoryBase.


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

Komunikační kanál WCF lze získat přístup 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)
       {
       }
   }

Klientský kód 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í ke clusteru.

Další kroky