user3733963 user3733963 -4 years ago 170
C# Question

WCF Client Proxy BeginExecute callback not fired

I discovered that sometimes the callback of the BeginExecute method for an OData service (v2) doesn't fire. This situation occurs only if there are multiple parallel async request running.
Some demonstartion code:

public void Run()
{
Service = new SAP.TEST_SRV_Entities(new Uri(@"https://..."));
Service.Credentials = new NetworkCredential("user", "password");
Service.MergeOption = System.Data.Services.Client.MergeOption.NoTracking;

for (int i = 0; i < 6; i++)
{
AsyncCallback callback = new AsyncCallback(Response);
string url = @"https://...";
Service.BeginExecute<SAP.Folder>(new Uri(url), callback, null);
Console.WriteLine("Request");
//System.Threading.Thread.Sleep(1000);
}
}

void Response(IAsyncResult asyncResult)
{
Console.WriteLine("Response");
List<SAP.Folder> sapfolders = Service.EndExecute<SAP.Folder>(asyncResult).ToList();
}


The output of the program is:

Request
Request
Request
Request
Request
Request
Response
Response
Response
Response


6 requests started but only 4 ended.
I don't receive the missing two requests on my server. If I insert a sleep between the requests or they are called synchronous, every thing is fine and I get all responses.
It seems to me that the first requests are not correct finished. Increasing ServicePointManager.DefaultConnectionLimit results in more finished requests.
The code was called by an eventhandler of a wpf button, so the application didn't exit.

I get a similar error, if I miss to call close on a WebResponse of a http request. Can't find a "close" in WCF.

Any idea how to get all responses? Is there something missing like some close/finish call on the first responses?

Answer Source

Since I havn't found a solution, I will use a workaround. Instead of using the async methods, will use the synchronous in an own thread.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download