从void异步方法获取返回值

时间:2016-06-24 14:46:27

标签: c# web-services asynchronous wsdl void

我有一个Web服务(WSDL)和一些void异步方法(我无法将其更改为返回Task)来调用更长的请求。

如何从这些方法中获取结果值?

在Web服务上,我有一些EventHandlers,EventArgs(我需要在EventHandle委托上使用)。

所以这是根本问题,我怎样才能弄清所有这些事情才能得到我的结果呢?

我有这个,直到现在:

//Methods signatures
getSomethingCompletedEventArgs(object[] results, Exception, bool, object userState)
getSomethingCompletedHandler(object sender, Service.getSomethingCompletedEventArgs)

//Didn't build
Service srv = new Service()
srv.getSomethingCompleted += new getSomethingCompletedEventHandler(srv.getSomethingAsync);

//Other try
var args = new getSomethingCompletedEventArgs(null, new Exception(), false, "");
//Didn't build too (Method name expected)    
srv.getSomethingCompleted += new getSomethingCompletedEventHandler(args);


//This built, but I don't know how to get the results
public async static void GetSomething()
{
   await Task.Run(() => srv.getSomethingAsync(arg1,arg2,arg3));
}

1 个答案:

答案 0 :(得分:1)

所以,我错过了一个实现我的回调并委托它的方法。

Service srv = new Service();

src.getSomethingCompleted += returnMethod;

Task.Run(() => srv.getSomethingAsync(arg1, arg2, arg3));

private static void returnMethod(object sender, getSomethingCompletedEventArgs e)
{
    var result = e.Result.items.ToList();
}

当getSomethingAsync收到“已完成”状态时,将调用return方法。