本次程序和上一节程序类似,只是为WCF服务创建了接口。
Demo:请点击这里
直接看下代码吧。
Service1.svc.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace WebApplication1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
//这里必须写命名空间,不要空着
[ServiceContract(Namespace = "GLM")]
public class Service1 : IService1
{
// 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
// 要创建返回 XML 的操作,
// 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// 并在操作正文中包括以下行:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string SayHello(string name)
{
// 在此处添加操作实现
return string.Format("hello,name={0}",name);
}
}
public interface IService1
{
string SayHello(string name);
}
}
Service1.svc代码:
<%@ ServiceHost Language="C#" Debug="true" Service="WebApplication1.Service1" CodeBehind="Service1.svc.cs" %>
WebForm1.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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 test() {
var client = new GLM.Service1();
var strName = "Jack";
client.SayHello(strName, OnComplete, OnError);
}
function OnComplete(result) {
alert(result);
}
function OnError(result) {
alert("Error");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Service1.svc" />
</Services>
</asp:ScriptManager>
<div>
<input id="button1" type="button" value="click" onclick="test();" />
</div>
</form>
</body>
</html>
好吧,问题已经解决,刚才查看了以下配置文件,发现问题所在了,如果想将OperationContract和ServiceContract用在接口上,必须修改配置文件的endPoint节点的contract属性,该节点原来是这样的:
<services>
<service name="WebApplication1.Service1">
<endpoint address="" behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="WebApplication1.Service1" />
</service>
</services>
修改成这样就OK了:
<services>
<service name="WebApplication1.Service1">
<endpoint address="" behaviorConfiguration="WebApplication1.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="WebApplication1.IService1" />
</service>
</services>
现在,Service1.svc.cs文件就是这样了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace WebApplication1
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
// 要使用 HTTP GET,请添加 [WebGet] 特性。(默认 ResponseFormat 为 WebMessageFormat.Json)
// 要创建返回 XML 的操作,
// 请添加 [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// 并在操作正文中包括以下行:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
public string SayHello(string name)
{
// 在此处添加操作实现
return string.Format("hello,name={0}",name);
}
}
//这里必须写命名空间,不要空着
[ServiceContract(Namespace = "GLM")]
public interface IService1
{
[OperationContract]
string SayHello(string name);
}
}
注意在WebForm1.aspx中调用服务的时候,要修改成:
<script type="text/javascript">
function test() {
var client = new GLM.IService1();//看这里
var strName = "Jack";
client.SayHello(strName, OnComplete, OnError);
}
function OnComplete(result) {
alert(result);
}
function OnError(result) {
alert("Error");
}
</script>