在 C# Web 服务中调用异步调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/246791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoking asynchronous call in a C# web service
提问by
I'm consuming a third-party resource (a .dll) in a web service, and my problem is, that invoking this resource (calling a method) is done asynchronous - I need to subscribe to an event, to get the answer for my request. How do I do that in a c# web service?
我正在使用 Web 服务中的第三方资源(.dll),我的问题是,调用此资源(调用方法)是异步完成的 - 我需要订阅一个事件,以获得答案我的请求。我如何在 ac# web 服务中做到这一点?
Update:
更新:
With regard to Sunny's answer:
关于Sunny 的回答:
I don't want to make my web service asynchronous.
我不想使我的 Web 服务异步。
采纳答案by csgero
If the 3rd party component does not support the standard asynchronous programming model (i.e it does not use IAsyncResult), you can still achieve synchronization using AutoResetEvent or ManualResetEvent. To do this, declare a field of type AutoResetEvent in your web service class:
如果第 3 方组件不支持标准的异步编程模型(即不使用 IAsyncResult),您仍然可以使用 AutoResetEvent 或 ManualResetEvent 来实现同步。为此,请在您的 Web 服务类中声明一个 AutoResetEvent 类型的字段:
AutoResetEvent processingCompleteEvent = new AutoResetEvent();
Then wait for the event to be signaled after calling the 3rd party component
然后在调用第 3 方组件后等待事件发出信号
// call 3rd party component
processingCompleteEvent.WaitOne()
And in the callback event handler signal the event to let the waiting thread continue execution:
并在回调事件处理程序中发出事件信号,让等待线程继续执行:
processingCompleteEvent.Set()
回答by Sunny Milenov
From MSDN docs:
来自MSDN 文档:
using System;
using System.Web.Services;
[WebService(Namespace="http://www.contoso.com/")]
public class MyService : WebService {
public RemoteService remoteService;
public MyService() {
// Create a new instance of proxy class for
// the XML Web service to be called.
remoteService = new RemoteService();
}
// Define the Begin method.
[WebMethod]
public IAsyncResult BeginGetAuthorRoyalties(String Author,
AsyncCallback callback, object asyncState) {
// Begin asynchronous communictation with a different XML Web
// service.
return remoteService.BeginReturnedStronglyTypedDS(Author,
callback,asyncState);
}
// Define the End method.
[WebMethod]
public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
asyncResult) {
// Return the asynchronous result from the other XML Web service.
return remoteService.EndReturnedStronglyTypedDS(asyncResult);
}
}
回答by Dan Finucane
Assuming your 3rd party component is using the asynchronous programming model pattern used throughout the .NET Framework you could do something like this
假设您的第 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();
}
Since you need your web service operation to block you should use the IAsyncResult.AsyncWaitHandle instead of the callback to block until the operation is complete.
由于您需要阻止 Web 服务操作,因此您应该使用 IAsyncResult.AsyncWaitHandle 而不是回调来阻止,直到操作完成。