WCF 服务调用的正确错误处理
我正在努力找出调用 WCF 服务并在发生错误或超时时进行处理的最佳方法。这就是我正在做的:
我有一个像这样的数据服务接口:
public interface IDataService
{ void GetUserId(字符串用户名, 字符串密码, Action getUserIdComplete); 我
是这样实现的:
public class MockDataService : IDataService
{
private Action<string> _getUserIdCompleted;
private SomeServiceClient;
public MockDataService()
{
_proxy = new SomeServiceClient();
}
public void GetUserId(string userName, string password, Action<int> getUserIdComplete)
{
_getUserComplete = getUserIdComplete;
var request = new UserRequest();
request.UserName = userName;
request.Password = password;
//populate any other request info
_proxy.GetUserIdCompleted += new EventHandler<GetUserCompletedEventArgs>(_proxy_GetUserIdCompleted);
_proxy.GetUserIdAsync(request);
}
void _proxy_GetUserIdCompleted(object sender, GetUserIdCompletedEventArgs e)
{
_proxy.GetUserIdCompleted -= new EventHandler<GetUserCompletedEventArgs>(_proxy_GetUserIdCompleted);
_getUserIdComplete(e.UserId);
}
}
我的问题是,当发生错误或请求超时时,应用程序将终止。我可以在调用周围添加一个 try catch 块,但这听起来是个坏主意。
有人可以帮我用这种方法优雅地处理超时和错误吗?
I am tyring to figure out the best way to go about calling a WCF service and handeling an error or timeout when it occurs. Here is what I am doing:
I have a data service interface like this:
public interface IDataService
{
void GetUserId(string userName, string password, Action getUserIdComplete);
}
I implement it like this:
public class MockDataService : IDataService
{
private Action<string> _getUserIdCompleted;
private SomeServiceClient;
public MockDataService()
{
_proxy = new SomeServiceClient();
}
public void GetUserId(string userName, string password, Action<int> getUserIdComplete)
{
_getUserComplete = getUserIdComplete;
var request = new UserRequest();
request.UserName = userName;
request.Password = password;
//populate any other request info
_proxy.GetUserIdCompleted += new EventHandler<GetUserCompletedEventArgs>(_proxy_GetUserIdCompleted);
_proxy.GetUserIdAsync(request);
}
void _proxy_GetUserIdCompleted(object sender, GetUserIdCompletedEventArgs e)
{
_proxy.GetUserIdCompleted -= new EventHandler<GetUserCompletedEventArgs>(_proxy_GetUserIdCompleted);
_getUserIdComplete(e.UserId);
}
}
My problem is that, when an error happens or the request times out, the application terminates. I could wrap a try catch block around the call but that sounds like a bad idea.
Can someone please help me to elegantly handle timeouts and errors with this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,捕获超时异常是处理它的唯一方法。
我更喜欢使用其他异步模式,因为它可以更灵活地处理异常。我会去这样的事情。
As far as I know, catching the timeout exception is the only way about handling it.
I prefer using the other asynchronous pattern because it allows greater flexibility in handling the exceptions. I would go something like this.
在我看来,后台线程的回调(异步执行产生的任何结果)应该始终包含在异常处理程序中。后台线程中未处理的异常将终止您的进程。
现在,这并不意味着您应该捕获异常并忽略它:)只是您应该正确处理它,无论这对您的应用程序意味着什么。对于某些将记录它们的应用程序。对于其他人来说,它将在某处更新某些状态。对于其他人来说,它可能会警告用户错误。或者它们的组合:)
In my very humble opinion, callbacks on background threads (anything that results from async executions) should always be wrapped in an exception handler. Unhandled Exceptions in background threads will kill your process.
Now, that does not mean that you should trap the exception and ignore it :) Just that you should handle it properly, whatever that means for your application. For some applications that will be logging them. For others it will be updating some status somewhere. For others it might be alerting the user of an error. Or a combination of them :)