windows服务中托管的c#wcf在5分钟后变为空闲

本文关键字:5分钟 c#wcf 服务 windows | 更新日期: 2023-09-27 18:19:39

我在Windows服务中托管了一个WCF服务。(所以它不在IIS中托管,它是自托管的)

该应用程序使通过HTTP接收测量成为可能。这些文件目前正在写入一个txt文件。

我还配置了一个任务,每天在特定时间运行。

我遇到的问题是,当我在至少5分钟内没有通过HTTP接收到测量结果时,服务似乎处于空闲状态。

我注意到5分钟后,服务需要30秒才能做出响应。30秒后,如果我通过http发送测量结果,我会得到快速响应。

可能是什么问题?

app.config

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="Applicatie.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="LargeWeb" maxBufferSize="1500000" maxBufferPoolSize="1500000"
          maxReceivedMessageSize="2000000000" transferMode="Streamed">
          <readerQuotas maxDepth="32" maxStringContentLength="656000" maxArrayLength="656000"
            maxBytesPerRead="656000" maxNameTableCharCount="656000" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="Applicatie.Applicatie" behaviorConfiguration="Proxy">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5001/Applicatie"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="LargeWeb" behaviorConfiguration="webBehavior" contract="Applicatie.IApplicatie"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
          <CorsSupport />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Proxy">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <!--<providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>-->
  </entityFramework>
</configuration>

windows服务中托管的c#wcf在5分钟后变为空闲

这似乎也是我们遇到的问题。我们有许多自托管服务。若有一段时间(约1分钟)服务没有收到任何请求,它就会变为空闲状态,并且下一次呼叫的速度非常慢。

所以这里介绍的原因和解决方案

我还确认此修复程序对我们的情况有所帮助

报价:

我已经向我们的开发人员确认,这是一个已知的问题。原因确实与线程池和线程超时有关。一种可能的解决方法是在短时间间隔内调用ThreadPool.UnsafeQueueNativeOverlapped方法以保持线程可用。

 class Program
{
    static void Main(string[] args)
    {
        using (var host = new ServiceHost(typeof(Service1)))
        {
            ThreadPool.QueueUserWorkItem(
                new WaitCallback(delegate
            {
                while (true)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(100));
                    QueueDummyIOCPWork();
                }
            }));
            host.Open();
            Console.WriteLine("Service running...");
            Console.ReadKey(false);
        }
    }
    private static unsafe void QueueDummyIOCPWork()
    {
        Overlapped ovl = new Overlapped();
        NativeOverlapped* pOvl = null;
        pOvl = ovl.Pack((a, b, c) => { Overlapped.Unpack(pOvl); Overlapped.Free(pOvl); }, null);
        ThreadPool.UnsafeQueueNativeOverlapped(pOvl);
    }
}