Changing the endpoint of a service reference at runtime, sometimes using the wrong endpoint

I have a WCF service, which in turn has a service reference for some other SOAP endpoint. This endpoint has an identical copy at a different address with the same metadata but with different data. When a request arrives at my service, it indicates which of the two identical endpoints that I consume, it wants to receive data. So, I have something like this:

using (var client = new ServiceClient()) { client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url); //do some work, pull some data, bake some muffins } 

This sometimes does not work when I have two requests that are very close to each other with different URLs. The second request ends with an exit to the same endpoint as the first. I understand that as soon as the channel is open, I cannot change the endpoint, but I thought that the client would be used only once, and then it would be deleted. Is there any kind of optimization when the same proxy is reused for multiple requests? What is a good approach to such problems?

EDIT: More details:

The snippet is part of the method (let it call foo ()) that was open to users of my RESTful service. This is not static. The endpoint URL is a local string that is provided as part of the URI.

 [OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "getStuff?url={url}")] string foo(string url); public string foo(string url) { using (var client = new ServiceClient()) { client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url); //do some work, pull some data, bake some muffins return "SO rocks!"; } } 

I have not configured anything explicitly for the concurrency control. I understand the theory underlying it, but I have no technical experience, so any manual work will be greatly appreciated.

Also! There was a semi-probable reason to change the endpoint address, rather than specifying it in the proxy constructor. However, for testing purposes, I just tried to specify in the constructor and it seems to work fine (thanks to JMeter!). With a little work, I could rebuild the real code to work this way, but I would still be very grateful for the explanation. Training is important.

+6
source share
1 answer

If you look at the source of ClientBase`1, you will notice that the endpoint is actually retrieved from the underlying ChannelFactory:

 public ServiceEndpoint Endpoint { get { TryDisableSharing(); return GetChannelFactory().Endpoint; } } 

by default, CacheSetting from ClientBase`1 uses CacheSetting.Default, which means it is trying to cache channel factories, so it seems like sometimes you had to modify the factory cache endpoint for two different clients

0
source

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


All Articles