2012-09-12 70 views
0

我有以下程序使用異步操作,但返回的IAsyncResult.AsyncState始終爲空。爲什麼我的asyncResult始終爲空?

我在做什麼錯了?

public interface ICommandService 
{ 
[OperationContract(AsyncPattern = true)] 
IAsyncResult BeginLogin(string userName, string password, AsyncCallback callback, object state); 

string EndLogin(IAsyncResult result); 
} 

class CommandService : ICommandService 
{ 
    public string Login(string userName, string password) 
    {    
     return "dorcohen"; 
    } 

    private Func<string, string, string> _LoginDelgateObject; 

    public IAsyncResult BeginLogin(string userName, string password, AsyncCallback callback, object state) 
    { 
     Func<string, string, string> function = new Func<string, string, string>(Login); 
     _LoginDelgateObject = function; 
     IAsyncResult result = function.BeginInvoke(userName, password, callback, state); 
     return result; 
    } 

    public string EndLogin(IAsyncResult result) 
    { 
     CommandService test = result.AsyncState as CommandService; 
     return test._LoginDelgateObject.EndInvoke(result); 
    } 
} 
+0

現在看來,這取決於您所呼叫BeginLogin(),作爲_state_與其他參數傳遞的方式。請給我們看看調用代碼嗎? –

+0

@EfranCobisi謝謝,的確與之有關。 –

回答

1

你不能使用下面的代碼在BeginLogin方法

function.BeginInvoke(userName, password, callback, this); 
相關問題