WCF连接重置:需要重启服务器

时间:2016-04-26 16:20:58

标签: c# wcf windows-server-2003

此问题与Windows Server 2003上的Windows服务中托管的WCF有关。

问题发生在正确工作几天后,只能通过重新启动服务器来修复。

C#代码在其日志

中没有捕获任何异常
serviceHost.Opening += new EventHandler(serviceHost_Opening);
serviceHost.Opened += new EventHandler(serviceHost_Opened);
serviceHost.Closing += new EventHandler(serviceHost_Closing);
serviceHost.Closed += new EventHandler(serviceHost_Closed);
serviceHost.Faulted += new EventHandler(serviceHost_Faulted);
serviceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(serviceHost_UnknownMessageReceived);

serviceHost.Open();

处理程序的实现方式如下:

 void serviceHost_Opened(object sender, EventArgs e)
{ 
    CentralReport.MyService.SrvLog("WinCentralRpt",String.Format("service opened by sender: {0}", sender.GetType().ToString()));
}

void serviceHost_Opening(object sender, EventArgs e)
{
    CentralReport.MyService.SrvLog("WinCentralRpt",String.Format("service opening by sender: {0}", sender.GetType().ToString()));
}    

public static void SrvLog(string user, string line) {
    string log_path = System.Configuration.ConfigurationManager.AppSettings["srv_log"];
    if (log_path != null) {
        using (System.IO.StreamWriter logSW = new System.IO.StreamWriter(
            log_path.Replace("{user}",user.ToLower()),true)) {
            logSW.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt", CultureInfo.InvariantCulture) + ": " + line);
        }   
    }
}

在客户端,C#WCF使用者在调用操作后正确关闭连接。

已激活最高级别跟踪。

   <system.diagnostics>
      <sources>
            <source name="System.ServiceModel" 
                        switchValue="All"
                        propagateActivity="true">
                <listeners>
                        <add name="xml" />
                </listeners>
             </source>
            <source name="CardSpace"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
            <source name="System.IO.Log"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
            <source name="System.Runtime.Serialization"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
            <source name="System.IdentityModel"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
            <source name="System.ServiceModel.MessageLogging"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
            <source name="System.ServiceModel.Activation"
                        switchValue="All">
                <listeners>
                    <add name="xml" />
                </listeners>
            </source>
      </sources>

        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="C:\MyPath\Traces.svclog" />
        </sharedListeners>
<trace autoflush="true" />
   </system.diagnostics> 

    <system.serviceModel>
  <diagnostics wmiProviderEnabled="true">
      <messageLogging 
           logEntireMessage="true" 
           logMalformedMessages="true"
           logMessagesAtServiceLevel="true" 
           logMessagesAtTransportLevel="true"
           maxMessagesToLog="3000" 
       />
  </diagnostics>

    <serviceBehaviors>
        <behavior name="MyServiceBehavior">
            <dataContractSerializer maxItemsInObjectGraph="6553500"/>
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
    </serviceBehaviors>

操作系统检查似乎很好

netstat -ao | grep 9nnn
tasklist /SVC | grep Opxxx

我的感觉是频道监听器无法正常工作,但跟踪日志中没有任何问题。 服务器只是停止响应此WCF的请求,但它从那一刻开始就不向日志写入任何内容。 从视觉角度来看,这是无响应和无效的图形。

non-responding - vs - enter image description here

有人建议比重启服务器更软的解决方案吗?显然,已经尝试重启主机服务(以及其他系统服务,如dns和ipsec),甚至重新安装了WCF。只有重新启动服务器才能解决问题。

知道可能是什么原因?

编辑 - 重启后

重新启动后 - 没有任何其他更改 - WCF再次开始工作。

旁注

还有一点我不完全明白。 现在正在运行 - 当一切正常时 - 命令

httpcfg.exe query urlacl 

这是Windows 2003版本的

netsh http show urlacl

我原本希望看到WCF网址,但输出只是(为什么?)

C:\>httpcfg.exe query urlacl
    URL : http://+:80/Temporary_Listen_Addresses/
    ACL : D:(A;;GX;;;WD)
-------------------------------------------------------

4 个答案:

答案 0 :(得分:1)

这是旧版.Net 4.0版本的WCF ThreadPool中的一个错误。 您可以找到一些参考文献herehere

您应该将服务帐户从本地系统移动到本地服务。这是用于保留本地服务帐户的URL的Windows 2003命令。

httpcfg set urlacl /u http://+:9nnn/Your/Url/ /a "D:(A;;GX;;;LS)"

此外,您只需启用WCF性能计数器并关闭WCF跟踪。

<system.serviceModel>
  <diagnostics performanceCounters="All" />
  ...

它们将显示在性能监视器的Service.ModelService类别下。

总之,使用单独的进程或使用不同的(非WCF)工作服务替换WCF服务中的线程操作(如数据库访问等)。

相关问题

全世界都记录了类似的问题

Technical Bulletin:运行高负载后,WCF服务可能无响应

  

如果症状适用,请重新启动托管无响应的服务器   服务。

这里有一个有趣的msdn建议

故障排除:Connection Forcibly Closed

原因:

  

网络硬件故障正在丢弃部分TCP流量

  

SynAttackProtect设置可能会丢弃连接。

查看以下注册表可能会很有用

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

以下article提到了两个原因:

  • 操作系统内存不足用于TCP缓冲区
  • 操作系统耗尽了可用的TCP“临时”端口(KB Microsoft,指的是上述注册表tcp/ip parameters

答案 1 :(得分:1)

潜在问题

这是非分页池资源问题。

无响应的WCF只是一个早鸟信息,在此之前发送。

实际上,以下是内存泄漏的典型症状

  

你仍然可以ping服务器,它仍然处理一些基本的   命令,如关机命令,但不允许浏览   文件或文件夹,你不能RDP到服务器。

您有几种工具可以监控它:

  • 流程资源管理器
  • Poolmon的

PearmanRussinovich阅读这两篇文章,以获取完整的指导。

旁注

您还可以查看\System32\LogFiles\HTTPERR下的http日志。

可能会应用hotfix

答案 2 :(得分:0)

我以前在使用过Web服务的旧版Windows上使用MemoryStream和Binary Writers时遇到了奇怪的问题。没有什么比这更有意义了,不知道它是硬件限制还是一些奇怪的一次性.Net错误。也许尝试将Stream拉出使用块并使用Try-Finally情况处理它。在过去与二进制编写器的问题,我将不得不自己处理它,它工作正常。几乎像“使用”块试图在它完成之前关闭所有东西。

答案 3 :(得分:0)

您是否正在使用服务中的服务? 请记住在消耗服务的任何地方关闭连接(即使在服务器端)。

在WCF中,您可能知道存在ChannelFactory,该工厂为您提供服务代理,但它也是ICommunicationObject,您必须配置该服务代理实例以关闭连接。 / p>

使用指令PerRequest检查ServiceHost销毁,以监控是否允许处置服务主机实例,以及是否存在代码的其他部分中存在的硬引用,而不允许使用Gcollected。

我认为您可能已经计算出最大连接数。

相关问题