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

发布于 2024-10-03 00:46:01 字数 1285 浏览 1 评论 0原文

我正在努力找出调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-10-10 00:46:02

据我所知,捕获超时异常是处理它的唯一方法。

我更喜欢使用其他异步模式,因为它可以更灵活地处理异常。我会去这样的事情。

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);
    }

}

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);
    }

}
笛声青案梦长安 2024-10-10 00:46:02

在我看来,后台线程的回调(异步执行产生的任何结果)应该始终包含在异常处理程序中。后台线程中未处理的异常将终止您的进程。

现在,这并不意味着您应该捕获异常并忽略它:)只是您应该正确处理它,无论这对您的应用程序意味着什么。对于某些将记录它们的应用程序。对于其他人来说,它将在某处更新某些状态。对于其他人来说,它可能会警告用户错误。或者它们的组合:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文