[英]How to catch Exception from remote WCF service?
从Visual Studio UI添加引用后,我初始化了客户端:
private WcfRequestProcessorClient _client;
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
string serviceFullUrl = "http://myserviceurl.com/svcapi";
var address = new EndpointAddress(serviceFullUrl);
_client = new WcfRequestProcessorClient(binding, address);
_client.ClientCredentials.UserName.UserName = "MyUserName";
_client.ClientCredentials.UserName.Password = "MyPassword";
然后,我将请求作为参数调用相关方法:
try
{
var itemsReq = LoadItemsRequest();
_client.ProcessRequestsAsync(itemsReq);
}
catch(Exception)
{
// source code for show an error in a popup
}
现在,当URL,用户名和密码正确时 , 一切正常 。 但是如果 URL,用户名或密码错误 , 应用程序会中断 ,Visual Studio会在调试时显示此窗口, 跳过我的catch块 :
处理这些类型的异常的方法是什么?
需要配置服务以将真实异常传播给您。
即使你不知道你所陈述的实际原因,它也会表明如果抛出异常。 '被抛出的例外'
未捕获异常的原因是该调用是异步的。
即异常被抛出在另一个堆栈上,然后是你调用它的那个。
如果它返回一个任务,你需要等待它。
如果它是APM风格的异步性,你必须提供一个回调并尝试在那里捕获它。
此外,您可以注册到AppDomain.UnhandledException以最终捕获此异常。
catch(Exception e)
{
MessageBox.Show(e.Message.ToString());
}
希望这可以帮助。 对于Advance异常抛出,请参阅WCF中的FaultException。 它专为面向服务的体系结构而设计。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.