2012-09-12 71 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); 
相关问题