【问题标题】:How to call Soap/mtom web service asynchronously from C# winform如何从 C# winform 异步调用 Soap/mtom Web 服务
【发布时间】:2014-05-30 11:48:24
【问题描述】:

我正在尝试调用/推送一个半大的 tiff 和一个 Gal 文件到 java web 服务。 平台为 Visual Studio 2013,C# windows 窗体应用程序。

我指向 WSDL 文件,“平台”正在为我生成一个服务引用类。 这对我来说都是非常抽象的,这是一件好事,因为我是这个领域的新手。 我勾选了“生成基于任务的代码”,我得到了一个 addSample 和 addSampleAsync 方法。

我填充类字段并将代码向上推送。

addSample 代码运行良好,但会阻塞 UI。

异步代码 addSampleAsync 也可以工作,但速度较慢且不是完全异步的。 addSampleAsync 将 UI 锁定大约一半的处理时间,并且在同一时间段内对 fncTestUpload 的函数调用不会返回。

    //Dimensioned at class level
    //private static addSamplePortClient Service = new addSamplePortClient();
    //private static addSampleResponse Myresult = new addSampleResponse(); 

    //ThisRequest is the WSDL modeled class object.

    //This code works, but is slow, 30 seconds on wifi
    ResponseType Myresult = Service.addSample(ThisRequest.Request);
    MessageBox.Show(Myresult.Message + Myresult.Code);


    //This code locks up the UI for about 15 - 20 seconds then takes another 15 to display the messagebox
    fncTestUpload(ThisRequest);       

    async void fncTestUpload(addSampleRequest SentRequest)
        {
            Myresult = await Service.addSampleAsync(SentRequest.Request);
            MessageBox.Show(Myresult.Response.Message + " - " + Myresult.Response.Code);
        }

我将响应对象设为类级变量,希望在调用 fncTestUpload 的函数中对其进行处理,它认为在调用 Async 函数时会立即返回。它直到 15 秒后才返回。??

我已经用谷歌搜索了几个小时,但没有找到任何关于为什么 addSampleAsync 没有像宣传的那样工作的答案。

Microsoft 的教程也可以用 Dilbert 的 Elbonian 语言编写。我无法关注它们,也觉得它们没有帮助,所以请不要引导我。

【问题讨论】:

标签: c# web-services soap async-await mtom


【解决方案1】:

当您在方法中使用 'await' 关键字时,您是在说“好的,您继续工作,我会返回给我的调用者,完成后告诉我” .

所以 15 秒的等待时间是你的服务处理请求,然后在之前等待的方法完成后调用异步方法生成的状态机返回到该方法的时间。这是 await 的正常行为。

关于需要 15 秒的 MessageBox,可能是 Response 属性正在延迟加载,并且实际上是在您第一次访问这些属性时尝试加载代码/消息。

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 2023-03-15
    • 2011-09-03
    相关资源
    最近更新 更多