Bagikan melalui


Tumpukan komunikasi berbasis WCF untuk Reliable Services

Kerangka kerja Reliable Services memungkinkan penulis layanan untuk memilih tumpukan komunikasi yang ingin mereka gunakan untuk layanan mereka. Mereka dapat memasang tumpukan komunikasi pilihan mereka melalui ICommunicationListener yang dikembalikan dari metode CreateServiceReplicaListeners atau CreateServiceInstanceListeners. Kerangka kerja ini menyediakan implementasi tumpukan komunikasi berdasarkan Windows Communication Foundation (WCF) untuk penulis layanan yang ingin menggunakan komunikasi berbasis WCF.

WCF Communication Listener

Implementasi khusus WCF dari ICommunicationListener disediakan oleh kelas Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener.

Katakanlah kita memiliki jenis kontrak layanan ICalculator

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

Kita dapat membuat WCF communication listener dalam layanan dengan cara berikut.


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

Menulis klien untuk tumpukan komunikasi WCF

Untuk menulis klien untuk berkomunikasi dengan layanan dengan menggunakan WCF, kerangka kerja menyediakan WcfClientCommunicationFactory, yang merupakan implementasi khusus WCF dari ClientCommunicationFactoryBase.


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

Saluran komunikasi WCF dapat diakses dari WcfCommunicationClient yang dibuat oleh 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)
       {
       }
   }

Kode klien dapat menggunakan WcfCommunicationClientFactory bersama dengan WcfCommunicationClient yang mengimplementasikan ServicePartitionClient untuk menentukan titik akhir layanan dan berkomunikasi dengan layanan.

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

Catatan

ServicePartitionResolver default mengasumsikan bahwa klien berjalan dalam klaster yang sama dengan layanan. Jika bukan itu masalahnya, buat objek ServicePartitionResolver dan lewati di titik akhir koneksi klaster.

Langkah berikutnya