如何让WCF服务更好地支持Web Request和AJAX调用

如何让WCF服务更好地支持Web Request和AJAX调用,第1张

1. 创建服务

2. 修改接口

为了做演示,我们将默认的那个Operation修改一下

using System

using System.Collections.Generic

using System.Linq

using System.Runtime.Serialization

using System.ServiceModel

using System.Text

using System.ServiceModel.Web

namespace WebApplication1

{

// 注意: 如果更改此处的接口名称 "INorthwindService",也必须更新 Web.config 中对 "INorthwindService" 的引用。

[ServiceContract]

public interface INorthwindService

{

[OperationContract]

[WebGet(UriTemplate="HelloWorld")]

string HelloWorld()

}

}

注意,我们这里加了一个WebGet的Attribute,这将允许WCF服务直接通过地址调用

3. 实现服务

using System

using System.Collections.Generic

using System.Linq

using System.Runtime.Serialization

using System.ServiceModel

using System.Text

namespace WebApplication1

{

// 注意: 如果更改此处的类名 "NorthwindService",也必须更新 Web.config 中对 "NorthwindService" 的引用。

public class NorthwindService : INorthwindService

{

#region INorthwindService 成员

public string HelloWorld()

{

return "Hello,world"

}

#endregion

}

}

这里的实现依然是我最喜欢的HelloWorld

4. 修改配置文件(web.config),要支持直接通过WebGet的方法调用WCF服务,必须用一个特殊的binding,是webHttpBinding

<system.serviceModel>

<behaviors>

<serviceBehaviors>

<behavior name="WebApplication1.NorthwindServiceBehavior">

<serviceMetadata httpGetEnabled="true" />

<serviceDebug includeExceptionDetailInFaults="false" />

behavior>

serviceBehaviors>

<endpointBehaviors>

<behavior name="test">

<webHttp/>

behavior>

endpointBehaviors>

behaviors>

<services>

<service behaviorConfiguration="WebApplication1.NorthwindServiceBehavior"

name="WebApplication1.NorthwindService">

<endpoint address="" binding="webHttpBinding" contract="WebApplication1.INorthwindService" behaviorConfiguration="test">

<identity>

<dns value="localhost" />

identity>

endpoint>

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

service>

services>

system.serviceModel>

上面的粗斜体部分是要添加或者修改的

5. 浏览该服务

我们看到,通过这样的地址就可以实现调用了。默认情况下,它返回的数据格式是XML的

6. 修改合约,让它返回json数据

using System

using System.Collections.Generic

using System.Linq

using System.Runtime.Serialization

using System.ServiceModel

using System.Text

using System.ServiceModel.Web

namespace WebApplication1

{

// 注意: 如果更改此处的接口名称 "INorthwindService",也必须更新 Web.config 中对 "INorthwindService" 的引用。

[ServiceContract]

public interface INorthwindService

{

[OperationContract]

[WebGet(UriTemplate="HelloWorld",ResponseFormat=WebMessageFormat.Json)]

string HelloWorld()

}

}

7. 在客户端脚本js中实现对WCF的调用

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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 src="jquery-1.3.2-vsdoc.js" type="text/javascript">script>

下面分享一下在.NET4中如何通过jQuery调用WCF:

说明:这里的WCF仅仅是给ajax调用的中转站,通过它再去调用本地服务层接口或者后台的其他WCF服务。

1. 在App_Code中添加一个类文件(就是普通的class),比如叫HelloService.cs。

2. 在HelloService.cs中添加一个方法,再加上一些WCF的设置

[ServcieContract]与[OperationContract]是标准设置,这里将‘服务接口”与"服务实现"写在了一个类中,不是推荐做法。我们这里这样做,是因为需要的是一个ajax调用中转站,越简单越好。

这里需要注意的是AspNetCompatibilityRequirements,如果不进行这个设置,WCF就不走ASP.NET管线,ASP.NET的上下文信息就拿不到,比如:不加这个设置,HttpContext.Current就为null,最常用的场景就是根据HttpContext获取用户登录信息,对用户权限进行验证。对应于这个设置,在web.config的<system.serviceModel>中需要加上<serviceHostingEnvironment aspNetCompatibilityEnabled="true">,

*注意的地方:web.config中加了上面的设置后,所有的WCF服务实现都要设置AspNetCompatibilityRequirements属性。

3. 继续在web.config增加设置,在system.serviceModel/serviceHostingEnvironment中增加serviceActivations

relativeAddress就是WCF服务的地址名,service就是之前创建的HelloService类名,factory是关键,支持ajax调用,靠的就是System.ServiceModel.Activation.WebScriptServiceHostFactory。

好了,三步搞定WCF端的配置。这时在VS2010启动项目,就可以在浏览器中访问这个WCF了。

需要System.Web.Extensions.dll(可能需要AJAXExtensionsToolbox.dll)

网上下载ASPAJAXExtSetup

使用的时候把上面的2个dll放到bin下,并添加引用(一般服务器没有安装ASPAJAXExtSetup)

==============================

Web服务

==============================

using System

using System.Web

using System.Collections

using System.Web.Services

using System.Web.Services.Protocols

using System.Web.Script.Services

using System.Data

using System.Text.RegularExpressions

/// <summary>

/// addComment 的摘要说明

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]

public class manageComment : System.Web.Services.WebService

{

[WebMethod(EnableSession = true)]

public string CommentByID(string userName)

{

//支持session

}

public string DeleteHostComment(int replayID)

{

}

}

aspx页面设置web服务文件的路径

===============================

<asp:ScriptManager ID="smAddComment" runat="server">

<Services>

<asp:ServiceReference Path="~/WS/manageComment.asmx" />

</Services>

</asp:ScriptManager>

js调用

=================================

manageComment.CommentByID('名称')

manageComment.DeleteHostComment(1)

tip:调用的格式->[命名空间.]类名.方法名(参数1[,参数2……])


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9260324.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存