WCF Web服务错误:该服务无法启动,因为它不支持ASP.NET兼容性(WCF web servi

2019-07-30 22:10发布

我想创建一个宁静的WCF Web服务。 当我尝试连接到通过客户端我收到以下错误的服务:

该服务无法启动,因为它不支持ASP.NET兼容性。 ASP.NET兼容性为该应用程序启用。 关闭ASP.NET兼容模式在web.config或AspNetCompatibilityRequirements属性添加到与RequirementsMode设置为“允许”或“必需”的服务类型。

其他人有问题,但他们固定它通过改变他们的web.config文件。 我已经实现了修复,但仍存在问题。 这里是我的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>

Answer 1:

在您的主要服务,你可以标记您的服务为:

[AspNetCompatibilityRequirements(
        RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

从http://forums.silverlight.net/t/21944.aspx



Answer 2:

它的工作:

你必须改变这一行代码或添加一行在web.config中:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> 
</system.serviceModel>


Answer 3:

如果有人有很多的服务和服务使用自定义创建的ServiceHostFactory ,然后AspNetCompatibilityRequirementsAttribute也可以在设置CreateServiceHost方法。

例:

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;
    }
}


Answer 4:

其实,按照最新的文档,你需要做两件事情,

1.对于服务类:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "url")]
public class Service : IService
{
}

2.对于web.config中

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>


文章来源: WCF web service error: The service cannot be activated because it does not support ASP.NET compatibility
标签: asp.net wcf rest