C# 웹 서비스에서 비동기 호출을 호출합니다
-
05-07-2019 - |
문제
웹 서비스에 타사 리소스 (.dll)를 소비하고 있으며, 내 문제는이 리소스를 호출하는 (메소드 호출)가 비동기식이라는 것입니다. 내 요청. AC# 웹 서비스에서 어떻게해야합니까?
업데이트:
관련하여 Sunny의 대답:
웹 서비스를 비동기로 만들고 싶지 않습니다.
해결책
제 3 자 구성 요소가 표준 비동기 프로그래밍 모델을 지원하지 않는 경우 (즉, IASyncresult를 사용하지 않음) Autoresetevent 또는 ManualResetevent를 사용하여 동기화를 달성 할 수 있습니다. 이렇게하려면 웹 서비스 클래스에서 Autoresetevent 유형 필드를 선언합니다.
AutoResetEvent processingCompleteEvent = new AutoResetEvent();
그런 다음 타사 구성 요소에 전화 한 후 이벤트가 신호를받을 때까지 기다립니다.
// call 3rd party component
processingCompleteEvent.WaitOne()
콜백 이벤트 핸들러에서 대기 스레드가 계속 실행되도록 이벤트를 신호합니다.
processingCompleteEvent.Set()
다른 팁
제 3 자 구성 요소가 .NET 프레임 워크 전반에 사용되는 비동기 프로그래밍 모델 패턴을 사용한다고 가정하면 이와 같은 작업을 수행 할 수 있습니다.
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();
}
차단하려면 웹 서비스 작업이 필요하기 때문에 콜백 대신 iAsyncresult.asyncwaithandle을 사용하여 작업이 완료 될 때까지 차단해야합니다.
에서 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);
}
}
제휴하지 않습니다 StackOverflow