title: Thread safety for OrganizationServiceProxy and generated xrm service context
body: <p>This is a best practices question.
   Per this <a href="http://msdn.microsoft.com/en-us/library/gg509027.aspx#Performance" rel="nofollow">best practices article</a> and per MSDN, the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> is not thread safe.</p>
  
  <p>If you have a multi threaded client application in which you are creating an instance of an
   <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.aspx" rel="nofollow"><code>OrganizationServiceContext</code></a> (on a per thread basis), the constructor of which accepts an
   <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.iorganizationservice.aspx" rel="nofollow"><code>IOrganizationService</code></a> instance and you pass in a global instance of the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> 
  (i.e a static instance allocated once at the "process level"), will this cause threading issues and/or if the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a>  instance faults, will it affect operations that the threads try to perform on their own "local" instance of the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.aspx" rel="nofollow"><code>OrganizationServiceContext</code></a>?</p>
  
  <p>My belief is that it will, and that an <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> instance needs to be created on a "per thread" basis and that each <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationservicecontext.aspx" rel="nofollow"><code>OrganizationServiceContext</code></a> in a multi threaded application would need its <strong>own corresponding</strong> <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> instance. </p>
  
  <p>I'm posting this to get confirmation of the above.</p>
  
  <p>Also, the article indicates</p>
  
  <blockquote>
    <p>The service proxy class performs the metadata download and user authentication by using the following class methods</p>
  </blockquote>
  
  <pre class="lang-cs prettyprint-override"><code>IServiceManagement&lt;IOrganizationService&gt; orgServiceManagement =
                   ServiceConfigurationFactory.CreateManagement&lt;IOrganizationService&gt;(
                   new Uri(organizationUrl))
  
  AuthenticationCredentials authCredentials = orgServiceManagement.Authenticate(credentials)
  </code></pre>
  
  <blockquote>
    <p><em>By caching the service management and authenticated credential objects, your application can more efficiently construct the service proxy objects more than one time per application session</em></p>
  </blockquote>
  
  <p>If I try to execute the above API calls manually, in Active directory authentication mode, the <code>authCredentials.SecurityTokenResponse</code> is null as <a href="http://msdn.microsoft.com/en-us/library/gg307068.aspx" rel="nofollow">indicated by MSDN</a></p>
  
  <p>Is there a way to perform the authentication just once for AD mode and pass an <strong>authenticated</strong> <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.authenticationcredentials.securitytokenresponse.aspx" rel="nofollow"><code>SecurityTokenResponse</code></a> to a newly created <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> via the following constructor?</p>
  
  <pre class="lang-cs prettyprint-override"><code>OrganizationServiceProxy (IServiceConfiguration, SecurityTokenResponse)
  </code></pre>
  
  <p>so that you don't have to take the authentication and metadata download hit on a "per thread basis" when constructing the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.client.organizationserviceproxy.aspx" rel="nofollow"><code>OrganizationServiceProxy</code></a> instance per thread and just take the hit once?</p>