WCF - 抛出许多许多第一次机会异常 - 然后使应用程序崩溃

Posted

技术标签:

【中文标题】WCF - 抛出许多许多第一次机会异常 - 然后使应用程序崩溃【英文标题】:WCF - Many many many first chance exceptions being thrown - then crashes App 【发布时间】:2011-03-18 20:21:01 【问题描述】:

我有一个 WPF 应用程序(主机),除其他外,它是具有多个 ServiceHost 的自托管 WCF(如下所示的示例)

host= new ServiceHost(typeof(Data));
ServiceEndpoint endpoint = _HostData.AddServiceEndpoint(typeof(IData), _tcpBindingBO, _netTcpAddress + "data");


// add throttle behaviour
ServiceThrottlingBehavior throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttle == null)

    throttle = new ServiceThrottlingBehavior();

    throttle.MaxConcurrentCalls = _SymphonyHostProp.MaxConcurrentCalls;     // 200;         // default 16       // 64
    throttle.MaxConcurrentInstances = _SymphonyHostProp.MaxConcurrentInstances; // 1000;     // default 26       // 464
    throttle.MaxConcurrentSessions = _SymphonyHostProp.MaxConcurrentCalls;      // default 10       // 400

    host.Description.Behaviors.Add(throttle);


// open the host - bring it into life!
host.Open();

我有客户端使用这样的 nettcpbinding 连接到此主机

int MaxBuffer = 64;     // 64 Mb
int bufferSize = MaxBuffer * 1024 * 1024;   // 67108864               

// -----------------------------------------
// binding for normal clients (default) 
// -----------------------------------------
_tcpBinding = new NetTcpBinding(SecurityMode.None, true);
_tcpBinding.MaxBufferPoolSize = bufferSize; // 64 Mb
_tcpBinding.MaxBufferSize = bufferSize;
_tcpBinding.MaxReceivedMessageSize = bufferSize;
_tcpBinding.TransferMode = TransferMode.Buffered;
_tcpBinding.ReaderQuotas.MaxArrayLength = bufferSize;
_tcpBinding.ReaderQuotas.MaxBytesPerRead = bufferSize;
_tcpBinding.ReaderQuotas.MaxStringContentLength = bufferSize;
_tcpBinding.MaxConnections = 100;
//_tcpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
_tcpBinding.SendTimeout = MyHost.SendTimeout;
_tcpBinding.ReliableSession.Enabled = true;
//tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(7, 0, 0, 0);    // 7 days

“数据”公开对象的定义如下:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple,
UseSynchronizationContext = false)]  
public class Data : HBase, IData

问题是,在主机正常运行期间,突然连接了大约 50 个用户,IDE 中的输出窗口显示 数百个 与 wcf 相关的错误。

然后,如果我幸运的话,它会在几秒钟后自行消失,但在此期间,整个 UI 和宿主应用程序的所有内容都会冻结,WCF 不再为任何其他用户提供服务。然后当它回来时一切都很好,直到它再次开始出错..

这里似乎没有任何内部网络问题。我已经启用了跟踪,但我在其中找不到太多有用的信息。 我只是想知道是否有人经历过这种行为?

如果需要,我很乐意提供更多信息!

在我的输出窗口中创建了 1000 个这些错误。

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.ServiceModel.CommunicationException' occurred in System.ServiceModel.dll

【问题讨论】:

【参考方案1】:

第一次机会异常不是错误。这些通常是会被捕获的异常。你应该忽略它们。

您应该担心的异常是未被捕获的异常。

【讨论】:

我知道我可以忽略它们,但是一旦这些错误开始出现(以它们的 1000 秒),主机就会变慢,冻结 UI,最终有时会崩溃。任何人都可以对可能发生的事情有所了解吗? 什么都没有发生。您假设这些与您的问题有关。他们可能不会。你已经开始研究香肠是如何制作的,但你不是很喜欢它。 如果您认为这些意味着什么,那么我建议您打开 WCF 跟踪,看看您是否可以获得有关正在发生的事情的更多详细信息。另外,用 Fiddler 之类的看一下网络。【参考方案2】:

我最近遇到了这个问题。在我的情况下,我使用的是自定义 log4net WCF Appender,并且 log4net 库旨在永远不会抛出未捕获的异常,这就是我在调试窗口中获得 A first chance exception 的原因。我只需将进行调用的客户端和服务本身封装在 try catch 块中,这样我就可以在 log4net 之前拦截异常,这让我能够找到真正的原因。

虽然您的异常似乎源于不同的问题,但解决方案仍然相同。获取这些尝试/追赶信息,以便获取完整的错误详细信息并确定问题的根源。

顺便说一句,如果您的整个应用程序崩溃了,您可以使用一个非常方便的技巧来获取导致它崩溃的未捕获异常。只需在应用程序启动后立即调用它,

AppDomain.Current.UnhandledException += (sender, error) => File.AppendAllText("Fatal.log", error.ExceptionObject.ToString() + "\r\n");

【讨论】:

以上是关于WCF - 抛出许多许多第一次机会异常 - 然后使应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

WCF 错误和异常

Jvm(49),指令集----异常处理指令

在IIS上抛出异常的WCF托管(没有权限和HTTP错误404.3 - 未找到)

从 WCF 抛出异常

为啥 WCF/Mongo 抛出异常:已添加具有相同键的项目

如何指定接口的实现者抛出的异常?