c# - 在 C# Web 服务中调用异步调用

标签 c# web-services asynchronous function-calls

我正在 Web 服务中使用第三方资源(.dll),而我的问题是,调用该资源(调用方法)是异步完成的 - 我需要订阅一个事件,以获得我的请求的答案。如何在 C# Web 服务中执行此操作?

更新:

关于Sunny's answer :

我不想让我的 Web 服务异步。

最佳答案

假设您的第 3 方组件正在使用整个 .NET Framework 中使用的异步编程模型模式,您可以执行类似的操作

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.stackoverflow.com");
    IAsyncResult asyncResult = httpWebRequest.BeginGetResponse(null, null);

    asyncResult.AsyncWaitHandle.WaitOne();

    using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(asyncResult))
    using (StreamReader responseStreamReader = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        string responseText = responseStreamReader.ReadToEnd();
    }

由于您需要阻止 Web 服务操作,因此应使用 IAsyncResult.AsyncWaitHandle 而不是回调来阻止,直到操作完成。

关于c# - 在 C# Web 服务中调用异步调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/246791/

相关文章:

javascript - RxJS:调用两个异步调用,返回第一个通过条件的调用,或者它们都没有的特殊值

c# - 使用 Async 和 Await 中断数据库调用(使用 Dapper)

c# - Visual Studio 2017 Xamarin Android 编译警告 "fakeLogOpen(/dev/log_security) failed"中断构建

c# - “使用”语句与 'try finally'

Java Web 服务 - 列表与数组

c# - 在 C# 控制台应用程序中处理 HTTP 请求

.net - 如何在 ASP.NET Web API 中指定我想要 JSON 还是 XML?

c# - 为什么 stackalloc 不能与引用类型一起使用?

c# - 最佳实践 : Converting an application to a service with a schedule

python - 向 subprocess.Popen() 进程发送命令