Windows Phone 7 在 foreach C# 5.0 异步中等待
我正在使用 C# 异步 CTP 调用一些返回 URI 的远程函数,我有以下代码:
public async Task<Uri> GetUriAsync(string service, string endpoint)
{
Uri result = null;
foreach (var service in _serviceProvider)
{
try
{
result = await service .GetAsync(service,endpoint);
if (result != null)
return result;
}
catch (Exception)
{
}
}
return result;
}
由于 foreach 中有一个等待,因此该方法应该在第一个等待中返回,但通过调试我注意到,当代码到达等待它跳转到“返回结果”
我之前使用过异步ctp(不在Windows Phone上)并完成了与此类似的代码。
这有什么问题吗?
编辑:这不是调试器错误/错误,因为远程调用从未完成(我在那里登录)。
I'm using the C# async CTP to call some remote functions that return me a URI, I have the following code:
public async Task<Uri> GetUriAsync(string service, string endpoint)
{
Uri result = null;
foreach (var service in _serviceProvider)
{
try
{
result = await service .GetAsync(service,endpoint);
if (result != null)
return result;
}
catch (Exception)
{
}
}
return result;
}
Since there is a await inside the foreach, this method should return in the first await, but by debugging I noticed that when the code reachs the await it jumps to "return result"
I've used async ctp before (not on windows phone) and done code similar to this one.
What is wrong in that?
EDIT: This isn't the debugger error/bug, since the remote call is never done (I have a log in there).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并没有什么问题。 CTP 中的调试体验远非完美。它在 VS 11 CTP 中有所改进,但仍然不完美。
在运行时,这应该会产生您期望的结果(返回第一个不为空的结果)。
There is nothing wrong with that. The debugging experience in the CTP is far from perfect. It's improved, but still not perfect, in the VS 11 CTP.
At runtime, this should produce the results you're expecting (returning the first result that is not null).
问题是 AsyncCtp dll 的内部异常。看来调试器将此异常归类为“第一次机会”异常,并决定不中止调试会话。通过使代码正常运行但不执行 Web 请求(在本例中),开发人员会认为一切正常。
感谢您的帮助@Reed。
The problem was an internal exception at AsyncCtp dll. It seams that the debugger categorizes this exceptions as First Chance exception and decide not to abort the debug session. By doing the code runs normally but without doing the web request (in this case) leaving the developer to think that everything is ok.
Thanks for the help @Reed.