捕获从包装的EAP请求到WCF的异常
我在WP8环境中有一个WCF请求,我根据该请求进行了包装捕获从包装的EAP请求到WCF的异常,wcf,windows-phone-8,asynchronous,exception-handling,Wcf,Windows Phone 8,Asynchronous,Exception Handling,我在WP8环境中有一个WCF请求,我根据该请求进行了包装 我对WCF服务的呼叫如下: try { var result = await mWCFClient.PerformRequestAsync(); } catch(Exception e) { } 其中PerformRequestAsync是一种扩展方法。i、 e public static ResultType PerformRequestAsync(this WCFClient client) { // EAP w
我对WCF服务的呼叫如下:
try
{
var result = await mWCFClient.PerformRequestAsync();
}
catch(Exception e)
{
}
其中PerformRequestAsync是一种扩展方法。i、 e
public static ResultType PerformRequestAsync(this WCFClient client)
{
// EAP wrapper code
}
发生的情况是,WCF服务偶尔会出错,并返回“NotFound”。我不是100%确定为什么会发生这种情况,这似乎是一个罕见的情况。但是,问题不在于WCF服务行为,而是它在自动生成的WCF代码中的EndPerformRequestAsync()中中断,而不是转到我的异常处理程序
我应该如何以及在何处捕获此异常,因为它从未到达我预期的处理程序
[编辑]
根据Stephen的要求,我在这里包含了包装器代码:
public static Task<RegistrationResult> RegisterAsync(this StoreServiceReference.StoreServiceClient client, string token, bool dummy)
{
var tcs = new TaskCompletionSource<RegistrationResult>();
EventHandler<RegisterCompletedEventArgs> handler = null;
handler = (_, e) =>
{
client.RegisterCompleted -= handler;
if (e.Error != null)
tcs.TrySetException(e.Error);
else if (e.Cancelled)
tcs.TrySetCanceled();
else
tcs.TrySetResult(e.Result);
};
client.RegisterCompleted += handler;
PerformStoreRequest(client, () => client.RegisterAsync(), token);
return tcs.Task;
}
private static void PerformStoreRequest(StoreServiceClient client, Action action, string token)
{
using (new OperationContextScope(client.InnerChannel))
{
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers[STORE_TOKEN_HTTP_HEADER] = token;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
action.Invoke();
// TODO: Do we need to add handler here?
}
}
公共静态任务注册表同步(此StoreServiceReference.StoreServiceClient客户端,字符串令牌,bool伪)
{
var tcs=new TaskCompletionSource();
EventHandler=null;
处理程序=(u,e)=>
{
client.RegisterCompleted-=处理程序;
如果(例如错误!=null)
tcs.TrySetException(即错误);
否则(如已取消)
tcs.trysetconceled();
其他的
tcs.TrySetResult(e.Result);
};
client.RegisterCompleted+=处理程序;
PerformStoreRequest(客户端,()=>client.RegisterAsync(),令牌);
返回tcs.Task;
}
私有静态void PerformStoreRequest(StoreServiceClient客户端、操作、字符串令牌)
{
使用(新OperationContextScope(client.InnerChannel))
{
HttpRequestMessageProperty requestMessage=新的HttpRequestMessageProperty();
Headers[STORE\u TOKEN\u HTTP\u HEADER]=令牌;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name]=requestMessage;
action.Invoke();
//TODO:我们需要在这里添加处理程序吗?
}
}
现在我看了一下,我认为问题源于动作调用的性质。但是向WP8 WCF服务添加自定义头已经是一件痛苦的事了。内部的操作是异步操作,但据我所知,不是调用。
在这里,正确的处理方法是什么?您确定它永远不会到达您的处理程序吗?尝试取消选中调试->异常对话框中的“抛出”复选框。它们都未选中。这太奇怪了。而且这似乎也是非常罕见的情况,很难正确地进行测试。不过我甚至不确定错误是怎么回事。这是一个System.ServiceModel.CommunicationException。“远程服务器返回错误:NotFound.”并且在innerexception中有相应的WebException。发布EAP包装代码。可能在所有情况下,它都没有将异常放置在返回的任务上。我发布了它。我现在认为问题不在于包装器代码本身,而在于PerformStoreRequest。尽管如此,我仍然不知道如何使这一点易于理解。我觉得我可能把事情复杂化了。