简体   繁体   English

如何从异步WCF调用中捕获FaultException?

[英]How to catch FaultExceptions from asynchronous WCF calls?

I would like to call a wcf service that can throw FaultException, but I would like to do that asynchronously. 我想调用一个可以抛出FaultException的wcf服务,但是我想异步地做到这一点。 If everything's ok, it returns no exception, but if the service throws one of my FaultExceptions, in the client I get a CommunicationObjectFaultedException and none of its properties contain my original FaultException. 如果一切正常,则不返回任何异常,但是如果服务抛出我的FaultException之一,则在客户端中,我会收到CommunicationObjectFaultedException,并且其任何属性都不包含原始的FaultException。

From what I could learn so far about this, is that information is stored elsewhere . 从到目前为止我可以了解到的是,信息存储在其他地方 Can anybody please tell me where exactly it is? 有人能告诉我哪儿它是什么?

For example these two handle the registration of a user: 例如,以下两个处理用户的注册:

internal void CallRegisterUser()
{
    _service.RegisterUserAsync("username", "pass");
}

void _service_RegisterUserCompleted(object sender, RegisterUserCompletedEventArgs e)
{
    if (e.Error != null) { MessageBox.Show(e.Error.Message); }
}

An oversimplified example, but this is how you would get your custom fault details: 一个过于简化的示例,但这是您如何获取自定义故障详细信息的方法:

    void client_RegisterUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            if (e.Error is FaultException<UserRegistrationFault>)
            {
                UserRegistrationFault fault = (e.Error as FaultException<UserRegistrationFault>).Detail;
                MessageBox.Show("Error: " + fault.TheExceptionMessage);
            }
            else
            {
                MessageBox.Show("Error: " + e.Error.ToString());
            }
        }            
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM