Abh*_*yak 8 c# wcf asynchronous web-services async-await
我在我的项目中使用WCF异步调用,我使用客户端异步方法.我有一个如下情况 -
//Code in Business Layer and this method is called from Web layer
private void GetGeneralNews()
{
client.BeginGetGeneralNewsFeed(GeneralNewsCallback, null);
}
//Call Back Method
private static void GeneralNewsCallback(IAsyncResult asyncResult)
{
string response = string.Empty;
try
{
response = client.EndGetGeneralNewsFeed(asyncResult);
}
catch(Exception ex)
{
throw ex; // Here is the problem. It does not throw the exception to the web layer instead it will suppress the error.
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如上面的代码片段所示,它不会将业务层中的异常抛出到Web层,因为它将在业务层本身中被抑制.
我检查了他们建议去异步和等待方法的一些博客和网站,因为我有.NET 4.0框架,我看到" 生成基于任务的操作 "选项被禁用.因此,如果有任何选项使用"IAsyncResult"(客户端的Begin&End),请告诉我.如果有任何其他方法也欢迎.
请有人帮助我.
谢谢.
无论如何,我通过使用 TPL(任务并行库)解决了这个问题。
我在上述方法中遇到问题的原因是我的新线程将无法识别主线程,该方法是从哪一层调用的。因此,使用 TPL,我让主线程等待,直到其他线程完成工作并返回,然后根据响应抛出异常。
希望能帮助到你。