简体   繁体   English

我应该如何处理从WCF回调接口对象集合之一引发的异常?

[英]How should I handle exceptions thrown from one of a collection of WCF callback interface objects?

UPDATE: Since writing this question, I have come across this Bug Report which provides a method of replicating the symptoms of the bug I am seeing consistently. 更新:自从写了这个问题以来,我遇到了这个错误报告 ,该报告提供了一种复制我一直看到的错误症状的方法。

I cannot, yet, confirm if this is the cause of the bug I am experiencing, but it warrants some investigation. 我还不能确定这是否是我遇到错误的原因,但值得进行一些调查。 I have not come across a work-around for the bug described in the report yet. 我还没有找到报告中描述的错误的解决方法。

To summarise the report: If the WCF client closes during authentication at the WCF service then an asynchronous exception is thrown at the service, which (in my case) is not caught until it reaches the program.cs file and so makes it difficult to handle gracefully. 总结报告:如果WCF客户端在WCF服务进行身份验证期间关闭,则会在服务处引发异步异常,在我看来,该异常不会被捕获,直到到达program.cs文件为止,因此很难处理优雅地。

Original Question: 原始问题:

I have a WCF Service hosted within a Windows Service. 我在Windows服务中托管了WCF服务。

The WCF service allows clients to subscribe (providing a callback), after which, the service publishes updates to all subscribers. WCF服务允许客户端进行订阅(提供回调),此后,该服务将更新发布到所有订阅者。

The service maintains a Dictionary<int, ICallback> of all current subscribers' callback interfaces. 该服务维护所有当前订户的回调接口的Dictionary<int, ICallback>

When an event occurs requiring information to be published, the service loops through the collection and calls the appropriate method defined in ICallback. 当发生事件要求发布信息时,服务将遍历集合并调用ICallback中定义的适当方法。

This all works fine and all occasions where the ICallback objects are used are wrapped in a try catch in case of object disposed exceptions and any other exceptions which might occur. 这一切都很好,并且在所有情况下,使用ICallback对象的情况都会被包裹在try catch中,以防对象处置异常和可能发生的任何其他异常。

The problem I am having is that a Communication Exception is being raised at the Service: 我遇到的问题是在服务处引发了通信异常:

System.ServiceModel.CommunicationException: System.ServiceModel.CommunicationException:

The socket connection was aborted. 套接字连接已中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. 这可能是由于处理您的消息时出错,远程主机超出了接收超时或潜在的网络资源问题引起的。

Local socket timeout was '10675199.02:48:05.4775807'. 本地套接字超时为'10675199.02:48:05.4775807'。

System.IO.IOException: The read operation failed, see inner exception. System.IO.IOException:读取操作失败,请参阅内部异常。

System.ServiceModel.CommunicationException: The socket connection was aborted. System.ServiceModel.CommunicationException:套接字连接已中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. 这可能是由于处理您的消息时出错,远程主机超出了接收超时或潜在的网络资源问题引起的。

Local socket timeout was '10675199.02:48:05.4775807'. 本地套接字超时为'10675199.02:48:05.4775807'。

System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host System.Net.Sockets.SocketException:现有连接被远程主机强行关闭

This exception is not being caught and is causing the application to crash. 未捕获此异常,并导致应用程序崩溃。

Because it is not being caught, I am assuming that the exception is not being thrown during a time when the service is attempting to publish data because this always occurs within a try - catch. 因为没有被捕获,所以我假设在服务试图发布数据的时候没有抛出异常,因为这总是在try-catch中发生。

Could this exception be caused by the client application closing the channel/socket incorrectly? 客户端应用程序错误地关闭通道/套接字会导致此异常吗?

More importantly, how is it possible to catch an exception raised in this way when the object from which the exception originates is not currently being accessed within a running thread? 更重要的是,当正在运行的线程中当前未访问异常源的对象时,如何捕获以这种方式引发的异常?

ie the ICallback object is sat in a dictionary, not being used, when the exception is thrown - so how do I handle the exception? 即,抛出异常时,ICallback对象位于字典中,而不被使用-那么如何处理异常? where do I put the try catch? 我该放在哪里?

WCF Service Subscribe Method WCF服务订阅方法

public static int Subscribe()
{
    int id = -1;

    OperationContext currentContext = OperationContext.Current;

    if (currentContext != null)
    {
        ICallback callback = currentContext.GetCallbackChannel<ICallback>();

        if (callback != null)
        {
            id = GetNextId();

            lock (SubscriberLock)
            {
                Subscribers.Add(id, callback);
            }
        }
    }
    return id;
}

The exception is thrown some period of time after the completion of this method. 此方法完成后的一段时间会引发异常。

I don't feel totally happy that I have explained the problem very clearly so please ask for clarification if there is a specific point which isn't very clear. 我已经很清楚地解释了这个问题,对此我并不感到十分高兴,因此请澄清一下是否有明确的问题。

Thanks! 谢谢!

The communication exception would be thrown at client side, rather than on WCF side. 通信异常将在客户端而不是WCF端抛出。 You could catch the communication exception there at client side by wrapping it inside try and catch. 通过将其包装在try and catch中,可以在客户端捕获通信异常。 CommunicationException is the base type of all the exception generated at wcf. CommunicationException是在wcf生成的所有异常的基本类型。 You should also add logging to your wcf service and client. 您还应该将日志记录添加到wcf服务和客户端。 Details of how to add logging here. 有关如何在此处添加日志记录的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM