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

时间:2015-10-28 11:30:59

标签: c# .net wcf

我在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 个答案:

答案 0 :(得分:3)

看起来我们也有这个问题。我们有很多自托管服务。如果一段时间(约1分钟)服务没有收到任何请求,它就会变为空闲,下一次呼叫真的很慢。

原因和解决方案described here

我也确认此修复程序在我们的案例中有所帮助。

从那里引用:

  

我已与我们的开发人员确认这是一个已知问题。原因确实与ThreadPool和线程超时有关。   一种可能的解决方法是在短时间间隔内调用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);
    }
}