ASP.NET应用程序的WCF服务

开发 后端
本文介绍ASP.NET应用程序的WCF服务,我们来学习如何在前面的ASP.NET网站中添加一个支持AJAX功能的WCF服务。

ASP.NET应用程序添加WCF服务

现在,我们来学习如何在前面的ASP.NET网站中添加一个支持AJAX功能的WCF服务。为此,请右击上面的示例网站AJAXWCFTest1并选择“Add New Items…”,在随后出现的“Add New Items”对话框中选择“AJAX-Enabled WCF Service”模板添加一个新的WCF服务并命名为TimeService。

添加支持AJAX技术的WCF服务

通过上面的操作后,你会发现Web网站中添加了一个服务端点(即timeservice.svc)以及与之相联系的位于文件夹App_Code下的 Code-behind文件timeservice.cs。此外,还注意到,配置文件web.config也被修改以便为刚刚创建的WCF服务提供相应的注册和发现信息。

现在创建的这个TimeService类中已经隐含地描述了所定义WCF服务的契约及其显式实现。注意,其中的ServiceContract和OperationContract属性承担了与以前的WCF版本编程中同样的角色。另外,为了简化起见,在此没有使用接口定义契约。

  1. using System;  
  2. using System.Runtime.Serialization;  
  3. using System.ServiceModel;  
  4. using System.ServiceModel.Activation;  
  5. using System.ServiceModel.Web;  
  6.  
  7. [ServiceContract (Namespace = "Samples.Services")]  
  8. [AspNetCompatibilityRequirements(  
  9. RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]  
  10. public class TimeService  
  11. ...{  
  12. [OperationContract]  
  13. public DateTime GetTime()  
  14. ...{  
  15. return DateTime.Now;  
  16. }  
  17. [OperationContract]  
  18. public string GetTimeFormat(string format)  
  19. ...{  
  20. return DateTime.Now.ToString(format);  
  21. }  
  22. }  

注意到,上面的TimeService类共暴露了两个公共端点,分别是GetTime和GetTimeFormat。
到达上面接口中方法的端点定义于一个SVC文件中。下面给出了文件timeservice.svc的内容:

  1. <%@ ServiceHost Language="C#" 
  2. Debug="true" 
  3. Service="TimeService" 
  4. CodeBehind="~/App_Code/TimeService.cs" %>  

这个服务宿主(ServiceHost)指明了实现该服务使用的语言以及相应的源文件的位置,***通过Service属性标识所使用的契约名字。
在正式开始测试这个服务前还有***一项工作就是在宿主ASP.NET应用程序的配置文件web.config中注册上面这个WCF服务。下面展示了配置文件web.config中的相关配置节的内容:

  1. <system.serviceModel> 
  2. <behaviors> 
  3. <endpointBehaviors> 
  4. <behavior name="TimeServiceAspNetAjaxBehavior"> 
  5. <enableWebScript /> 
  6. </behavior> 
  7. </endpointBehaviors> 
  8. </behaviors> 
  9. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
  10. <services> 
  11. <service name="TimeService"> 
  12. <endpoint address="" 
  13. behaviorConfiguration="TimeServiceAspNetAjaxBehavior" 
  14. binding="webHttpBinding" 
  15. contract="TimeService" /> 
  16. </service> 
  17. </services> 
  18. </system.serviceModel>  

注意,上面的配置内容是随着WCF服务的创建由系统自动生成的。

在此,首先针对前面WCF服务中的所有端点注册一个行为列表。通过这种方式,为WCF服务TimeServiceAspNetAjaxBehavior定义了一个行为并且指出它使用客户端脚本经由HTTP Web协议接受请求。从逻辑上分析,上面的enableWebScript元素与ASP.NET Web服务中用于修饰Web服务类的ScriptService属性是一致的。

然后,需要枚举宿主于当前ASP.NET应用程序中的所有WCF服务。注意,上面的web.config文件中仅展示了一个名字为TimeService的服务,它的一个端点使用了TimeService契约和webHttpBinding绑定模型。      

【编辑推荐】

  1. XML和ASP.NET
  2. ASP.NET中Java script中调用c#方法
  3. ASP.NET整个Postback程序处理的过程
  4. ASP.NET服务器端控件CheckBoxList
  5. 浅析ASP.NET的Membership
责任编辑:佚名 来源: 博客堂
相关推荐

2009-07-27 16:09:50

2009-07-20 16:08:04

ASP.NET应用程序

2009-07-29 17:01:13

2009-08-05 10:16:54

部署ASP.NET应用

2009-07-29 17:21:10

2009-07-22 17:32:40

ASP.NET应用程序

2009-07-21 15:02:19

ASP.NET应用程序

2009-07-23 13:26:21

2009-07-20 17:39:36

WCF服务ASP.NET AJA

2009-07-23 14:25:03

ASP.NET 2.0

2009-07-21 15:14:32

预编译应用程序ASP.NET

2009-07-28 10:11:06

ASP.NET应用程序

2009-07-29 10:30:53

Web应用程序ASP.NET

2009-07-27 17:54:39

WCF服务ASP.NET

2009-07-23 13:14:20

ASP.NET应用程序

2009-07-27 17:51:58

WCF服务ASP.NET

2009-08-04 11:46:09

2009-07-21 15:23:55

预编译Web应用程序ASP.NET

2009-07-24 10:41:00

ASP.NET Web

2009-07-22 18:07:55

论坛应用程序ASP.NET MVC
点赞
收藏

51CTO技术栈公众号