第一次从MVC站点访问时,WCF服务返回“请求的服务'...'无法激活”

时间:2012-07-05 11:39:32

标签: c# .net asp.net-mvc-3 wcf exception-handling

我们有一个MVC3网站正在访问的WCF服务(没有安全性)。

在开发人员计算机上我们没有任何问题但是当我们的TeamCity设置构建中央版本并在IIS7下部署它时,我们第一次加载MVC3站点并且它访问WCF服务 - 服务调用返回消息:< / p>

  

System.ServiceModel.ServiceActivationException:无法激活所请求的服务“http:// localhost:83 / ABCStaticData / StaticDataService.svc”。有关详细信息,请参阅服务器的诊断跟踪日志。**

如果我们立即再次访问同一网站,一切正常。

抛出错误时的服务器堆栈跟踪是:

服务器堆栈跟踪:

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& 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.<>c__DisplayClass22.<.ctor>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

有没有人对可能导致问题的原因或从哪里开始寻找有任何想法?

更新:我忘了提到第一个,如果我们暂时不访问该网站然后返回,它会给出相同的错误 - 然后第二次再次工作。

UPDATE2:这是事件日志条目文本:

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'.. ---> 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& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& 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.<>c__DisplayClassa.<SelectMethodsForInjection>b__9(IInjectionHeuristic h)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at Ninject.Selection.Selector.<SelectMethodsForInjection>b__8(MethodInfo m)
   at System.Linq.Enumerable.WhereArrayIterator`1.MoveNext()
   at Ninject.Planning.Strategies.MethodReflectionStrategy.Execute(IPlan plan)
   at Ninject.Planning.Planner.<>c__DisplayClass1.<CreateNewPlan>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.<>c__DisplayClass10.<Resolve>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.<>c__DisplayClass4.<Create>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.<>c__DisplayClass10.<Resolve>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.<>c__DisplayClass4.<Create>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.<>c__DisplayClass10.<Resolve>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

我们正在使用.NET 4.5和Visual Studio 2012 RC进行开发。

10 个答案:

答案 0 :(得分:25)

我只是设法解决随机'System.ServiceModel.ServiceActivationException'的类似问题(抛出异常,然后在第二次命中时运行。)。在我的情况下异常的原因是服务器上缺少内存。要确认内存问题,您可以执行以下操作:

应用程序调用Web服务:

System.ServiceModel.ServiceActivationException: The requested service, 'XXX' could not be activated. See the server's diagnostic trace logs for more information.

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.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)

WCF跟踪日志:没有记录。

在事件日志中服务器托管wcf服务(与调用应用程序btw相同),除了我发现的大量垃圾信息:

自定义活动详情:

WebHost failed to process a request.
 Sender Information: System.ServiceModel.ServiceHostingEnvironment+HostingManager/58366981
 Exception: System.ServiceModel.ServiceActivationException: The service 'xxx' cannot be activated due to an exception during compilation.  The exception message is: Memory gates checking failed because the free memory (xxx bytes) is less than 5% of total memory.  As a result, the service will not be available for incoming requests.  To resolve this, either reduce the load on the machine or adjust the value of minFreeMemoryPercentageToActivateService on the serviceHostingEnvironment config element.. ---> System.InsufficientMemoryException: Memory gates checking failed because the free memory (xxx bytes) is less than 5% of total memory.  As a result, the service will not be available for incoming requests.  To resolve this, either reduce the load on the machine or adjust the value of minFreeMemoryPercentageToActivateService on the serviceHostingEnvironment config element.

所以我的解决方案是:  1.增加更多的公羊  2.释放一些ram(临时解决方案)  3.更新Web服务的Web配置:

<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment ... minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>
</configuration>

答案 1 :(得分:12)

我也有这个问题,只是简单地把

<serviceHostingEnvironment  minFreeMemoryPercentageToActivateService="1">

下的

<system.serviceModel>

一切正常。

答案 2 :(得分:2)

我遇到了同样的“无法加载类型'System.Runtime.CompilerServices.AsyncStateMachineAttribute'”,我发现这是因为我使用VS 2012 RTM构建了我的应用程序,但后来尝试将其部署在一台机器上只有4.5 Beta。 AsyncStateMachineAttribute是Beta之后添加的新类型。

答案 3 :(得分:0)

.svc扩展名是否在IIS下注册?

有关详细信息,请参阅MSDN

答案 4 :(得分:0)

我也有这个问题,我做的是:

  • 转到服务所在的文件系统中的目录
  • 您将拥有2个服务文件
    • yourService.svc
    • yourService.svc.cs
  • 打开文件yourService.svc并确保您的服务已配置,后面的代码如下所示:

    <%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.yourService.svc" CodeBehind="yourService.svc.cs" %>
    
我以这种方式解决了我的问题。我希望它有所帮助!

答案 5 :(得分:0)

检查您托管服务的服务器的内存是否已满。

答案 6 :(得分:0)

如果在安装Windows Communication Foundation(WCF)后安装IIS,则会发生此错误。要解决此问题,只需运行以下命令:

"%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r

来源:IIS Hosted Service Fails

答案 7 :(得分:0)

我今天遇到了同样的问题,而不是RAM而是磁盘空间。释放空间后,我必须重新启动应用程序池,问题已解决。

答案 8 :(得分:0)

对我来说,原来是IIS中的身份验证设置配置不正确。因此,身份验证失败阻止了初始请求,从而阻止了服务的激活。

答案 9 :(得分:-1)

每次我遇到同样的问题,我都会重新启动电脑,效果很好。它释放了内存