WCF-alapú kommunikációs verem a Reliable Serviceshez
A Reliable Services keretrendszerrel a szolgáltatásszerzők kiválaszthatják a szolgáltatásukhoz használni kívánt kommunikációs vermet. Az általuk választott kommunikációs vermet a CreateServiceReplicaListeners vagy a CreateServiceInstanceListeners metódusból visszaadottICommunicationListener használatával csatlakoztathatják. A keretrendszer a Windows Communication Foundation (WCF) alapján biztosítja a kommunikációs verem implementálását azon szolgáltatásszerzők számára, akik WCF-alapú kommunikációt szeretnének használni.
WCF kommunikációs figyelő
Az ICommunicationListener WCF-specifikus implementációját a Microsoft.ServiceFabric.Services.Communication.Wcf.Runtime.WcfCommunicationListener osztály biztosítja.
Tegyük fel, hogy van egy típusú szolgáltatási szerződésünk. ICalculator
[ServiceContract]
public interface ICalculator
{
[OperationContract]
Task<int> Add(int value1, int value2);
}
A szolgáltatásban a következő módon hozhatunk létre WCF kommunikációs figyelőt.
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()
)
)};
}
Ügyfelek írása a WCF kommunikációs veremhez
Ahhoz, hogy az ügyfelek a WCF használatával kommunikáljanak a szolgáltatásokkal, a keretrendszer a WcfClientCommunicationFactory szolgáltatást biztosítja, amely a ClientCommunicationFactoryBase WCF-specifikus implementációja.
public WcfCommunicationClientFactory(
Binding clientBinding = null,
IEnumerable<IExceptionHandler> exceptionHandlers = null,
IServicePartitionResolver servicePartitionResolver = null,
string traceId = null,
object callback = null);
A WCF kommunikációs csatorna a WcfCommunicationClient által létrehozott WcfCommunicationClientből érhető el.
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)
{
}
}
Az ügyfélkód használhatja a WcfCommunicationClientFactory és a WcfCommunicationClient együttes használatát, amely a ServicePartitionClientet implementálja a szolgáltatásvégpont meghatározásához és a szolgáltatással való kommunikációhoz.
// 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;
Megjegyzés
Az alapértelmezett ServicePartitionResolver feltételezi, hogy az ügyfél ugyanabban a fürtben fut, mint a szolgáltatás. Ha nem ez a helyzet, hozzon létre egy ServicePartitionResolver objektumot, és adja át a fürt kapcsolati végpontjait.
Következő lépések
Visszajelzés
https://aka.ms/ContentUserFeedback.
Hamarosan elérhető: 2024-ben fokozatosan kivezetjük a GitHub-problémákat a tartalom visszajelzési mechanizmusaként, és lecseréljük egy új visszajelzési rendszerre. További információ:Visszajelzés küldése és megtekintése a következőhöz: