简体   繁体   English

处理来自异步WCF Web服务调用的异常

[英]Handling Exceptions from an Async WCF Web Service call

I have a third-party web-service reference which has a web method AddDocument . 我有一个第三方Web服务参考,其中包含一个Web方法AddDocument Our application called this method synchronously before but I need to implement asynchronous call to this method now. 我们的应用程序之前已经同步调用了此方法,但是现在我需要实现对此方法的异步调用。

I have reconfigured web-service reference in my project by checking "Generate Asynchronous Operations" checkbox. 通过选中“生成异步操作”复选框,我已经在项目中重新配置了Web服务参考。 Now I have a BeginAddDocument method available, which I am calling as: 现在,我有一个BeginAddDocument方法可用,我将其称为:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = "TestAsyncState";
srv.BeginAddDocument(Credentials, document, callback, asyncState);

And I have prepared a stab for a callback method: 我为回调方法准备了一个刺:

public void ProcessAsyncResult(IAsyncResult result)
{
}

Sometimes the web-service returns errors. 有时,Web服务返回错误。 They can be easily catched and handled while calling method synchronously. 在同步调用方法时,可以轻松捕获和处理它们。

But unfortunately I cannot find any clear answers how to catch exceptions using asynchronous approach, keeping in mind that I cannot modify the web-service itself. 但是不幸的是,我牢记我无法修改Web服务本身,因此找不到如何使用异步方法捕获异常的明确答案。

IAsyncResult in the callback method seems to be the same regardless of the operation result - be it a success or an exception. 不管操作结果如何,回调方法中的IAsyncResult似乎都是相同的-是成功还是异常。

Is it possible at all to catch exceptions from a web service using asynchronous approach, and if it is, what is the best way to make it possible? 是否可以使用异步方法从Web服务中捕获异常,如果可以,则使它成为可能的最佳方法是什么?

Usually with the "BeginYYYxxx" ASync pattern, there is a matching "EndYYYxxx" method that you call. 通常使用“ BeginYYYxxx” ASync模式,会调用一个匹配的“ EndYYYxxx”方法。 Assuming they've written the third-party service using this pattern, you should be able to just get the resulting object from the AsyncState of IAsyncResult. 假设他们已经使用此模式编写了第三方服务,则您应该能够从IAsyncResult的AsyncState获取结果对象。 The documentation for IAsyncResult may be of use to you. IAsyncResult文档可能对您有用。

Without seeing the documentation for the service, this is my best guess: 在没有看到该服务的文档的情况下,这是我的最佳猜测:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = "TestAsyncState";
srv.BeginAddDocument(Credentials, document, callback, asyncState);


public void ProcessAsyncResult(IAsyncResult result)
{
    TypeThatsrvIs s = (TypeThatsrvIs)result.AsyncState;

    //"s" will be the same srv object that you called BeginAddDocument on.
    //It "should" contain the web-service result.
    s = s.EndAddDocument(result);

    /*
    Do stuff with that resulting object.
    */

}

Hope that helps. 希望能有所帮助。

Edit: You may actually have to pass the srv object as the AsyncState in order to get it back in the ProcessAsyncResult method: 编辑:实际上,您可能必须将srv对象作为AsyncState传递,以便在ProcessAsyncResult方法中将其取回:

AsyncCallback callback = ProcessAsyncResult;
object asyncState = srv;
srv.BeginAddDocument(Credentials, document, callback, asyncState);

As Brandon suggested in his reply, the solution was really simple, and was not clear for me only due to my lack of experience in asynchronous web method call pattern. 正如布兰登在他的答复中所建议的那样,该解决方案确实非常简单,并且由于我缺乏异步Web方法调用模式方面的经验,因此对我来说还不清楚。 The resulting code of the callback method is as follows: 回调方法的结果代码如下:

public void ProcessAsyncResult(IAsyncResult result)
    {
        ServiceSoapClient srv = result.AsyncState as ServiceSoapClient;
        if (srv == null) return;
        try
        {
            srv.EndAddDocument(result);
        }
        catch (WebException ex)
        {
            //your code to handle exception here
        }
    }

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

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