title: TermStore Count is 0
body: <p><strong>Overview</strong>
  I am observing a really strange behaviour with code which attempts to retrieve the TermStore from the TaxonomySession. The code looks like this:</p>
  
  <pre><code>            using (SPSite mySite = new SPSite(url))
              {
              TaxonomySession session = new TaxonomySession(mySite);
  
              // Get all the TermStores associated with the provided site.
              TermStoreCollection stores = session.TermStores;
              Console.WriteLine(stores.Count);
  </code></pre>
  
  <p>Here is the issue:</p>
  
  <ol>
  <li>The above code does not return any TermStores, i.e. the count is zero</li>
  <li><p>If I run the following PowerShell script from the same command prompt, I get 1 TermStore in the count.</p>
  
  <pre><code>$site = new-object Microsoft.SharePoint.SPSite("https://mysite") 
  $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($site) 
  $session.TermStores.Count 
  </code></pre></li>
  </ol>
  
  <p>I have tried the following:</p>
  
  <ol>
  <li>My Managed Metadata Application Proxy is a part of the default proxy group</li>
  <li>The administrator for the Managed Metadata Service is configured correctly</li>
  <li>The permissions for the Managed Metadata Service are configured correctly</li>
  <li>The Managed Metadata application pool is running under the farm account</li>
  <li>Modifying the code above to run under elevated privileges</li>
  </ol>
  
  <p>The weird thing is that the first block of code above DOES NOT retrieve any TermStore(s) when compiled as an executable and invoked from a command window, which is running under the farm credentials. IN THE SAME window, I can copy and paste the second block of the equivalent PowerShell scripts and have it run!</p>
  
  <p>Also, this issue is only happening on our Production environment. The same executable ran successfully on our DEV, TEST and Pre Prod environments.</p>
  
  <p>Any help at this stage will be much appreciated.</p>