Stack di comunicazione basato su WCF per Reliable ServicesWCF-based communication stack for Reliable Services

Il framework di Reliable Services consente agli autori del servizio di scegliere lo stack di comunicazione da usare per il servizio,The Reliable Services framework allows service authors to choose the communication stack that they want to use for their service. nonché di collegarsi allo stack di comunicazione scelto tramite l'oggetto ICommunicationListener restituito dai metodi CreateServiceReplicaListeners o CreateServiceInstanceListeners.They can plug in the communication stack of their choice via the ICommunicationListener returned from the CreateServiceReplicaListeners or CreateServiceInstanceListeners methods. Il framework offre un'implementazione dello stack di comunicazione basata su Windows Communication Foundation (WCF) per gli autori del servizio che intendono usare la comunicazione basata su 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.

Listener di comunicazione WCFWCF Communication Listener

L'implementazione specifica di WCF dell'oggetto ICommunicationListener viene fornita dalla classe Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener.The WCF-specific implementation of ICommunicationListener is provided by the Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener class.

Si supponga di avere un contratto di servizio di tipo ICalculatorLest say we have a service contract of type ICalculator

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

È possibile creare un listener di comunicazione WCF nel servizio come descritto di seguito.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()
        )
    )};
}

Scrittura di client per lo stack di comunicazione WCFWriting clients for the WCF communication stack

Per consentire ai client di scrittura di comunicare con i servizi usando WCF, il framework fornisce l'oggetto WcfClientCommunicationFactory, ovvero l'implementazione specifica di WCF di 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);

Il canale di comunicazione WCF è accessibile dall'oggetto WcfCommunicationClient creato da 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)
       {
       }
   }

Il codice client può usare WcfCommunicationClientFactory insieme a WcfCommunicationClient che implementa ServicePartitionClient per determinare l'endpoint del servizio e comunicare con esso.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

L'oggetto ServicePartitionResolver predefinito presuppone che il client sia in esecuzione nello stesso cluster del servizio.The default ServicePartitionResolver assumes that the client is running in same cluster as the service. In caso contrario, creare un oggetto ServicePartitionResolver e passare gli endpoint di connessione del cluster.If that is not the case, create a ServicePartitionResolver object and pass in the cluster connection endpoints.

Passaggi successiviNext steps