技术开发 频道

在ASP.NET AJAX的web应用中使用TODO服务

  【IT168 专稿】现在我们已经定义了所有运行我们的TODO应用程序所需要的资料,是该以启用ASP.NET AJAX的WCF服务方式暴露服务给客户端的时候了。对于这点,我们将添加一个启用AJAX的WCF服务.svc文件。同时,我们将去掉代码分离文件。

  第一篇:使用ASP.NET AJAX调用WCF服务项模板
  第二篇:在ASP.NET AJAX的类库中使用服务接口定义

  或者,我们可以添加一个XML文件或者文本文件然后将文件重新命名为ToDoService.svc即可。使用XML编辑器打开它并添加如下所示的指令:

<%@ ServiceHost Language="C#" Debug="true" Service="ServiceLibrary.ToDoService" %>

  现在,我们将在web.config文件中放置运行该服务的必要配置信息。该代码如下所示:

<system.serviceModel>
  
<behaviors>
  
<endpointBehaviors>
    
<behavior name="AspNetAjaxBehavior">
    
<enableWebScript />
    
</behavior>
    
<behavior name="WebUI.HelloWorldServiceAspNetAjaxBehavior">
    
<enableWebScript />
    
</behavior>
  
</endpointBehaviors>
  
</behaviors>
  
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  
<services>
  
<service name="ServiceLibrary.ToDoService">
    
<endpoint behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding"
     contract
="ServiceLibrary.IToDoService" />
  
</service>
  
<service name="WebUI.HelloWorldService">
    
<endpoint address="" behaviorConfiguration="WebUI.HelloWorldServiceAspNetAjaxBehavior"
     binding
="webHttpBinding" contract="WebUI.HelloWorldService" />
  
</service>
  
</services>
</system.serviceModel>

  现在,在文件上右键属性并选择在浏览器中查看来查看运行的服务。再进入下一阶段之前,必须提到几点事情。在ASP.NET附带的一些特性如HTTP Context、Session等中你需要添加一个serviceHostingEnvironment 并设置aspNetCompatibilityEnabled="true" 来启用使用的WCF服务。

0
相关文章