views:

232

answers:

1

I am trying to make a simple asynchronous call with WCF, but the callback function is never executed. Can anyone tell me what is wrong with the code?

I am using visual studio 2008 with .Net 3.5

You can download a visual studio 2008 solution from here: http://dl.getdropbox.com/u/419244/AsyncCalls.zip

Service code

 [ServiceContract]
public interface IService1
{
    [OperationContract(AsyncPattern = true) ]
    IAsyncResult BeginGetData(string value, AsyncCallback callback, object state);

    string EndGetData(IAsyncResult result);
}

public class Service1 : IService1
{

    #region IService1 Members

    public IAsyncResult BeginGetData(string value, AsyncCallback callback, object state)
    {
        return new CompletedAsyncResult<string>(value, state);
    }

    public string EndGetData(IAsyncResult r)
    {
        CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
        return result.Data;
    }

    #endregion
}

Client side code

class Program
{
    static void Main(string[] args)
    {

        Service1Client client = new Service1Client();

        Console.WriteLine("Start async call");
        IAsyncResult result = client.BeginGetData("abc", callback, null);
        Console.ReadLine();
    }
    static void callback(IAsyncResult result)
    {
        string a = "in callback";

        Console.WriteLine(a);
    }
}
+1  A: 

You need call callback explicitly.

        IAsyncResult result = client.BeginGetData("abc", callback, null);

        callback(result);

        Console.ReadLine();

see reference here.

http://blogs.msdn.com/mjm/archive/2005/05/04/414793.aspx

Henry Gao
+1 Yes, you are missing the very basic part. If you don't specify the callback in BeginInvoke, how will it be called?Another links for your reference http://stackoverflow.com/questions/1047662/what-is-asynccallback/1047677#1047677http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx
Rashmi Pandit