【问题标题】:WCF worker finished notifyWCF 工作人员完成通知
【发布时间】:2012-08-14 05:19:31
【问题描述】:

我有我的网络应用程序,它可以生成报告。 mvc3 上的 Web 应用程序。并且构建器是 WCF 服务。
我使用线程池使我的报告自动生成。
基本模型如下所示:
-- Web 应用程序发送报告生成请求。
-- WCF 服务创建工作线程并向服务接受工作的 Web 应用程序发送响应。
-- 网络应用继续工作。

我需要什么:当 WCF 工作线程完成工作时,我需要通知 Web 应用程序该工作已完成。

那么如何使用我的 Web 应用程序捕获该回调。(我使用 mvc 3)

你能推荐最简单的方法来实现这个逻辑吗?

【问题讨论】:

    标签: wcf methods


    【解决方案1】:

    有多种方法可以使用 Comet 或 Reverse-Ajax。之前已经在这里回答了这个问题: Is there some way to PUSH data from web server to browser?

    【讨论】:

      【解决方案2】:

      有趣的问题。如果我了解您的要求,您需要允许用户在后台生成报告时继续他们的业务,然后最终通过 AJAX 向用户发送报告已准备就绪的通知。

      我相信你正朝着正确的方向前进。我的建议是利用 WCF 异步回调。 here 上提供了一篇关于 MSDN 的不错的入门文章。回调事件处理程序应使用报表请求最初发送到 WCF 操作时创建的唯一缓存键来设置报表的缓存状态。

      可以使用报告状态轮询机制来实现客户端通知,该机制使用由 AJAX 启用的相同的唯一缓存键定期检查报告的状态。

      异步 ​​WCF 回调的一个简单示例可以执行以下操作:

      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 实现可以使用相同的 ReportStatusKey 以您认为合适的任何频率检查报告的缓存状态。

      【讨论】:

        【解决方案3】:

        为什么不使用 svcutil /async 为您的 wcf 服务生成异步代理。这样一来,您的服务中就不需要自己的工作线程,客户端只需注册回调即可。

        【讨论】:

          【解决方案4】:

          这可以帮助您: WCF Service with callbacks coming from background thread?

          如果还不够,您必须向您的 web 应用实施推送通知。

          【讨论】:

            【解决方案5】:

            WCF 包含称为 Completion Callbacks 的东西。在这里,您的客户端有一个方法,它仅在异步操作完成后要求 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();
                }
            }
            

            这将允许您使用简单的事件驱动模型。

            【讨论】:

              猜你喜欢
              • 2023-03-25
              • 2018-10-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-01-04
              • 2015-06-14
              • 1970-01-01
              相关资源
              最近更新 更多