首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 开发频道>ASP.NET Ajax调用WCF服务示例

ASP.NET Ajax调用WCF服务示例

2007-09-28 15:29作者:dudu出处:论坛整理责任编辑:幽灵

  如果不通过Ajax调用WCF,需要设置为:Factory="System.ServiceModel.Web.WebServiceHostFactory"。

  5、开始第一次运行,访问http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc,会出现如下页面:

  asp.net ajax 调用wcf服务

  6、继续运行,访问http://localhost/AjaxWCFDemo/CNBlogsWCFService.svc/js,你会看到自动生成访问WCF的客户端代理脚本。

  7、OK!服务器端的WCF已经准备好了,下面就开始客户端的访问。

  8、配置ASP.NET Ajax,在web.config中进行设置:

以下是引用片段:
<?xml version="1.0"?>
<configuration> 
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="false">
      <assemblies>
        <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <authentication mode="Forms" />
    <httpHandlers>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpModules>
  </system.web>
</configuration>

  注意:要设置为3.5版本的System.Web.Extensions,如果使用asp.net ajax 1.0会得不到调用WCF服务返回的结果。

  9、修改default.aspx的代码:

  1)添加ScriptManager,将ServiceReference设置为:~/CNBlogsWCFService.svc。

  2)将

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

  Namespace="System.Web.UI" TagPrefix="asp" %>

以下是引用片段:
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>

  改为:

以下是引用片段:
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"

  Namespace="System.Web.UI" TagPrefix="asp" %>

  2)添加调用WCF服务的代码,完整代码如下:

以下是引用片段:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"   Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Ajax WCF 演示 </title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center" style="margin-top:50px">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/CNBlogsWCFService.svc" />
            </Services>
        </asp:ScriptManager>
        <a href="#" onclick="AddToFavorites('1','2')">收藏</a><br />
        <br />
        <span style="color:Red" id="Msg"></span>

        <script type="text/javascript">
        function AddToFavorites(blogID,postID)
        {
            var wcf = new www.cnblog.com.ICNBlogsWCFService();
            wcf.AddToFavorites(blogID,postID,OnSucceeded);
        }
        function OnSucceeded(result)
        {   
            document.getElementById("Msg").innerHTML = result;        
        }  
        </script>

    </div>
    </form>
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

  10、一切就绪,体验一下Ajax调用WCF的快乐!

关注此文的读者还看过:

返回开发频道首页

共2页。 上一页12

软件频道最新更新

热点推荐

天极服务|关于我们|About us|网站律师|RSS订阅|友情合作|加入我们|天极动态|网站地图|意见反馈|MSN/QQ上看天极
Copyright (C) 1999-2012 Yesky.com, All Rights Reserved 版权所有 天极网络