异步调用中不执行回调函数

时间:2009-11-05 02:19:15

标签: wcf asynchronous callback

我正在尝试使用WCF进行简单的异步调用,但回调函数永远不会执行。谁能告诉我代码有什么问题?

我正在使用带有.Net 3.5的visual studio 2008

服务代码

 [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
}

客户端代码

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 个答案:

答案 0 :(得分:1)

您需要明确调用回调。

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

        callback(result);

        Console.ReadLine();

请参阅此处的参考资料。

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

相关问题