Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Updated 21-Sep-11 0:16am
v2

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.

C#
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>
 
Share this answer
 
v2
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);
 
Share this answer
 
actually i dont want to use service reference.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900