title: Why is the first WCF client call slow?
body: <p>I am trying to figure out why the first WCF call after client application start takes much more time comparing to second one.</p>
  
  <p>What I did to test that:</p>
  
  <ol>
  <li>Implemented simple self hosted WCF Server and console client.</li>
  <li><strong>Server IS warmed up</strong> - I run it and call method several times before running test.</li>
  <li>Binding is <code>basicHttpBinding</code> to reduce network and security overhead.</li>
  <li>Testing scenario - start console client app, making two identical WCF service calls in a row. </li>
  </ol>
  
  <p>In my tests I see ~700 milliseconds for first call and ~3 milliseconds for second call.</p>
  
  <p>Almost a second seems to be too much time for JIT compiler. I would accept if that time is used to initialize some complicated infrastructure like <code>ObjectContext</code> in Entity Framework but my code is very simple and proxy classes are already compiled.</p>
  
  <p>I also tried <code>netNamedPipeBinding</code> binding. Result proves pattern - first call takes ~800 ms, second call takes ~8 ms.</p>
  
  <p>Will appreciate if anybody can explain why the first service call takes so much time. </p>
  
  <p>Tested in Win 7 64 bit.</p>
  
  <p>My implementation is below.</p>
  
  <p>Contract:</p>
  
  <pre><code>[ServiceContract]
  public interface ICounter
  {
          [OperationContract]
          int Add(int num);
  }
  </code></pre>
  
  <p>Service Implementation:</p>
  
  <pre><code>public class CounterService: ICounter
  {
          private int _value = 0;
  
          public int Add(int num)
          {
              _value += num;
              Console.WriteLine("Method Add called with argument {0}. Method  returned {1}", num, _value);
              return _value;
          }
  }
  </code></pre>
  
  <p>Server Implementation:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Uri baseAddress = new Uri("http://localhost:8080/Service");
  
          // Create the ServiceHost.
          using (ServiceHost host = new ServiceHost(typeof(CounterService), baseAddress))
          {
              host.Open();
  
              Console.WriteLine("The service is ready at {0}", baseAddress);
              Console.WriteLine("Press &lt;Enter&gt; to stop the service.");
              Console.ReadLine();
  
              // Close the ServiceHost.
              host.Close();
          }
      }
  }
  </code></pre>
  
  <p>Server Configuration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="Server.CounterService"&gt;
          &lt;endpoint address="base" binding="basicHttpBinding" name="baseDefault"
            contract="Contract.ICounter" /&gt;
          &lt;endpoint address="net.pipe://localhost/Service/netNamedPipe"
            binding="netNamedPipeBinding" name="netNamedPipeDefault" contract="Contract.ICounter" /&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=""&gt;
            &lt;serviceMetadata httpGetEnabled="true" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Client Implementation (<code>CounterProxy</code> is generated from service reference):</p>
  
  <pre><code>Stopwatch stopWatch = new Stopwatch();
  stopWatch.Start();
  
  using (var proxy = new CounterProxy.CounterClient(_endpointConfigurationName))
  {
      output = proxy.Add(1);
  }
  
  stopWatch.Stop();
  // Get the elapsed time as a TimeSpan value.
  TimeSpan ts = stopWatch.Elapsed;
  </code></pre>
  
  <p>Function that contains that code called two times in a row.</p>
  
  <p>Client Configuration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;client&gt;
        &lt;endpoint address="http://localhost:8080/Service/base" binding="basicHttpBinding"
            contract="CounterProxy.ICounter"
            name="baseDefault" /&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>