我有一个正常的方法
public List<string> FindNearByCity(string targetCity)
{
// ... some implementation
}
我想为此方法添加异步支持,所以我写了这个:
public IAsyncResult BeginFindNearByCity(string targetCity, AsyncCallback callback, object obj)
{
Func<string, List<string>> method = FindNearByCity;
return method.BeginInvoke(targetCity, callback, obj);
}
public List<string> EndFindNearByCity(IAsyncResult result)
{
Func<string, List<string>> method = FindNearByCity;
return method.EndInvoke(result);
}
BeginFindNearByCity运行正常,但是当它遇到EndFindNearByCity时,异常会在它触及EndInvoke时抛出。
我研究了auto gen异步Web服务方法,似乎我需要实现调用“ChannelBase”的东西
任何人都能指出一些更简单的东西,比如我可以看一下的教程或样本吗?
由于
答案 0 :(得分:3)
您在EndXXX
方法中创建的代理是您在BeginXXX
方法中创建的代理的单独实例,因此它不知道您传递的IAsyncResult
它的EndInvoke()
方法。
您必须在EndXXX
方法中使用与BeginXXX
方法相同的委托,例如
public class Foo
{
private readonly Func<string, List<string>> method;
public Foo()
{
this.method = this.FindNearByCity;
}
public IAsyncResult BeginFindNearByCity(string targetCity, AsyncCallback callback, object obj)
{
return this.method.BeginInvoke(targetCity, callback, obj);
}
public List<string> EndFindNearByCity(IAsyncResult result)
{
return this.method.EndInvoke(result);
}
public List<string> FindNearByCity(string targetCity)
{
// ... some implementation
}
}