title: WCF Service returning "requested service '...' could not be activated" the first time it's accessed from an MVC site
body: <p>We have a WCF service (with no security) that is being accessed by an MVC3 website.</p>
  
  <p>On the developer machines we have no problems with it but when our TeamCity setup builds the central version and deploys it under IIS7 the first time we load the MVC3 site and it accesses the WCF service - the service call returns the message:</p>
  
  <blockquote>
    <p>System.ServiceModel.ServiceActivationException: The requested service, 'http://localhost:83/ABCStaticData/StaticDataService.svc' could not be activated. See the server's diagnostic trace logs for more information.**</p>
  </blockquote>
  
  <p>If we immediately hit the same site again, everything works fine.</p>
  
  <p>The server stack track when it throws the error is:</p>
  
  <p>Server stack trace:</p>
  
  <pre><code>at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory1 factory, WebException responseException, ChannelBinding channelBinding)
  at System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
  at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
  at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
  at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout
  at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
  at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
  at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData, Int32 type) 
  at ABC.StaticData.Editor.StaticDataServiceProxy.IStaticDataService.GetAllMarkets()
  at ABC.StaticData.Editor.StaticDataServiceProxy.StaticDataServiceClient.GetAllMarkets() in c:\BuildAgent\work\55a3bcc468adcccb\StaticData\ABC.StaticData.Editor\Service References\StaticDataServiceProxy\Reference.cs:line 3195
  at ABC.StaticData.Editor.Controllers.MarketsController.&lt;&gt;c__DisplayClass22.&lt;.ctor&gt;b__13() in c:\BuildAgent\work\55a3bcc468adcccb\StaticData\ABC.StaticData.Editor\Controllers\MarketsController.cs:line 263
  at ABC.StaticData.Editor.Controllers.MarketsController.Index() in c:\BuildAgent\work\55a3bcc468adcccb\StaticData\ABC.StaticData.Editor\Controllers\MarketsController.cs:line 306
  </code></pre>
  
  <p>Does anyone have any ideas on what could be causing the problem or where to start looking?</p>
  
  <p>UPDATE: I forgot to mention the first that if we then don't access the site for a while and then go back, it gives the same error - then works again the second time.</p>
  
  <p>UPDATE2: This is the event log entry text:</p>
  
  <pre><code>WebHost failed to process a request.
   Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/3043589
   Exception: System.ServiceModel.ServiceActivationException: The service '/ABCStaticData/StaticDataService.svc' cannot be activated due to an exception during compilation.  The exception message is: Could not load type 'System.Runtime.CompilerServices.AsyncStateMachineAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.. ---&gt; System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.AsyncStateMachineAttribute' 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 attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType&amp; attributeType, IRuntimeMethodInfo&amp; ctor, Boolean&amp; ctorHasParameters, Boolean&amp; isVarArg)
     at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)
     at System.Reflection.CustomAttribute.IsDefined(RuntimeMethodInfo method, RuntimeType caType, Boolean inherit)
     at System.Reflection.RuntimeMethodInfo.IsDefined(Type attributeType, Boolean inherit)
     at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.HasAttribute(MemberInfo member, Type type)
     at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject(MemberInfo member)
     at Ninject.Selection.Selector.&lt;&gt;c__DisplayClassa.&lt;SelectMethodsForInjection&gt;b__9(IInjectionHeuristic h)
     at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
     at Ninject.Selection.Selector.&lt;SelectMethodsForInjection&gt;b__8(MethodInfo m)
     at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
     at Ninject.Planning.Strategies.MethodReflectionStrategy.Execute(IPlan plan)
     at Ninject.Planning.Planner.&lt;&gt;c__DisplayClass1.&lt;CreateNewPlan&gt;b__0(IPlanningStrategy s)
     at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action)
     at Ninject.Planning.Planner.CreateNewPlan(Type type)
     at Ninject.Planning.Planner.GetPlan(Type type)
     at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass10.&lt;Resolve&gt;b__c(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
     at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
     at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
     at Ninject.Activation.Providers.StandardProvider.&lt;&gt;c__DisplayClass4.&lt;Create&gt;b__2(ITarget target)
     at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass10.&lt;Resolve&gt;b__c(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
     at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
     at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
     at Ninject.Activation.Providers.StandardProvider.&lt;&gt;c__DisplayClass4.&lt;Create&gt;b__2(ITarget target)
     at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
     at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
     at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
     at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
     at Ninject.Activation.Context.Resolve()
     at Ninject.KernelBase.&lt;&gt;c__DisplayClass10.&lt;Resolve&gt;b__c(IBinding binding)
     at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
     at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
     at Ninject.Extensions.Wcf.BaseNinjectServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses)
     at System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses)
     at System.ServiceModel.ServiceHostingEnvironment.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity)
     at System.ServiceModel.ServiceHostingEnvironment.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity)
     at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity)
     --- End of inner exception stack trace ---
     at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity)
     at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath, EventTraceActivity eventTraceActivity)
   Process Name: w3wp
   Process ID: 3248
  </code></pre>
  
  <p>Just another point that we're developing using .NET 4.5 and Visual Studio 2012 RC.</p>