Web 服务重新连接模式

发布于 2024-12-04 16:51:51 字数 1572 浏览 0 评论 0原文

我正在开发一个 .Net Webform 应用程序,大量使用 Web 服务与外部服务器数据库进行通信。

因此,我试图找到处理调用 WS 方法时断开连接和失败的最佳方法。

现在,我为我调用的每个 WS 方法创建了一个代理函数(某种层),它在循环中重复特定的 WS 调用,直到成功为止。

对于同步和异步调用,我已经解决了我的问题,但是我在我的 WebService 层中添加了一个烦人的额外层,需要额外的维护和大量冗余代码。

我不相信这种标准情况没有现有的解决方案,但在任何地方都找不到它。

有什么想法吗?

的示例:

public static int WsMethod(string param1, int param2)
{
 while(true)
 {
  try
  {
   return new Webpoint().WsMethod(param1, param2);
  }
  catch (Exception)
  {
   Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
  }
 }
}

以下是我的额外层(同步)和异步

    public static void WsMethodAsync(string param1, int param2, WsMethodCompletedEventHandler handler)
    {
        while (true)
        {
            try
            {
                var server = new Webpoint();
                server.WsMethodAsyncCompleted += delegate(object sender, WsMethodAsyncCompletedEventArgs args)
                {
                    if (args.Error != null)
                    {
                        Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
                        this.WsMethodAsync(param1, param2, handler);
                    }
                    else
                    {
                        handler(sender, args);
                    }
                };
                server.WsMethodAsyncAsync(param1, param2);
                return;
            }
            catch (Exception)
            {
                Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
            }
        }
    }

I'm developing a .Net Webform application, with heavy use of web services to communicate with an outside-server database.

So, I'm trying to find the best way to deal with disconnections and failures when calling a WS method.

For now, I've made a proxy function -kind of a layer- for every WS method I call, that repeats the specific WS call in a loop until it cames out successfully.

For Both Sync and Async calls, I've solved my problem, but I added an annoying extra layer to my WebService layer, with extra maintenance, and a lot of redundant code.

I refuse to believe there's not an existing solution for this standard situation, but can't find it anywhere.

Any Ideas?

Following, an example of my extra layer (Sync):

public static int WsMethod(string param1, int param2)
{
 while(true)
 {
  try
  {
   return new Webpoint().WsMethod(param1, param2);
  }
  catch (Exception)
  {
   Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
  }
 }
}

And Async:

    public static void WsMethodAsync(string param1, int param2, WsMethodCompletedEventHandler handler)
    {
        while (true)
        {
            try
            {
                var server = new Webpoint();
                server.WsMethodAsyncCompleted += delegate(object sender, WsMethodAsyncCompletedEventArgs args)
                {
                    if (args.Error != null)
                    {
                        Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
                        this.WsMethodAsync(param1, param2, handler);
                    }
                    else
                    {
                        handler(sender, args);
                    }
                };
                server.WsMethodAsyncAsync(param1, param2);
                return;
            }
            catch (Exception)
            {
                Thread.Sleep(new TimeSpan(0, 0, sleep_seconds));
            }
        }
    }

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

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

发布评论

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

评论(2

伤感在游骋 2024-12-11 16:51:51

我不会推荐这种模式。如果您的调用参数存在问题,这将永远运行。
通常,我会捕获一些预期的异常(CommunicationException、SocketException,无论您需要什么)并为此返回一些状态代码(Ok,或 NoNetwork,或其他)。
或者将所有预期的异常包装到 MyCommunicationException 中并抛出此异常(以向调用者隐藏实现细节并使异常处理更容易),

但将控制权交还给调用者并让调用者决定如何继续。不要捕获其他意外异常或重新抛出它们。

然后,呼叫者可以决定一次又一次地尝试,或者尝试 3 次,或者其他什么。

I would not recommend this pattern. If there is some problem with the parameters on your call this will run forever.
Normaly I would catch the few expected exceptions (CommunicationException, SocketException, whatever you need) and return some status-code for this (Ok, or NoNetwork, or whatever).
Or wrap up all expected exceptions into a MyCommunicationException and throw this (to hide implementation details from the caller and make exception-handling easier for it)

But give the control back to the caller and let the caller decide how to go on. Don't catch the other unexpected exceptions or rethrow them.

The caller can then decide to try time and again or 3-times or whatever.

讽刺将军 2024-12-11 16:51:51

如果服务、连接或发出的请求确实出现问题,那么这种情况就会无限期地重复,而不会告诉您出了什么问题。

服务调用失败有何影响?它真正失败的频率有多少?而且,最重要的是,它失败的原因是什么?如果原因是可以解决的,就应该解决。没有解决。

举一个简单的例子,如果这个后端服务调用是由网站用户发起的(例如,他们试图获取一些数据进行编辑),那么如果调用失败,您只需向用户显示一个错误。像这样的东西:

“很抱歉,目前无法获得该数据。支持团队已收到有关此问题的通知。请重试您的请求。如果问题仍然存在,请致电 800-555-1234 联系帮助台”

现在,这不应该只是一个向用户显示的通用错误,无论发生什么情况。代码需要足够强大才能区分一种错误和另一种错误。如果无法访问该服务,则会出现此错误。如果服务表明请求无效,则说明用户正在执行的操作或您的代码正在执行的操作存在问题,需要修复。等等。

如何处理错误并维护可用的应用程序最终取决于您和整个企业。但老实说,我不能推荐您在这个问题上概述的方法。这种方法并不能解决任何问题,它只是忽略问题,直到问题变得更糟。您需要确定错误的根本原因并解决它,而不是忽略它们。

此外,只要抑制/忽略错误,小猫就会死亡。

If something were genuinely wrong with the service, or the connection thereto, or the request being made, then this would repeat indefinitely without ever telling you what's wrong.

What are the implications of the service call failing? How often does it really fail? And, most importantly, for what reason does it fail? If the reason is something that can be fixed, it should be fixed. Not worked around.

As a simple example, if this back-end service call is something initiated by a user of the website (say, they're trying to fetch some data to edit) then if the call fails you just present an error to the user. Something like:

"I'm sorry, but that data is not available at this time. The support team has been notified of this problem. Please try your request again. If the problem persists, contact the help desk at 800-555-1234."

Now, this shouldn't just be a single generic error to show the user no matter what happens. The code needs to be robust enough to discern one kind of error from another. If the service is unreachable, this error applies. If the service is saying that the request is invalid, then there's something wrong either with that the user is doing or what your code is doing, and that needs to be fixed. Etc.

How you deal with the errors and maintain a usable application is ultimately up to you and the business overall. But I honestly can't recommend the approach you outline on the question. That approach doesn't solve anything, it just ignores the problem until it gets worse. You need to determine the root cause of the errors and address that, not ignore them.

Also, any time an error is suppressed/ignored, a kitten dies.

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