How to get a response from a server in reliable WCF messaging after reducing connections

So, I created Client / Server WCF. I want when I send a message to the server from this client and for some reason the connection is disconnected, the client is turned off, for example, how can this client get a response when it will be available again?

Is it possible to establish a session or something like this between a client and a server?

My client code:

private static void Main(string[] args) { var client = new FlipCaseServiceClient("ReliableMessageService"); var sd = new StringData { FirstName = "Turgut", LastName = "Kançeltik" }; var fullName = client.GetFullName(ref sd); Console.WriteLine(fullName); } 

My server code:

 [DeliveryRequirements(RequireOrderedDelivery = true)] [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)] public class FlipCaseService : IFlipCaseService { public string GetFullName(ref StringData stringData) { var fullName = $"{stringData.FirstName} {stringData.LastName}"; stringData.FullName = fullName; return fullName; } } 

And the server configuration as a whole:

 <service behaviorConfiguration="ServiceBehaviorMetaData" name="FlipCaseService.FlipCaseService" > <endpoint name="ReliableMessageService" address="flipcase/wsAddress" binding="wsHttpBinding" bindingConfiguration="BindingReliableMessaging" contract="FlipCaseService.IFlipCaseService" > <identity> <dns value="localhost" /> </identity> </endpoint> </service> <bindings> <wsHttpBinding> <binding name="BindingReliableMessaging"> <reliableSession enabled="true" inactivityTimeout="00:10:00"/> </binding> </wsHttpBinding> </bindings> <behavior name="ServiceBehaviorMetaData"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8080/flipcase/metadata" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> 
+5
source share
1 answer

One reasonable approach here is to use an asynchronous request-response. This means that the client does not wait for the server to finish working, just launches the request and forgets. When the server is completed, it calls the client back with the results of the operation. In particular, WCF has duplex contracts to achieve this: http://www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF .

When the server response is ready, it tries to deliver it to the client. If this fails, the server may try again later until success or some timeout is reached.

If you follow this pattern, the client must have a unique identifier, so even if the connection is restored, the server knows that this is the same client, and it knows what responses this client is waiting for.

Another approach would be to cache the results on the server for some (limited) time. You can provide a unique identifier for each request, then check on the server whether the request with that identifier has already been completed, and if so, immediately deliver the results. Otherwise, the response of the process and caching it for a limited time, in the event that the client will try again later. On the client - just try again.

+2
source

Source: https://habr.com/ru/post/1258929/


All Articles