ASP.NET Ajax调用WCFService(VS2008 beta2 简体中文版)
2007-09-29 12:56
447 查看
看见Dudu大哥写了一篇ASP.NET Ajax =》WCF(.NET 3.5 Beta 2,VS2005) 觉得挺有意思,以前没有WCF的时候我们都是调用Webservice,当然调用页面.cs里面的静态方法也可以;听说WCF的目标就是要替换以前那些实现分布式的技术,例如WebService,那我们也只好改成调用WCFService 了。到底这两种孰好孰差?大家可以讨论一下:
首先建立一个WebApplication项目,例如叫做AjaxAndWCFWebApplication:(默认支持AJAX)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace AjaxAndWCFWebApplication.WCFService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace AjaxAndWCFWebApplication.WCFService
<%@ ServiceHost Language="C#" Debug="true"
Service="AjaxAndWCFWebApplication.WCFService.WCFAjaxService"
CodeBehind="WCFAjaxService.svc.cs"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
其实就是在后面加上 Factory=.....那些。
接下来删除Web.config 中 <System.ServiceModel> 部分,为什么呢?因为你用了
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory",The
WebScriptServiceHostFactory class is designed to enable Windows Communication Foundation
services to be used to serve data to AJAX-enabled Web pages without having to write WCF
configuration settings.
好了,如果你现在右键WCFAjaxService.svc选择在浏览器中查看,你将会得到如下错误:
IIS 指定了身份验证方案“Ntlm, Anonymous”,但绑定仅支持一种身份验证的规范。有效的身份验证方案
为摘要、协商、NTLM、基本或匿名。请更改 IIS 设置,以便仅使用单一的身份验证方案。
这是个bug,参看:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1922152&SiteID=1&pageid=0
可是这个是vs里集成的iis啊?怎么改?高人请赐教,我的办法是在本机的iis中建立个站点,并把这个站点
的“集成windows身份验证”前面的勾去掉,只留下一个匿名认证。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="AjaxAndWCFWebApplication._Default" %>
<!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>无标题页</title>
<script type="text/javascript">
function OnSucceeded(result)
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WCFService/WCFAjaxService.svc" />
</Services>
</asp:ScriptManager>
<div>
<h2>Ajax调用WCF Service</h2>
<p> Calling a service that returns the current server time and the input name.</p>
请输入昵称:<input id="xname" type="text" name="xname" /><input id="DateButton"
type="button" value="调用" onclick="ClientGetServerDateTimeByName()" />
</div>
<div>
<br />
<span id="Results"></span>
</div>
</div>
</form>
</body>
</html>
在浏览器中打开刚才建立的网站,运行如下:
源代码下载 下载
首先建立一个WebApplication项目,例如叫做AjaxAndWCFWebApplication:(默认支持AJAX)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace AjaxAndWCFWebApplication.WCFService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace AjaxAndWCFWebApplication.WCFService
<%@ ServiceHost Language="C#" Debug="true"
Service="AjaxAndWCFWebApplication.WCFService.WCFAjaxService"
CodeBehind="WCFAjaxService.svc.cs"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
其实就是在后面加上 Factory=.....那些。
接下来删除Web.config 中 <System.ServiceModel> 部分,为什么呢?因为你用了
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory",The
WebScriptServiceHostFactory class is designed to enable Windows Communication Foundation
services to be used to serve data to AJAX-enabled Web pages without having to write WCF
configuration settings.
好了,如果你现在右键WCFAjaxService.svc选择在浏览器中查看,你将会得到如下错误:
IIS 指定了身份验证方案“Ntlm, Anonymous”,但绑定仅支持一种身份验证的规范。有效的身份验证方案
为摘要、协商、NTLM、基本或匿名。请更改 IIS 设置,以便仅使用单一的身份验证方案。
这是个bug,参看:http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1922152&SiteID=1&pageid=0
可是这个是vs里集成的iis啊?怎么改?高人请赐教,我的办法是在本机的iis中建立个站点,并把这个站点
的“集成windows身份验证”前面的勾去掉,只留下一个匿名认证。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="AjaxAndWCFWebApplication._Default" %>
<!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>无标题页</title>
<script type="text/javascript">
function OnSucceeded(result)
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WCFService/WCFAjaxService.svc" />
</Services>
</asp:ScriptManager>
<div>
<h2>Ajax调用WCF Service</h2>
<p> Calling a service that returns the current server time and the input name.</p>
请输入昵称:<input id="xname" type="text" name="xname" /><input id="DateButton"
type="button" value="调用" onclick="ClientGetServerDateTimeByName()" />
</div>
<div>
<br />
<span id="Results"></span>
</div>
</div>
</form>
</body>
</html>
在浏览器中打开刚才建立的网站,运行如下:
源代码下载 下载
相关文章推荐
- 新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)
- 系统地学习ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)
- 新瓶旧酒ASP.NET AJAX(9) - 客户端脚本编程(Sys.Net命名空间下的WebServiceProxy、WebServiceError、Generated Proxy Classes以及调用WebService、PageMethod)
- ASP.NET AJAX 调用Service Method (Too simply)
- asp.net WebService+Ajax调用
- EXT调用ASP.NET AJAX WebService
- Asp.Net Ajax+WCF Service+ADO.NET Entity Framework实现数据列表,数据筛选,分页,排序,删除
- Jquery 调用asp.net ajax (web service/static page method)的示例(一)---简单参数
- Asp.Net Ajax+WCF Service+ADO.NET Entity Framework实现数据列表,数据筛选,分页,排序,删除
- ASP.NET Ajax调用WCF服务示例dudu
- EXT调用ASP.NET AJAX WebService
- 配置AJAX Enabled WCF在hosting时: Showing the serviceMetadata in an ASP.NET AJAX Service
- ASP.NET Ajax调用WCF服务的代码示例
- ASP.NET调用多Endpoint多Contract的WCF Service
- ASP.NET WebService+Ajax调用
- Jquery 调用asp.net ajax (web service/static page method)的示例(二)---复杂参数
- ASP.NET AJAX,WCF,ADO.NET Entity 开发实例
- ASP.NET 3.5核心编程学习笔记(56):AJAX Web Service的调用
- ASP.NET Ajax实例—Javascript直接调用服务器C#代码
- ASP.NET 前台Ajax技术调用后台方法