怎样用js调用wcf服务

JavaScript021

怎样用js调用wcf服务,第1张

需要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……])

[OperationContract(Name="sayHelloJson")]

[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "sayHello", BodyStyle = WebMessageBodyStyle.Wrapped)]

String sayHello()

[OperationContract(Name = "SendMessageJson")]

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SendMessage/{Message}", BodyStyle = WebMessageBodyStyle.Wrapped)]

String SendMessage(String Message)

比如说第一个不带参数的:http://localhost:8000/Service/Json/sayHello

那么第二个带参数的应该怎么调用?(比如说参数是"abc")

我指的是在浏览器中或java的httpclient中,因为我准备在android上实现客户端,所以.net的调用方式就不必讲了。

你URITemplate已经设置了。Get方式。

我猜测一下调用的URL应该是: 网站URL/SendMessage/你好

另外建议你使用REST WCF自带的一个帮助页面,里面会给出更精确的调用示例。

在 Silverlight 端设置 SendTimeout 和 ReceiveTimeout 属性,经常在 Debug 时需要调试上好几分钟、或者放上几十分钟。

可以在 ServiceReferences.ClientConfig 中配置,不过个人习惯使用后台代码,毕竟配置最灵活:

BasicHttpBinding 在 Debug/ Release 时的代码配置

C# code

defaultBasicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.None)

{

Name = "DefaultBasicHttpBinding",

OpenTimeout = new TimeSpan(0, 1, 0),

CloseTimeout = new TimeSpan(0, 1, 0),

#if DEBUG

SendTimeout = new TimeSpan(0, 18, 00),

ReceiveTimeout = new TimeSpan(18, 18, 00),

MaxBufferSize = int.MaxValue,

MaxReceivedMessageSize = int.MaxValue

#else

SendTimeout = new TimeSpan(0, 8, 00),

ReceiveTimeout = new TimeSpan(0, 18, 00),

MaxBufferSize = int.MaxValue,

MaxReceivedMessageSize = int.MaxValue

#endif

}

CustomBinding

C# code

 

customBinding = new CustomBinding()

{

Name = "CustomBinding",

OpenTimeout = new TimeSpan(0, 1, 0),

CloseTimeout = new TimeSpan(0, 1, 0),

#if DEBUG

SendTimeout = new TimeSpan(0, 18, 00),

ReceiveTimeout = new TimeSpan(18, 18, 00),

#else

SendTimeout = new TimeSpan(0, 8, 00),

ReceiveTimeout = new TimeSpan(0, 18, 00),

#endif

}

customBinding.Elements.Add(new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement())

customBinding.Elements.Add(new HttpTransportBindingElement()

{

MaxBufferSize = int.MaxValue, MaxReceivedMessageSize= int.MaxValue

}

)