gpt4 book ai didi

c# - 如何从远程 WCF 服务捕获异常?

转载 作者:行者123 更新时间:2023-11-30 18:24:56 25 4
gpt4 key购买 nike

从 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 在调试时向我显示此窗口,跳过我的捕获 block :

enter image description here

处理此类异常的方法是什么?

最佳答案

服务需要配置为将真正的异常传播回给您。

即使您不知道您所说的实际原因,它也会说明是否抛出异常。 '抛出异常'

没有捕获异常的原因是调用是异步的。

即异常抛出在与您调用它的堆栈不同的堆栈上。

如果它返回一个任务,您需要等待它。

如果它是 APM 样式的异步性,您将必须提供回调并尝试在那里捕获它。

另外你可以注册到AppDomain.UnhandledException最终捕获此异常。

关于c# - 如何从远程 WCF 服务捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30641942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com