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

托管在windows服务中的c#wcf在5分钟后空闲,c#,.net,wcf,C#,.net,Wcf,我在Windows服务中托管了一个WCF服务。(因此它不是在IIS中托管的,而是自托管的) 该应用程序使通过HTTP接收测量成为可能。这些文件当前正在写入txt文件 我还配置了一个每天在特定时间运行的任务 我遇到的问题是,当我至少5分钟没有通过HTTP接收到测量值时,服务似乎处于空闲状态 我注意到5分钟后,服务需要30秒才能响应。30秒后,如果我通过http发送测量值,我会得到一个快速响应 有什么问题吗 app.config <?xml version="1.0"?> <con

我在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>

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

那么原因和解决办法呢

我还确认此修复对我们的案例有帮助。

引述:

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


你能发布你服务的配置文件吗?不管是.config文件还是设置服务的c#代码?@Micky我添加了app.config文件。服务的托管地?IIS?如果是,是哪个版本(或Windows Server版本)?它是包含在Windows服务中的自托管WCF。
 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);
    }
}