title: Runtime exception with .NET 4.5 build (Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib)
body: <p>I have been having trouble producing a .NET 4.5 build using our TFS 2012 build server since upgrading our solution to .NET 4.5.  I am having some trouble identifying why TFS builds throw a runtime exception.</p>
  
  <ul>
  <li>All projects in the solution declare .NET 4.5 as the Target
  framework.</li>
  <li>Solution builds and runs properly on local developer machines running VS2012 Pro.</li>
  <li>Solution builds properly on Team Foundation Build Server 2012.</li>
  <li>Solution built on build server throws the below run-time exception.</li>
  <li>Builds from VS2012 and build server both refer to C:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll at runtime
  (confirmed by both application logging and tracing with procmon.exe)</li>
  <li>The version of C:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll is 4.0.30319.18034</li>
  <li>Browsing  with ildasm.exe shows that C:\Windows\Microsoft.NET\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll does contain the type 'System.Runtime.CompilerServices.ExtensionAttribute'.</li>
  <li>The application is 32-bit.</li>
  <li>I don't believe our build process uses ILMerge.exe (we have separate dlls or each project).</li>
  </ul>
  
  <p>The run-time exception encountered with the build from the build server showing that is unable to find the expected type 'ExtensionAttribute' that was moved into mscorlib.dll in version 4.5:</p>
  
  <pre><code>System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
  System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
     at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
     at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
     at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
     at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly&amp; lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType ...). System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. (Fault Detail is equal to An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
  System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
     at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
     at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
     at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
     at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly&amp; lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType ...).
  </code></pre>
  
  <p><strong>Why do TFS builds throw throw this runtime exception when ostensibly they are dynamically linking to the .NET 4.5 version of mscorlib.dll?</strong></p>
  
  <p>Some related SA questions:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/13748055/could-not-load-type-system-runtime-compilerservices-extensionattribute-from-as">Could not load type &#39;System.Runtime.CompilerServices.ExtensionAttribute&#39; from assembly &#39;mscorlib</a></li>
  </ul>