WCF web服务错误:无法激活该服务,因为它不支持ASP.NET兼容性
我正在尝试创建一个restful wcf web服务。当我尝试通过客户端连接到服务时,出现以下错误: 无法激活该服务,因为它不支持ASP.NET兼容性。已为此应用程序启用ASP.NET兼容性。在web.config中关闭ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到RequirementsMode设置为“允许”或“必需”的服务类型中。 其他人也有问题,但他们通过更改web.config来解决。我已经实现了他们的修复,但问题仍然存在。这是我的web.config:WCF web服务错误:无法激活该服务,因为它不支持ASP.NET兼容性,asp.net,wcf,rest,Asp.net,Wcf,Rest,我正在尝试创建一个restful wcf web服务。当我尝试通过客户端连接到服务时,出现以下错误: 无法激活该服务,因为它不支持ASP.NET兼容性。已为此应用程序启用ASP.NET兼容性。在web.config中关闭ASP.NET兼容模式,或将AspNetCompatibilityRequirements属性添加到RequirementsMode设置为“允许”或“必需”的服务类型中。 其他人也有问题,但他们通过更改web.config来解决。我已经实现了他们的修复,但问题仍然存在。这是我的w
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior" >
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="myfirstwcf">
<endpoint address="ws" binding="basicHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="ws2" binding="wsHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="" behaviorConfiguration="WebBehavior"
binding="webHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled= "true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
在您的主要服务上,您可以将您的服务标记为:
[AspNetCompatibilityRequirements(
RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
从开始,它将工作:
您已在代码中更改此行或在web.config中添加此行:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior" >
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="myfirstwcf">
<endpoint address="ws" binding="basicHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="ws2" binding="wsHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="" behaviorConfiguration="WebBehavior"
binding="webHttpBinding"
contract="Imyfirstwcf" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled= "true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
如果有人拥有大量服务,并且服务是使用自定义的
ServiceHostFactory
创建的,那么也可以在CreateServiceHost
方法中设置AspNetCompatibilityRequirementsAttribute
示例:
public class HostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var host = new ServiceHost(serviceType, baseAddresses);
//other relevent code to configure host's end point etc
if (host.Description.Behaviors.Contains(typeof(AspNetCompatibilityRequirementsAttribute)))
{
var compatibilityRequirementsAttribute = host.Description.Behaviors[typeof(AspNetCompatibilityRequirementsAttribute)] as AspNetCompatibilityRequirementsAttribute;
compatibilityRequirementsAttribute.RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed;
}
else
{
host.Description.Behaviors.Add(new AspNetCompatibilityRequirementsAttribute() { RequirementsMode =AspNetCompatibilityRequirementsMode.Allowed});
}
return host;
}
}
实际上,根据最新的文档,你需要做两件事 1.对于您的服务级别:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "url")]
public class Service : IService
{
}
2.对于web.config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
有效的感谢可能重复。如果其他人正在使用vb.net…语法是:我真正需要的是一个使用vb.net语法创建restful wcf的好例子,但我能找到的只是C#示例,我花时间尝试转换到vb.net。如果有人知道vb.net中的好示例代码,请让我知道,或者给我发送一个链接。在我找到这个答案之前,我花了大约12个小时试图找到“服务器没有提供有意义的回复;这可能是由于合同不匹配、会话过早关闭或内部服务器错误造成的”,并且解决了我的问题=P谢谢!当我把我的操作系统从Win7切换到Win8时,我得到了这个。甚至调试项目也不起作用。IIS8上的某些内容使得WCF服务需要此配置。谢谢你,史蒂夫!:)工作起来很有魅力。另外,别忘了在您的使用列表中添加System.ServiceModel.Activation命名空间。这解决了我的问题,更改了aspNetCompatibilityEnabled=“false”
这对我很有效。这种方法的另一个好处是,它不需要您重新编译源代码。我更喜欢更改配置文件,而不是更改代码文件。