简体   繁体   English

WCF服务调用上的正确错误处理

[英]Proper Error Handling on a WCF service call

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. 我想找出最好的方法来调用WCF服务,并在发生错误或超时时进行处理。 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); {void GetUserId(字符串userName,字符串密码,操作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. 我可以在电话周围包裹一个try catch块,但这听起来是个坏主意。

Can someone please help me to elegantly handle timeouts and errors with this method? 有人可以帮我用这种方法优雅地处理超时和错误吗?

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. 我会这样。

public class MockDataService : IDataService
{
    private SomeServiceChannel _channel;

    public MockDataService()
    {
        var channelFactory = new ChannelFactory<SomeServiceChannel>(
                    "CustomBinding_SomeService");
        _channel = channelFactory.CreateChannel();
        //to increase the timeout
        _channel.OperationTimeout = TimeSpan.FromMinutes(5);
    }

    public void GetUserId(string userName, string password, Action<int> 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);
        _channel.BeginGetUserId(request, (iar) =>
           {
               try
               {
                   var result = _channel.EndGetUserId(iar);
                   getUserIdComplete(result.UserId);
               }
               catch (Exception ex)
               {
                   //handle the exception
               }
           }, null);
    }

}

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 :) 或它们的组合:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM