【菜鸟学WCF】使用ScriptManager+Ajax调用WCF服务之如何为服务创建接口

本文探讨了在创建WCF服务时,如何正确地在接口与类上使用[OperationContract]和[ServiceContract]特性,特别是针对启用Ajax的WCF服务。通过对比实例代码与配置文件修改,解释了在接口上应用这两个特性的必要性及具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本次程序和上一节程序类似,只是为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>


问题来了,我们添加的服务是“启用了Ajax的WCF服务”,在Service1.svc。cs文件中,[OperationContract]和[ServiceContract]必须放在类定义上,不能放在接口上,不知道为什么,以上实例代码运行正常。有哪位大神知道原因,请指教,谢了。


好吧,问题已经解决,刚才查看了以下配置文件,发现问题所在了,如果想将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>




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值