简体   繁体   English

WCF“自托管”应用程序变得无法响应

[英]WCF “Self-Hosted” application becomes unresponsive

We have a C# (.Net 4.0) console application that "self hosts" two WCFs services: one used WSHttpBinding , and another uses BasicHttpBinding . 我们有一个C#(。Net 4.0)控制台应用程序,它“自托管”两个WCF服务:一个使用WSHttpBinding ,另一个使用BasicHttpBinding

Connecting to these services, we have two separate client applications: a Silverlight-based service that uses the BasicHttpBinding , and another console app that uses the WSHttpBinding . 连接到这些服务,我们有两个独立的客户端应用程序:使用BasicHttpBinding的基于Silverlight的服务,以及使用WSHttpBinding另一个控制台应用程序。

The WCF service app usually has around 30 users connected via the Silverlight client, and another couple of connections from the console application client. WCF服务应用程序通常有大约30个用户通过Silverlight客户端连接,另外几个连接来自控制台应用程序客户端。 It's not "flat out" by any means; 它无论如何都不是“平坦的”; each client queries the WCF service maybe once every 5 seconds at the very most. 每个客户端最多可以每5秒查询一次WCF服务。

The problem is: intermittently the service application becomes unresponsive. 问题是:间歇性地使服务应用程序无响应。 Although the server itself continues to run (it continues to write to a log file), all WCF activity (on both ServiceHost s) appear to "seize". 虽然服务器本身继续运行(它继续写入日志文件),但所有WCF活动(在两个ServiceHost )都显示为“占用”。 New requests aren't processed (although the TCP connections are accepted). 不处理新请求(尽管接受TCP连接)。 Also, the number of threads consumed by the application starts to ramp up dramatically, at the rate of around one new thread per second. 此外,应用程序消耗的线程数开始急剧增加,速度大约为每秒一个新线程。 The code itself doesn't do anything with Thread s or ThreadPool s, although it occasionally will issue a Thread.Sleep for a few hundred milliseconds. 代码本身对ThreadThreadPool没有任何作用,尽管它偶尔会发出一个Thread.Sleep几百毫秒。

The frustrating thing is the intermittent nature of the problem: the code regularly runs for hours, even days without any issues. 令人沮丧的是问题的间歇性:代码定期运行数小时,甚至几天没有任何问题。 Then, with no apparent cause, it suddenly becomes unresponsive and the Thread count starts ticking up. 然后,没有明显的原因,它突然变得没有响应,线程计数开始滴答作响。

I've tried simulating user activity - connecting and disconnecting clients, "swamping" the service with requests - but I can do nothing to reproduce the fault. 我已经尝试模拟用户活动 - 连接和断开客户端,“淹没”服务请求 - 但我无法重现故障。

Just in case the issue was WCF Throttling, I've added this code: 如果问题是WCF Throttling,我已添加以下代码:

 ServiceThrottlingBehavior throttlingBehavior = new System.ServiceModel.Description.ServiceThrottlingBehavior
                                                           {
                                                               MaxConcurrentCalls = 512,
                                                               MaxConcurrentInstances = 8192,
                                                               MaxConcurrentSessions = 8192
                                                           };

        host.Description.Behaviors.Add(throttlingBehavior);
        host2.Description.Behaviors.Add(throttlingBehavior);

.. with no apparent effect. ..没有明显的效果。

I've put extensive logging in the code to try and determine what it is that triggers this behaviour - logging each call to each method - but nothing has appeared as a result. 我已经在代码中进行了大量的日志记录,以尝试确定触发此行为的内容 - 将每个调用记录到每个方法 - 但结果没有出现任何内容。 I've wrapped everything in try ... catch blocks and spitting any exceptions to the log file, to see if something's falling over somewhere, and also trapped the UnhandledException s in a similar fashion ... but again, nothing appears to be going wrong. 我把所有东西都包装在try ... catch块中并将任何异常吐出到日志文件中,以查看某些东西是否落在某处,并且还以类似的方式捕获了UnhandledException ......但是,似乎没有任何东西可以进行错误。

Does the above behaviour sound familiar to anyone, or can anyone suggest on the best way forward to troubleshoot this issue? 上述行为对任何人来说都是熟悉的,或者有人可以建议最好的方法来解决这个问题吗?

EDIT: following Wal's advice below, I've captured a .DMP of the application when it starts mis-behaving, and looking at the Parallel Stacks view in VS2012, I see: 编辑:按照下面Wal的建议,当它开始出现错误时,我已经捕获了应用程序的.DMP,并查看VS2012中的Parallel Stacks视图,我看到:

在此输入图像描述在此输入图像描述

... and others very similar but with different numbers of threads. ......和其他非常相似但线程数不同的人。 I'm not clever enough to decode exactly what this means.. can anyone suggest where to start looking next? 我不够巧妙地解释这意味着什么..谁能建议从哪里开始寻找下一个?

what is the concurrencymode for the service? 什么是服务的并发模式? and the instancecontextmode? 和instancecontextmode?

the default instancecontextmode is per session, it may be worth changing this to percall, this will use more memory but will ensure that each service instance is no hanging around (provided the client is correctly disposed of http://coding.abel.nu/2012/02/using-and-disposing-of-wcf-clients/ ) 默认的instancecontextmode是每个会话,可能值得将此更改为percall,这将使用更多的内存,但将确保每个服务实例不会挂起(假设客户端正确处理http://coding.abel.nu/ 2012/02 /使用和处置wcf客户端/

As pointed out sooner, sounds like you have a race condition. 正如你所指出的那样,听起来像你有竞争条件。 Aren't you by any chance checking System.ServiceModel.ICommunicationObject.State of the connection somewhere in the code? 您是否有机会检查代码中某处连接的System.ServiceModel.ICommunicationObject.State? See MSDN article : 请参阅MSDN文章

Checking the value of the System.ServiceModel.ICommunicationObject.State property is 
a race condition and is not recommended to determine whether to reuse or close a channel.

Thank you to everyone who's commented and answered; 感谢所有评论和回答的人; your suggestions and input have really helped - not least to confirm that it doesn't seem to be something trivial I've missed. 你的建议和意见确实有所帮助 - 尤其是确认它似乎并不是我错过的微不足道的东西。

However, and slightly frustratingly, the problem appears to have gone away. 然而,有点令人沮丧的是,这个问题似乎已经消失了。 Here's what I've changed: 这是我改变的:

  • The application was writing to the console regularly (my "WriteToLog" method had Console.WriteLine as well as appending to a file; this was purely for my own convenience during development). 应用程序定期写入控制台(我的“WriteToLog”方法具有Console.WriteLine以及附加到文件;这在开发期间纯粹是为了我自己的方便)。 The application was also running as a service using FireDaemon, and for some reason we started to see high CPU time on conhost.exe . 该应用程序也使用FireDaemon作为服务运行,由于某种原因,我们开始在conhost.exe上看到高CPU时间。 So to counteract that, I've commented out the Console.WriteLine . 所以为了抵消这种情况,我已经注释掉了Console.WriteLine

  • Due to the high CPU, we also increased the performance of the Virtual Machine the code was running on by throwing a few more cores at it. 由于高CPU,我们还增加了运行代码的虚拟机的性能,方法是再添加几个内核。

As a result, the application is now much "quieter" in terms of CPU usage. 因此,就CPU使用率而言,应用程序现在更加“安静”。 As others have mentioned, there's almost certainly a "race condition" in the code somewhere, but by making the underlying machine faster and the code more efficient, it almost seems as if I've decreased the chances of the race condition occurring. 正如其他人所提到的那样,代码中的某些地方几乎肯定存在“竞争条件”,但通过使底层机器更快并且代码更高效,几乎可以看出我已经减少了竞争条件发生的可能性。 Certainly, the problem that was occurring at least once a day hasn't happened in nearly a week. 当然,至少每天发生一次的问题在近一周内没有发生。

Just to be sure, I've gone through the code and made sure each shared object is wrapped in a Lock() where there's any possibility of it being modified by another thread - even though I'm not doing any explicit threading, I'm assuming that the WCF mechanism will automatically do that and there's the potential for an incoming request to try and modify an object whilst something else is chewing on it. 只是为了确定,我已经完成了代码并确保每个共享对象都包含在一个Lock() ,其中有任何可能被另一个线程修改 - 即使我没有做任何显式线程,我假设WCF机制会自动执行此操作,并且可能会有传入请求尝试修改对象,而其他东西正在咀嚼它。 I would have expected some kind of concurrency exception if that happened though? 如果发生这种情况,我本来期望某种并发异常?

Thanks again for the help, and here's hoping the code doesn't fall over just after I click the Post Your Answer button :/ 再次感谢您的帮助,这里希望在我点击“ Post Your Answer按钮后代码不会Post Your Answer :/

May be its a threading issue which has nothing to do with WCF - as mentioned in previous posts, lock statements may be the suspect - your application (WCF part or not), may have spun up threads, which cannot exit due to locking issue. 可能是它与WCF无关的线程问题 - 正如之前的帖子中所提到的,锁定语句可能是嫌疑人 - 您的应用程序(WCF部分与否),可能已经启动线程,由于锁定问题而无法退出。

On the other hand, it could be WCF, did you receive a lot of hit on your WCF service? 另一方面,它可能是WCF,你收到了很多WCF服务吗? Try throttle it. 试试节流。 http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicethrottlingbehavior.maxconcurrentinstances.aspx http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicethrottlingbehavior.maxconcurrentinstances.aspx

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

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