实现异步WCF服务 [英] Implementing an async WCF service

查看:192
本文介绍了实现异步WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我目前正在对分离成客户端和服务器端的WPF应用程序 - 使用WCF。我不喜欢我最初得到了与直正解残局,所以现在我正在改制按照米格尔·卡斯特罗的截屏的建议,的 WCF至尊。如果你不熟悉的视频他基本上手动设置整个通信 - 无需使用服务引用。这包括:

I have a WPF application which I'm currently working on separating into a client and server side - using WCF. I did not like the mess I initially got with the straight forward solution, so now I'm restructuring following the recommendations in the screencast of Miguel Castro, WCF Extreme. If you're not familiar with the video he basically sets up the whole communication manually - without using service references. This includes:


  • 所有的服务和数据契约常见的合同 - 由客户端和服务器
  • 引用
  • 托管服务控制台应用程序

  • 在客户端上
  • 代理类映射了服务,并(使用ClientBase或ClientFactory)通过调用它

我一直跟着他的所有步骤,我真的很喜欢这是怎么回事。不过,他并没有解决异步服务调用,这是希望我来使用。

I've followed all his steps, and I really like where this is going. However, he does not address asynchronous service calls, and this is what I'd like to use.

在添加服务引用我可以检查一个生成异步操作复选框,然后我得到MyServiceCompleted和MyServiceAsync中。不过,我想这是这是添加服务引用,而不是一些魔法在这个基础上的类时生成的东西吗?

When adding a Service Reference I can check a "Generate async operations" checkbox, and I get MyServiceCompleted and MyServiceAsync. However, I guess this is something which was generated when adding the service reference, and not some magic in the classes this builds on?

所以,我可以得到自ClientBase或ClientFactory某种程度上异步操作?还是我来定义实际的服务器端服务是异步?如果是这样的 - 可能有人请给我如何得到一个简单的异步服务启动一些提示或例子?我一直在MSDN上阅读各地关于这个问题,但它给我留下的所有困惑的感觉像一个白痴不得到这已经..

So, can I get async operations from ClientBase or ClientFactory somehow? Or do I have to define the actual server side services to be async? If so - could someone please give me a few hints or examples on how to get started with a simple async service? I've been reading around on MSDN on the subject, but it has left me all confused feeling like an idiot for not getting this already..

推荐答案

在服务器端实现异步操作非常简单。确保你的方法名一致,并且prefixed有开始和结束。 GetImageAsyncResult是一个自定义的IAsyncResult实现(很多网站上的例子)。

Implementing async operations on server side is quite simple. Make sure you method names match and are prefixed with Begin and End. GetImageAsyncResult is a custom IAsyncResult implementation (lots of examples on web).

    public class MapProvider : IMapProvider //implementation - belongs to server
    {
         public IAsyncResult BeginGetImage(int level, int x, int y, string[] layers, AsyncCallback callback, object state)
         {
              GetImageAsyncResult asyncResult = new GetImageAsyncResult(level, x, y, layers, callback, state);
              ThreadPool.QueueUserWorkItem(Callback, asyncResult);
              return asyncResult;
         }

         private void Callback(object state)
         {

              GetImageAsyncResult asyncResult = state as GetImageAsyncResult;
              asyncResult.Image = TileProvider.GetImage(asyncResult.Level, asyncResult.X, asyncResult.Y, asyncResult.Layers);
              asyncResult.Complete();
         }

         public System.Drawing.Bitmap EndGetImage(IAsyncResult result)
         {
              using (GetImageAsyncResult asyncResult = result as GetImageAsyncResult)
              {
                   asyncResult.AsyncWait.WaitOne();
                   return asyncResult.Image;
              }
         }
    }

    public class MapProviderProxy : ClientBase<IMapProvider>, IMapProvider, IDisposable
    {
         public IAsyncResult BeginGetImage(int level, int x, int y, string[] layers, AsyncCallback callback, object state)
         {
              return Channel.BeginGetImage(level, x, y, layers, callback, state);
         }

         public System.Drawing.Bitmap EndGetImage(IAsyncResult result)
         {
              return Channel.EndGetImage(result);
         }

         public void Dispose()
         {
              if (State == CommunicationState.Faulted)
              {
                   Abort();
              }
              else
              {
                   try
                   {
                        Close();
                   }
                   catch
                   {
                        Abort();
                   }
              }
         }
    }

这篇关于实现异步WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆