防止 WCF 回调出现异常

发布于 2024-08-23 02:18:54 字数 409 浏览 3 评论 0原文

我正在制作一个小型聊天应用程序。我遇到了这样的情况:一个客户端发送一条消息,然后通过使用房间中其他客户端的回调,他们很快就会收到该消息。因此,如果房间里有 10 个人,则发送消息的客户端的服务实例将在 10 个线程中调用此方法:

targetCallback.RecieveMessage(message);

现在,互联网连接不稳定,可能会发生这种情况:执行,因此会抛出异常,并且客户端实例会自动销毁,从而使消息未发送或成功一半。

我发现一些例子根本没有真正处理这些异常,而其他例子则很简单:

try
{
    targetCallback.RecieveMessage(message);
}
catch
{
}

但是,这是处理此异常的最佳方法吗?

I'm making a small chat application. I got to the point where one client sends a message, and then by using callbacks of other clients in the room, they quickly recieve the message. So, if there are 10 people in the room, the service instance of the client who is sending the message, will invoke this in 10 threads:

targetCallback.RecieveMessage(message);

Now, internet connections are volatile, and it could just so happen that it breaks a moment before that's performed, so an exception would be thrown and the client instance automatically destroyed, leaving the message unsent or half-successful.

I found examples that don't really handle those exceptions at all, while other put a simple:

try
{
    targetCallback.RecieveMessage(message);
}
catch
{
}

But, is that the best way to handle this exception?

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

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

发布评论

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

评论(2

西瓜 2024-08-30 02:18:54

请参阅

http://msdn.microsoft.com/en-us/library/aa354510 .aspx

以获得好的建议。简而言之,您应该捕获 TimeoutException 和 CommunicationException。

See

http://msdn.microsoft.com/en-us/library/aa354510.aspx

for good advice. Briefly, you should catch TimeoutException and CommunicationException.

套路撩心 2024-08-30 02:18:54

我在调用 WCF 回调时也见过 ObjectDisposeExceptions,也许还见过某种 SecurityException,但我不记得是哪一个。

对于需要更高可靠性的应用程序,我编写了一个基本的包装方法,用于捕获所有异常并从回调集合中删除该回调。然后我只通过包装器调用回调。

通常,我还会从 OperationContext 中捕获一些有关连接的上下文信息,例如 IP 地址、会话 ID 和用户令牌,以便我可以识别失败的人员并记录下来。但这留给读者作为练习。

public interface ICallbackContract
{
    void Operation(string arg);
}

class Program
{
    private List<ICallbackContract> Callbacks;

    private void SendMessage(string msg)
    {
        lock (this.Callbacks)
        {
            foreach (var callback in this.Callbacks)
            {
                this.InvokeWcf(callback, (c) => c.Operation(msg));
            }
        }
    }

    public void InvokeWcf(this ICallbackContract contract, Action<ICallbackContract> op)
    {
        if (((ICommunicationObject)contract).State != CommunicationState.Opened)
        {
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.LogError("That contract isn't open! Disconnected.");
            return;
        }

        try
        {
            op(contract);
        }
        catch (TimeoutException ex)
        {
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.LogError("That contract timed out! Disconnected.", ex);
            return;
        }
        catch (CommunicationException ex)
        {
            ...
        }
        catch (ObjectDisposedException ex)
        {
            ...
        }
        catch (Exception ex)
        }
            // Unexpected case.
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.FatalError("Something really bad happened!.", ex);
            throw;
        {
    }

}

I've also seen ObjectDisposedExceptions when calling WCF callbacks, and maybe a SecurityException of some sort, but I can't remember which one.

For applications that need a higher degree of reliability, I write a basic wrapper method that catches all the exceptions and removes that callback from your callback collection. Then I only ever invoke the callback through the wrapper.

Normally I'd also capture some contextual information about the conneciton from the OperationContext like IP address, sessionID and user token so I can identify who failed and log it. But that's left as an exercise to the reader.

public interface ICallbackContract
{
    void Operation(string arg);
}

class Program
{
    private List<ICallbackContract> Callbacks;

    private void SendMessage(string msg)
    {
        lock (this.Callbacks)
        {
            foreach (var callback in this.Callbacks)
            {
                this.InvokeWcf(callback, (c) => c.Operation(msg));
            }
        }
    }

    public void InvokeWcf(this ICallbackContract contract, Action<ICallbackContract> op)
    {
        if (((ICommunicationObject)contract).State != CommunicationState.Opened)
        {
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.LogError("That contract isn't open! Disconnected.");
            return;
        }

        try
        {
            op(contract);
        }
        catch (TimeoutException ex)
        {
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.LogError("That contract timed out! Disconnected.", ex);
            return;
        }
        catch (CommunicationException ex)
        {
            ...
        }
        catch (ObjectDisposedException ex)
        {
            ...
        }
        catch (Exception ex)
        }
            // Unexpected case.
            lock (this.Callbacks)
                Callbacks.Remove(contract);
            myLogger.FatalError("Something really bad happened!.", ex);
            throw;
        {
    }

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