مكدس الذاكرة المؤقتة لاتصالات قائمة على WCF للخدمات الموثوقة

يسمح إطار عمل الخدمات الموثوقة لمؤلفي الخدمة باختيار مكدس ذاكرة مؤقتة للاتصالات الذي يريدون استخدامه لخدمتهم. يمكنهم توصيل مكدس الذاكرة المؤقتة للاتصال الذي يختارونه عبر ICommunicationListener الذي تم إرجاعه من أساليب CreateServiceReplicaListeners or CreateServiceInstanceListeners. يوفر الإطار تنفيذ مكدس الذاكرة المؤقتة للاتصالات استناداً إلى Windows Communication Foundation (WCF) لمؤلفي الخدمة الذين يريدون استخدام الاتصالات القائمة على WCF.

وحدة استماع اتصالات WCF

يتم توفير التنفيذ الخاص بـ WCF على ICommunicationListener بواسطة فئة Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener.

لنفترض أن لدينا عقد خدمة من النوع ICalculator

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

يمكننا إنشاء وحدة استماع اتصالات WCF في الخدمة بالطريقة التالية.


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

كتابة العملاء لمكدس الذاكرة المؤقتة لاتصالات WCF

لكتابة العملاء للتواصل مع الخدمات باستخدام WCF، يوفر إطار العمل WcfClientCommunicationFactory، وهو التنفيذ الخاص بـ WCF على ClientCommunicationFactoryBase.


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

يمكن الوصول إلى قناة اتصال WCF من WcfCommunicationClient التي أنشأها 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)
       {
       }
   }

يمكن للتعليمة البرمجية للعميل استخدام WcfCommunicationClientFactory إلى جانب WcfCommunicationClient الذي ينفذ ServicePartitionClient لتحديد نقطة تقديم الخدمة والتواصل مع الخدمة.

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

إشعار

يفترض ServicePartitionResolver الافتراضي أن العميل قيد التشغيل في نفس نظام المجموعة مثل الخدمة. إذا لم يكن الأمر كذلك، قم بإنشاء عنصر ServicePartitionResolver وقم بتمريره في نقاط نهاية اتصال نظام المجموعة.

الخطوات التالية