Click here to Skip to main content
15,885,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to post Multipartformdatacontent from my proxy wcf service, but it behaves differently when I use PostAsync method from HttpClient. WCF is not waiting for result to come back, instead its executing next line of code immediately and failing due to object reference error. Same solution works in console application. Any reason ? am I missing any threading model in WCF


C#
var message = new HttpRequestMessage();
message.Method = HttpMethod.Post;
message.Content = content;
message.Headers.Add("VSAML", vSaml);
message.RequestUri = new Uri(cmsStoreUrl);
 
var client = new HttpClient() { Timeout = TimeSpan.FromMinutes(15) };
var response = client.PostAsync(cmsStoreUrl, content);
var res = response.Result.EnsureSuccessStatusCode();
var a = res.Content.ReadAsStringAsync();
cmsStoreResponse = a.Result;


As soon as I do PostAsync, it throws null reference exception in next line. same code works in console application
Posted
Updated 1-Aug-14 6:14am
v4
Comments
Sergey Alexandrovich Kryukov 31-Jul-14 14:02pm    
Probably not; it could be just some bug. To discuss it, you would need to create some complete but short code sample manifesting the problem. Please see: SSCCE.
—SA
sam3440 31-Jul-14 16:41pm    
Not sure if I need to report as bug, I did not find any one having same issue. So that makes me think that HttpClient should be used in different way in WCF ? any thought
Sergey Alexandrovich Kryukov 31-Jul-14 23:52pm    
What thought? Why would some class would behave differently? Anyway, you really need to show some code. So far, you did not provide any information useful for resolution...
—SA
sam3440 1-Aug-14 10:10am    
Updated code in question.


As soon as I do PostAsync, it fails in next line. same code works in console application
Sergey Alexandrovich Kryukov 1-Aug-14 11:59am    
First of all, please, code goes to the question, using "Improve question". Format if (in the tag <pre lang="cs">...).
What does it mean, fail? what line? full exception information, please.
—SA

1 solution

This is asynchronous operation! You should not assume that the result is ready immediately. For a while response.Result will remain null, and later it might turn to real result.

So, the conclusion is: in both cases, in your console-only application and WCF application, this fragment of code was wrong, really badly wrong. The fact it ran in console application is even worse: it looks like the only difference is timing. You created a bad thing, incorrect dependency on the order of execution, usually called race condition: http://en.wikipedia.org/wiki/Race_condition[^].

The readiness of the result of asynchronous operation and check up of this result raced again each other; if the result was ready sooner, it created only the illusion of success.

For asynchronous operation usage, please see:
http://msdn.microsoft.com/en-us/library/system.net.http.httpclient.postasync%28v=vs.118%29.aspx[^],
you used this form of it: http://msdn.microsoft.com/en-us/library/hh138190%28v=vs.118%29.aspx[^].

For correct use of asynchronous result, please see, for example, this code sample: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client[^].

Now, for service operation (and many other cases), I usually recommend using synchronous (blocking) API in combination with explicitly created separate threads. Please see my arguments:
problem in multithreading ? !!![^],
TcpListener, TcpClient, Ping in Visual Basic 2005[^],
TCP Socket - Send and receive question[^],
Async Await Multiple Telnet Servers[^].

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900