title: Slow WCF: 500ms for "void Test()"
body: <p>I have a WCF service hosted by a Windows Service. It works but calling it is very slow: A simple function like <code>void Test()</code> takes around 500 ms from client call to server receive.</p>
  
  <p>I tried several different configurations but haven't succeeded in making that faster. Both client and server are on the same machine.</p>
  
  <p>Here is the code:</p>
  
  <p><code>Shared.dll</code>:</p>
  
  <pre><code>[ServiceContract]
  public interface IContract
  {
      [OperationContract]
      void Test();
  }
  </code></pre>
  
  <p><code>Server.exe</code>:</p>
  
  <pre><code>public class Service : IContract
  {
      public void Test()
      {
          this.Log("Test: " + DateTime.Now.TimeOfDay);
      }
  }
  </code></pre>
  
  <p><code>Client.exe</code>:</p>
  
  <pre><code>var binding = ...;
  var factory = new ChannelFactory&lt;IContract&gt;(binding, "net.tcp://localhost/Service");
  var service = factory.CreateChannel();
  this.Log("Test: " + DateTime.Now.TimeOfDay);
  service.Test();
  </code></pre>
  
  <p><code>app.config</code>:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;system.serviceModel&gt;
    &lt;services&gt;
      &lt;service behaviorConfiguration="ServiceBehavior" name="Server.Service"&gt;
        &lt;endpoint address="Service" binding="netTcpBinding" bindingConfiguration="NetTcp" contract="Shared.IContract"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;host&gt;
          &lt;baseAddresses&gt;
            &lt;add baseAddress="net.tcp://localhost/" /&gt;
          &lt;/baseAddresses&gt;
        &lt;/host&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;bindings&gt;
      &lt;netTcpBinding&gt;
        &lt;binding name="NetTcp" portSharingEnabled="true"&gt;
          &lt;security mode="None"&gt;
            &lt;message clientCredentialType="None"/&gt;
            &lt;transport protectionLevel="None" clientCredentialType="None"/&gt;
          &lt;/security&gt;
          &lt;reliableSession enabled="false" /&gt;
        &lt;/binding&gt;
      &lt;/netTcpBinding&gt;
    &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="ServiceBehavior"&gt;
          &lt;serviceMetadata httpGetEnabled="false" /&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="false" /&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <h2>Edit</h2>
  
  <p>I am currently testing this with client and server in the same machine but the idea is to have them in different machine for production.</p>
  
  <p>The factory and channel creations are not the culprit here. I ruled that out putting a <code>Thread.Sleep(20000)</code> between the creation and the log and got the same result.</p>
  
  <p>The difference between the client log and the server log is around 500 ms for the first call (actually, it's anywhere between 300 ms and 1 second) but then, it lasts less than 5 ms for any more call to <code>Test()</code>. I want my function to be always very fast, including the first call. How can I do that?</p>