创建从Silverlight应用程序到已托管的WCF服务的异步代理 [英] creating a asynchronous proxy from a silverlight application to a already hosted WCF Service

查看:70
本文介绍了创建从Silverlight应用程序到已托管的WCF服务的异步代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经托管了WCF服务.此WCF服务没有实现任何异步功能.表示我的界面中没有类似的内容-
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginGetUser(int id);
用户EndGetUser(IAsyncResult ar);

相反,我有
[OperationContract]
用户GetUser(int id);

现在,我想创建一个从Silverlight应用程序到此WCF服务的异步代理,并异步调用方法BeginGetUser.

我也不想使用服务参考.想要以编程方式进行操作.

我在Google上进行了很多搜索,但除了使用IAsyncResult之外,找不到其他选择.

有人可以在这里帮助我吗?真的很棒.

在此先感谢

i have a already hosted WCF service. this WCF service doesn not have any asynchronous feature implemented in it. meaning i dont have something like this in my interface-
[OperationContract(AsyncPattern=true)]
IAsyncResult BeginGetUser(int id);
User EndGetUser(IAsyncResult ar);

instead i have
[OperationContract]
User GetUser(int id);

Now i want to create a asynchronous proxy from my silverlight application to this WCF service and call the method BeginGetUser asynchronously.

I also dont want to use service reference. Want to do it programatically.

I searched a lot on google but could not find any alternatives other than using IAsyncResult.

Anybody can help me out here? would be really greatful.

Thanks in advance

推荐答案

您应该能够向Silverlight应用程序添加服务引用,并告诉实现者使用Asyncronouse调用.

http://msdn.microsoft.com/en-us/library/cc197937 (v = VS.95).aspx [
You should be able to add a service reference to the Silverlight Application and tell the implimentation to use Asyncronouse calls.

http://msdn.microsoft.com/en-us/library/cc197937(v=VS.95).aspx[^]

Then create a managing class (for ease of use later) that will setup the Service and create methods for calling the Async service methods.
You can then use the ServiceHelper class within the Silverlight Application

For Eg.

Public class MyServiceHelper
{
   private myServiceReference.myServiceReferenceClient _service;
   public MyServiceHelper()
   { 
      // This will use the Web.Config client section to initiate the service.
      _service = new myServiceReference.myServiceReferenceClient();
      _service.GetUserCompleted += new EventHandler<myservicereference.getusercompletedeventargs>(OnGetUserCompleted);
   }
   
   private void OnGetUserCompleted(object sender, GetUserCompletedEventArgs e)
   {
      //Get the callback we passed in from the method

      Action<user> callback = e.UserState as Action<user>;
      if (e.Error != null)
      {
          //Log Error here
          
          throw e.Error;
      }
      
      if (callback!= null)
      {
          if (e.Result != null)
          {
              //Send the result back.
              callback.Invoke(e.Result);
          }
      }
    } 

    public void GetUser(int id, Action<user> callback)
    { 
       //Call the service with the callback as the user object.
       _service.GetUserAsync(id, callback);
    }
}  </user></user></user></myservicereference.getusercompletedeventargs>


我不知道为什么,但是它一直在文本的底部添加垃圾.现在更新了三次:(

该行也应显示为

新的EventHandler< myservicereference.getusercompletedeventargs>(OnGetUserCompleted);
I don''t know why but it keeps adding the garbage at the bottom of the text. Updated it three times now :(

Also the line should read

new EventHandler<myservicereference.getusercompletedeventargs>(OnGetUserCompleted);


实际上我不想使用服务引用.
actually i dont want to use service reference.


这篇关于创建从Silverlight应用程序到已托管的WCF服务的异步代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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