2012-08-14 2 views
0

나는 웹 앱을 가지고 있으며, 보고서를 작성합니다. mvc3의 웹 앱. 그리고 빌더는 WCF 서비스입니다.
스레드 풀을 사용하여 보고서를 자동으로 생성합니다.
기본 모델은 다음과 같습니다.
- 웹 앱이 보고서 생성시 요청을 보냅니다.
- WCF 서비스는 작업자 스레드를 생성하고 서비스가 허용 된 작업을 웹 응용 프로그램에 응답을 보냅니다.
- 웹 앱이 계속 작동합니다.WCF 작업자가 알림을 완료했습니다.

내가 필요한 것 : WCF 작업자 스레드가 작업을 마칠 때, 나는 작업이 완료 웹 애플리케이션에 통보해야합니다.

는 어떻게 내 웹 앱이 콜백을 잡을 수 있습니다.

이 로직을 구현하는 가장 간단한 방법을 제안 할 수 있습니다 (I은 3 MVC 사용)?

답변

0

흥미로운 문제. 요구 사항을 이해하면 보고서가 백그라운드에서 생성되는 동안 사용자가 비즈니스에 대해 계속할 수있게 한 다음 최종적으로 보고서가 AJAX를 통해 준비되었다는 알림을 사용자에게 보냅니다.

나는 올바른 방향으로 가고 있다고 생각합니다. WCF 비동기 콜백을 활용하는 것이 좋습니다. MSDN에서 좋은 시작 문서는 here입니다. 콜백 이벤트 처리기는 보고서 요청이 처음 WCF 작업으로 전송 될 때 만들어진 고유 한 캐시 키를 사용하여 보고서의 캐시 된 상태를 설정해야합니다.

클라이언트 쪽 알림은 AJAX에서 사용하는 동일한 고유 한 캐시 키를 사용하여 보고서의 상태를 주기적으로 확인하는 보고서 상태 폴링 메커니즘을 사용하여 수행 할 수 있습니다.

ReportServiceClient reportSvcClient = new ReportServiceClient(); 
Guid reportStatusKey = Guid.NewGuid(); 
reportSvcClient.GenerateReportCompleted += new EventHandler<GenerateReportCompletedEventArgs>(ReportStatusCallback); 
reportSvcClient.GenerateReportAsync(reportStatusKey, <other operation paramters>); 

// Set initial report status to False 
// (recommend setting an appropriate expiration period) 
Cache.Insert(reportStatusKey.ToString(), false); 

// WCF callback static method which sets the report status in cache 
static void ReportStatusCallback(object sender, GenerateReportCompletedEventArgs e) 
{ 
    Cache[e.ReportStatusKey.ToString()] = e.IsComplete; 
} 
... 
public partial class GenerateReportCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs 
{ 
    private object[] results; 

    public GenerateReportCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
     base(exception, cancelled, userState) 
    {  this.results = results;   } 

    public Guid ReportStatusKey 
    { 
     get   { 
      base.RaiseExceptionIfNecessary(); 
      return ((Guid)(this.results[0])); 
     } 
    } 
    public bool IsComplete 
    { 
     get { 
      base.RaiseExceptionIfNecessary(); 
      return ((bool)(this.results[1])); 
     } 
    } 
} 

가 클라이언트 측 AJAX 구현은 사용이 적절 믿는 어떤 주파수에서 보고서의 캐시 된 상태를 확인할 수 있습니다 :

비동기 WCF 콜백의 간단한 예는 다음과 같은 일을 할 수있는 동일한 ReportStatusKey.

0

왜 svcutil/async를 사용하여 wcf 서비스 용 비동기 프록시를 생성하지 않습니까? 그렇게하면 서비스 내에서 자신의 작업자 스레드가 필요 없으며 클라이언트는 콜백에 등록하기 만하면됩니다.

0

WCF에는 완료 콜백이 포함되어 있습니다. 여기서 클라이언트는 비동기 작업이 완료되면 WCF에 호출하도록 요청하는 메서드를 제공합니다.예를 들어

는 :

public class MyClient : IDisposable 
{ 
    BuilderClient _proxy = new BuilderClient(); 

    public void CallAsync(object input) 
    { 
     _proxy.BeginBuildReport(input, Oncompletion, null); 
    } 

    void OnCompletion(IAsyncResult result) 
    { 
     object output = _proxy.EndBuildReport(result); 
     //Do whatever you want with your output here. 
    } 

    public void Dispose() 
    { 
     _proxy.Close(); 
    } 
} 

이 간단한 이벤트 기반 모델을 사용 할 수 있습니다.

관련 문제