为什么我的asyncResult总是为空?

时间:2012-09-12 07:27:10

标签: c# asynchronous iasyncresult

我有以下使用异步操作的程序,但返回的IAsyncResult.AsyncState始终为null。

我做错了什么?

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);
    }
}

1 个答案:

答案 0 :(得分:1)

您无法在BeginLogin方法中使用以下代码

function.BeginInvoke(userName, password, callback, this);