如何在WCF中同步使用HttpClient [英] How to use HttpClient in WCF syncronously

查看:136
本文介绍了如何在WCF中同步使用HttpClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的代理wcf服务发布Multipartformdatacontent,但是当我使用来自HttpClient的PostAsync方法时,它的行为有所不同。 WCF没有等待结果返回,而是由于对象引用错误而立即执行下一行代码并失败。相同的解决方案在控制台应用程任何原因 ?我错过了WCF中的任何线程模型





  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;





一旦我执行PostAsync,它会在下一行中抛出空引用异常。相同的代码在控制台应用程序中工作

解决方案

这是异步操作!你不应该假设结果立即就绪。一段时间 response.Result 将保持为null,之后它可能会转向实际结果。



因此,结论是:在这两种情况下,在仅限控制台的应用程序和WCF应用程序中,这段代码是错误的,非常错误。它在控制台应用程序中运行的事实更糟糕:看起来唯一的区别是计时。你创造了一件坏事,对执行顺序的不正确依赖,通常称为竞争条件 http://en.wikipedia.org/wiki/Race_condition [ ^ ]。



异步操作结果的准备情况以及对此结果的检查再次相互竞争;如果结果准备好了,它只创造了成功的幻觉



对于异步操作使用,请参阅:

http:// msdn.microsoft.com/en-us/library/system.net.http.httpclient.postasync%28v=vs.118%29.aspx [ ^ ],
你使用这种形式
http:// msdn .microsoft.com / zh-CN / library / hh138190%28v = vs.118%29.aspx [ ^ ]。



正确使用异步结果,请参阅,例如,此代码示例: http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a -net-client [ ^ ]。



现在,对于服务运营(以及许多其他情况),我通常建议将同步(阻塞)API与显式创建的单独线程结合使用。请参阅我的论点:

多线程问题? !!! [ ^ ],

TcpListener,TcpClient,在Visual Basic 2005中Ping [ ^ ],

TCP套接字 - 发送和接收问题 [ ^ ],

异步等待多个Telnet服务器 [ ^ ]。



-SA

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


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

解决方案

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


这篇关于如何在WCF中同步使用HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆