开发者

Proper Error Handling on a WCF service call

开发者 https://www.devze.com 2023-01-25 16:37 出处:网络
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 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 Get开发者_开发技巧UserId(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?


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消