0
我正在嘗試使用WCF進行簡單的異步調用,但從不執行回調函數。任何人都可以告訴我代碼有什麼問題嗎?在異步調用中不執行回調函數
我使用Visual Studio 2008與.net 3.5
服務代碼
[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是的,你錯過了最基本的部分。如果你沒有在BeginInvoke中指定回調,它將如何被調用? 另一個鏈接供大家參考http://stackoverflow.com/questions/1047662/what-is-asynccallback/1047677#1047677 http://msdn.microsoft.com/en-us/library/2e08f6yc.aspx – 2009-11-22 06:11:01