WCF The service cannot be activated because it does not support ASP.NET compatibility

测试发布到虚拟目录中时发生的报错现象,由于不支持asp.net兼容性而导致服务无法激活启用。

具体错误信息如下:

Server Error in ‘/Service2‘ Application.
--------------------------------------------------------------------------------

The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as ‘Allowed‘ or ‘Required‘.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as ‘Allowed‘ or ‘Required‘.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 

[InvalidOperationException: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as ‘Allowed‘ or ‘Required‘.]
   System.ServiceModel.Activation.HostedAspNetEnvironment.ValidateCompatibilityRequirements(AspNetCompatibilityRequirementsMode compatibilityMode) +120262
   System.ServiceModel.Activation.AspNetCompatibilityRequirementsAttribute.System.ServiceModel.Description.IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase) +31
   System.ServiceModel.Description.DispatcherBuilder.ValidateDescription(ServiceDescription description, ServiceHostBase serviceHost) +190
   System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +109
   System.ServiceModel.ServiceHostBase.InitializeRuntime() +60
   System.ServiceModel.ServiceHostBase.OnBeginOpen() +27
   System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +50
   System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +318
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +206
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +651

[ServiceActivationException: The service ‘/Service2/DataService.svc‘ cannot be activated due to an exception during compilation.  The exception message is: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as ‘Allowed‘ or ‘Required‘..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +687598
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.ServiceHttpHandler.EndProcessRequest(IAsyncResult result) +6
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +96

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

中文版错误信息:点击打开链接

解决方案:

1、web.config中增加

<serviceHostingEnvironment aspNetCompatibilityEnabled="false">

注意这个是放在<system.serviceModel>中的。

2、在wcf的服务类加上附加属性 AspNetCompatibilityRequirements

其实,通过阅读小注可以发现,下面两种方式均可:

小注:

1、ServiceHostingEnvironment.AspNetCompatibilityEnabled 属性:

获取一个值,该值指示此服务是否在 ASP.NET HTTP 应用程序管道的上下文中运行。
如果为当前 AppDomain 启用了 ASP.NET 兼容性,则为 true;否则为 false。 默认值为 false。

2、AspNetCompatibilityRequirementsMode 枚举:
指定 Windows Communication Foundation (WCF) 服务是否(或能否)以与 ASP.NET 兼容的模式运行。

3、在 ASP.NET 兼容模式中承载 WCF 服务

尽管 WCF 模型旨在跨宿主环境和传输保持行为的一致性,但经常在一些方案中,应用程序中不要求这种程度的灵活性。 WCF 的 ASP.NET 兼容模式适用于具有以下特点的方案:不需要具有在 IIS 外部承载或通过 HTTP 之外的协议进行通信的能力,但使用 ASP.NET Web 应用程序平台的所有功能。

在默认的并行配置中,承载基础结构的 WCF 截获 WCF 消息并将其路由到 HTTP 管道之外;与此不同的是,在 ASP.NET 兼容模式中运行的 WCF 服务完全参与 ASP.NET HTTP 请求生命周期。 IHttpHandler implementation, similar to the way requests for ASPX pages and ASMX Web services are handled." xml:space="preserve">在兼容模式中,WCF 服务通过IHttpHandler 实现来使用 HTTP 管道,其方式类似于处理 ASPX 页和 ASMX Web 服务的请求。 因此,WCF 的行为在以下 ASP.NET 功能方面与 ASMX 相同:

  • HttpContext: WCF services running in ASP.NET Compatibility Mode can access Current and its associated state." xml:space="preserve">在 ASP.NET 兼容模式中运行的 HttpContext: WCF 服务可以访问 Current 以及与其关联的状态。
  • 基于文件的授权:在 ASP.NET 兼容模式中运行的 WCF 服务可以通过将文件系统访问控制列表 (ACL) 附加到服务的 .svc 文件来获得保护。
  • 可配置的 URL 授权:当 WCF 服务在 ASP.NET 兼容模式中运行时,将对 WCF 请求强制执行 ASP.NET 的 URL 授权规则。
  • HttpModuleCollection extensibility: Because WCF services running in ASP.NET Compatibility Mode participate fully in the ASP.NET HTTP request lifecycle, any HTTP module configured in the HTTP pipeline is able to operate on WCF requests both before and after service invocation." xml:space="preserve">HttpModuleCollection 扩展性:由于在 ASP.NET 兼容模式中运行的 WCF 服务完全参与 ASP.NET HTTP 请求生命周期,因此在 HTTP 管道中配置的任何 HTTP 模块能够在服务调用前后的 WCF 请求上进行操作。
  • ASP.NET 模拟:WCF 服务使用 ASP.NET 模拟线程的当前标识来运行,如果已为应用程序启用 ASP.NET 模拟,则该标识可能与 IIS 进程标识不同。 如果为某个特定服务操作同时启用 ASP.NET 模拟和 WCF 模拟,则服务模拟最终使用从 WCF 获得的标识来运行。

可通过下面的配置(位于应用程序的 Web.config 文件中)在应用程序级别上启用 WCF 的 ASP.NET 兼容模式:

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

true” if not specified." xml:space="preserve"> 如果没有指定,则此值默认为“true”。 false” indicates that all WCF services running in the application will not run in ASP.NET Compatibility Mode." xml:space="preserve">若将此值设置为“false”,则指示在应用程序中运行的所有 WCF 服务将不在 ASP.NET 兼容模式中运行。

由于 ASP.NET 兼容模式暗含的请求处理语义与 WCF 默认值完全不同,因此单独的服务实现能够控制其是否在已启用 ASP.NET 兼容模式的应用程序内运行。 AspNetCompatibilityRequirementsAttribute to indicate whether they support ASP.NET Compatibility Mode." xml:space="preserve">服务可以使用 AspNetCompatibilityRequirementsAttribute 来指示其是否支持 ASP.NET 兼容模式。 Allowed." xml:space="preserve">此特性的默认值为 Allowed

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

public class CalculatorService : ICalculatorSession

{//Implement calculator service methods.}

下表演示应用程序范围的兼容模式设置如何与单独服务指定的支持级别交互:


应用程序范围的兼容模式设置


[AspNetCompatibilityRequirementsMode]

设置


观察到的结果


true”" xml:space="preserve">aspNetCompatibilityEnabled = “true”


Required


服务成功激活。


true”" xml:space="preserve">aspNetCompatibilityEnabled = “true”


Allowed


服务成功激活。


true”" xml:space="preserve">aspNetCompatibilityEnabled = “true”


NotAllowed


服务接收消息时发生激活错误。


false”" xml:space="preserve">aspNetCompatibilityEnabled = “false”


Required


服务接收消息时发生激活错误。


false”" xml:space="preserve">aspNetCompatibilityEnabled = “false”


Allowed


服务成功激活。


false”" xml:space="preserve">aspNetCompatibilityEnabled = “false”


NotAllowed


服务成功激活。

说明

IIS 7.0 和 WAS 允许 WCF 服务通过 HTTP 之外的协议进行通信。 但是,在已启用 ASP.NET 兼容模式的应用程序中运行的 WCF 服务不允许公开非 HTTP 终结点。 在服务接收其第一条消息时,这种配置会生成激活异常。

AspNetCompatibilityRequirementsMode and the ASP.NET Compatibility sample." xml:space="preserve">有关为 WCF 服务启用 ASP.NET 兼容模式的更多信息,请参见 AspNetCompatibilityRequirementsMode 和ASP.NET 兼容性示例。

版权声明:作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文版权归作者和CSDN共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

时间: 2024-10-01 02:29:45

WCF The service cannot be activated because it does not support ASP.NET compatibility的相关文章

The service cannot be activated because it does not support ASP.NET compatibility

刚刚在ASP.NET创建一个Service,在运行时,它即显示异常: The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompat

WCF RIA SERVICE相关技术

WCF RIA SERVICE实体属性拷贝 private void DoSubmit() { ((IEditableObject)this.RepairContract).EndEdit(); var domainContext = new DmsDomainContext(); var newRepairContract = new RepairContract(); domainContext.RepairContracts.Add(newRepairContract); newRepai

WCF Data Service ----理论准备

WCF数据服务(WCF Data Service )是.NET Framework的一个组件. 可以使用此组件创建一些服务,利用开放式数据协议(OData)来借助具象状态传输(REST)语义通过Web或Intranet公开和使用数据. OData(Open Data Protocol)开放数据协议 OData将数据公开为可通过URI寻址的资源,可使用GET.PUT.POST和DELETE这些标准HTTP谓词来访问和更改数据. OData使用Entity Data Model的实体关系约定,将资源

WCF Windows Service Using TopShelf and ServiceModelEx z

http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are two excellent .NET libraries that help us to build enterprise solutions using the Windows Communication Foundation (WCF) – TopShelf and ServiceModelEx

Wcf Restful Service服务搭建

目的 使用Wcf(C#)搭建一个Restful Service 背景 最近接到一个项目,客户要求使用Restful 方式接收到数据,并对数据提供对数据的统计显示功能,简单是简单,但必须要使用Restful方式,客户端传递数据就必须使用Rest ful的格式的url,提交方式为Post. 其实在之前的公司里边就开发过一个restful服务,但只记得自己使用的wcf,而今已经忘记的差不多了,还以为只是简单的搭建一个wcf就可以,今天起初就创建了一个wcf项目,发现wcf项目http://localh

WCF - Windows Service Hosting

WCF - Windows Service Hosting The operation of Windows service hosting is a simple one. Given below are the steps with requisite coding and screenshots that explain the process in an easy way. 在windows服务上托管wcf是一个简单的操作. Step-1: Now let’s create a WCF

调用WCF Data Service的几点Tips

使用Linq实现sql in statement的时候,用EF的时候可以通过Contains.Exists的方法实现.但是在使用WCF Data Service的context的时候,会报不支持该方法的错误.解决方案就是加上AsEnumerable(). var products = from p in ctx.V_Product.AsEnumerable() where catalogs.Exists(c => c.CatalogID == p.CatalogID) select p 主从表级

Learning WCF Chapter2 Service Description

While messaging protocols are responsible for message serialization formats,there must be a way to capture the protocols required to communicate between clients and services. In Chapter 1,you worked with WSDL documents,which describe the requirements

(WCF) WCF and Service Debug

需要做一个多程序间的通讯,采用WCF和WCF Service是目前的选择. 需求:和产品进行通讯,和用户有交互操作,并将最后结果传送个DB 基本思路: 1. 用WPF客户端程序和产品进行通讯,获取必要的结果. 2. WPF客户端程序里调用WCF 的Proxy, 将结果传送个WCF Service. 3. 另外一个程序通过另一个WCF Proxy获取结果,并传送到DB Control System(Service)             <-------------------->   WCF