title: Parallelization in WCF
body: <p>I create WCF service with Spring.NET framework. This service is math service
  and provide some computations for client apps.</p>
  
  <p>I have question about WCF service parallelization on multi-core server. For simple
  example I have server with 20 cores.</p>
  
  <p>First here is a simplified code.</p>
  
  <pre><code>//WS interface
  public interface IMatlabService
  {
      List&lt;ResultData&gt; Calculate(byte [] data);
  }
  
  //WS class definition
  [ServiceBehavior(Namespace = "http://server.com/MatlabService")]
  public class MatlabService: IMatlabService
  {
      public IMatlabManager MatlabManager{get;set:}
  
      //web metod for math computations
      public List&lt;ResultData&gt; Calculate(byte [] data)
      {
          var result = new List&lt;ResultData&gt;();
  
          //do math work in another thread
          Task&lt;List&lt;ResultData&gt;&gt; task = Task.Factory.StartNew&lt;List&lt;ResultData&gt;&gt;(() =&gt;
                                                             {
                                                                 return MatlabManager.CalculateWithFiniteElementMethod(data);
                                                             });
  
          result.AddRange(task.Result)
  
          return result;
  
      }
  }
  
  public interface IMatlabManager 
  {
      List&lt;ResultData&gt; CalculateWithFiniteElementMethod(byte [] data);
  }
  
  public class MatlabManager : IMatlabManager 
  {
      public List&lt;ResultData&gt; CalculateWithFiniteElementMethod(byte [] data)
      {
          // do some math work
      }
  }
  </code></pre>
  
  <p>With Spring.NET I configure web service and manager class as not singleton.</p>
  
  <p><strong>Spring.NET XML configuration is here.</strong></p>
  
  <p>Matlab manager configuration:</p>
  
  <pre><code>  &lt;object name="matlabManager"
            type="MatlabManager"
            singleton="false"/&gt;
  </code></pre>
  
  <p>MatlabService configuration:</p>
  
  <pre><code>  &lt;object name="matlabService"
            type="MatlabService"
            singleton="false"&gt;
         &lt;property name="MatlabManager" ref="matlabManager"/&gt;
    &lt;/object&gt;
  </code></pre>
  
  <p><strong>WCF service configuration from web.config</strong></p>
  
  <pre><code> &lt;behavior name="Behavior1"&gt;
       &lt;serviceMetadata httpGetEnabled="true"/&gt;
       &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
    &lt;/behavior&gt;
  
  &lt;services&gt;
    &lt;service name="matlabService" 
              behaviorConfiguration="Behavior1"&gt;
      &lt;endpoint address="" 
              binding="basicHttpBinding" 
              contract="IMatlabService" 
              bindingNamespace="http://server.com/MatlabService"/&gt;
      &lt;endpoint contract="IMetadataExchange" 
              binding="mexHttpBinding" 
              address="mex"/&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p><strong>SVC file.</strong></p>
  
  <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true" Service="MatlabServiceService" Factory="Spring.ServiceModel.Activation.ServiceHostFactory" %&gt;
  </code></pre>
  
  <ol>
  <li><p>I believe for each client web metod call is created new instance of
  MatlabService and WCF service work  is do on new thread (WCF service
  thread) and OS assign this thread to CPU core.</p>
  
  <p>Or I am wrong and behavior create new service object per call I must
  define in ServiceBehavior property InstanceContextMode?</p>
  
  <p><strong>[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]</strong></p></li>
  <li><p>In web method Calculate of MatlabService I use
  System.Threading.Tasks for parallelization so math work is do in
  another thread (Math thread).</p>
  
  <p>For each call is created WCF service thread and in WCF service
  thread is created Math thread.</p>
  
  <p>I am not sure if this is true.</p>
  
  <p>Maybe is needed allow multi-threading in WCF service in
  ConcurrencyMode?</p>
  
  <p><strong>[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode
      = ConcurrencyMode.Multiple)]</strong></p></li>
  </ol>
  
  <p>I would like heard ideas how can parallelize web metod calls for multi core CPU. 
  I google it but not find any clear and usefull for me because I use Spring.NET framework for creation WCF service.</p>