处理与WCF网络相关的例外的正确方法

发布于 2025-01-28 07:48:58 字数 2122 浏览 1 评论 0原文

在应用程序中,使用WCF(基本的HTTP绑定,没有花哨的东西)与远程服务有一些通信始终返回true的方法(例如ping或其他东西

void Foo()
        {
            var client = new PingServiceClient();

            try
            {
                bool result = client.PingAsync().GetAwaiter().GetResult();
            }
            catch
            {
                //log something
            }
            finally
            {
                client.Abort();
            }
        }

。我应该在网络下降时正确处理案例吗? ,但不会崩溃。

Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll

未观察到的taskexception),或者有时只是默默地输出大量的错误消息

提前致谢。

UPD: 我试图通过开始/结束对重新创建代理,并覆盖部分类别中的最终方法实现:

public partial class PingServiceClient : IPingServiceClient, PingService
    {
        public Task<bool> PingSync()
        {
            return Task.Factory.FromAsync(BeginPing(null, null), HandledEndPing);
        }

        private bool HandledEndPing(System.IAsyncResult result)
        {
            var res = false;

            try
            {
                res = EndPing(result);
            }
            catch (Exception e)
            {
                ;
            }

            return res;
        }
    }

输出中的消息仍然与以前相同(尽管捕获量都在起作用)。

In the application I"m working with, there is some communication with the remote service using WCF (basic http binding, no fancy stuff). Since the client is lightweigh, the details of the server are irrelevant, you may assume that there is just a method that always return true (like ping or something).

The proxy is generated using a Task option, the new client instance is created each time the operation is called. Something like this could be spinning inside the timer:

void Foo()
        {
            var client = new PingServiceClient();

            try
            {
                bool result = client.PingAsync().GetAwaiter().GetResult();
            }
            catch
            {
                //log something
            }
            finally
            {
                client.Abort();
            }
        }

My question is, how should I correctly handle the cases when the network is down? Because the behavior is different. I either get an application crashing (I assume on a task finalizer, which is for some reason not handled neither in AppDomain.CurrentDomain.UnhandledException nor in TaskScheduler.UnobservedTaskException), or sometimes it just silently outputs tons of error messages, but not crashing anything. Messages like these:

Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.ServiceModel.EndpointNotFoundException' in System.ServiceModel.Internals.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.AggregateException' in mscorlib.dll

I'm struggling to find a graceful way of handling these, so if anybody has some knowledge regarding this please share the approach.

Thanks in advance.

UPD:
I have tried to re-create the proxy with Begin/End pair and override the end method implementation in a partial class:

public partial class PingServiceClient : IPingServiceClient, PingService
    {
        public Task<bool> PingSync()
        {
            return Task.Factory.FromAsync(BeginPing(null, null), HandledEndPing);
        }

        private bool HandledEndPing(System.IAsyncResult result)
        {
            var res = false;

            try
            {
                res = EndPing(result);
            }
            catch (Exception e)
            {
                ;
            }

            return res;
        }
    }

Still the same barrage of messages in the output as before (the catch is working, though).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文