技术开发 频道

WCF中的REST架构

  添加服务

  添加一个支持AJAX的 WCF 服务相当简单。第一步就是使用'AJAX-enabled WCF Service'模板添加服务。过程如下:在Solution Explorer里面选择”Add New Item’ > 选择'AJAX-enabled WCF Service' > 将其命名为 CatalogService.cs > 点击 ‘Add’. 


 
  模板看起来相当神奇,其实它不过是添加了一些特定的文件,并更改了另一些文件(通常是*.config文件)而已。如果你是按照我的步骤,那么这个模板做了下列事情:
  1. 在项目根目录下添加CatalogService.svc文件
  2. 在app_code目录下添加CatalogService.cs文件
  3. 在web.config内配置服务

  分析一下生成的文件
  我们来看一下上面模板生成的文件并稍加讨论: 

CatalogService.svc
<%@ ServiceHost Language="C#"
Debug
="true"
Service
="CatalogService"
CodeBehind
="~/App_Code/CatalogService.cs" %>

  *.svc文件可以类比于*.asmx文件。如果该服务在IIS或者WAS上运行,这个文件就作为服务的可定位资源。换句话说,*.svc 就是你服务的的基址。这个文件里我们只需注意两个属性:首先是Service属性,该属性被设为实现该服务的CLR类型名(你可以在app_code目录下的CatalogService.cs中找到该类型)。第二个就是指向实现文件的CodeBehind属性。

  你无需修改svc 文件的内容,我只是为了向你展示该文件就是作为服务的base地址。 

web.config
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CatalogServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="CatalogService">
<endpoint address=""
behaviorConfiguration
="CatalogServiceAspNetAjaxBehavior"
binding
="webHttpBinding"
contract
="CatalogService" />
</service>
</services>
</system.serviceModel>
0
相关文章