1.开发工具调用WCF javascript
这中方法很方便也很简单,不少工做VS就帮咱们完成了。相信你们也不会对这种方法陌生。这里简单提一下。打开VS,在项目中添加服务引用:
在config中自动声明了有关服务的节点信息,这样VS就建立了调用服务的代理:css
2.C#动态调用WCFhtml
这个方法比较实用,能够经过工具或代码生成代理类generatedProxy.cs和配置文件app.config,来和WCF进行交互。不须要人为的手动进行服务的引用。生成代理类 java
vs工具中:jquery
工具--svcutil:
参数: /language:cs /out:generatedProxy.cs /config:app.config http://localhost:9002/Service1.svcweb
有了这个代理类,工做就好作啦!经过这个代理类就能够调用WCF了。ajax
这样,若是多个服务的方法相同,只是address不一样(分布在不一样的服务器)。这样的调用是很不错的选择! 除此以外,咱们能够采用通道工厂的方式生成客户端服务对象实例,可是前提仍是须要上面生成的代理类的帮助。你们能够参看大牛Robin的文章(下面有连接)。json
3.JS(jQuery)调用WCF服务器
这里实现的思想和ASP.NET Ajax的有些相似,只不过有一些工做须要咱们本身来完成,而且这个方法很灵活。 首先是WCF上:咱们要在类和方法前进行以下的声明:app
[ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class WCFservice { [OperationContract] [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] public string SayHello(string name) { return "hello:"+name; } }
接着就是配置文件:
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="AllenBehavior"> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <services> <service name="jqueryWCF.WCFservice"> <endpoint address="" behaviorConfiguration="AllenBehavior" binding="webHttpBinding" contract="jqueryWCF.WCFservice" /> </service> </services> </system.serviceModel>
<behavior name="AllenBehavior"><enableWebScript /></behavior>
准备工做作好后就能够前台调用了:
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>wcf</title> <script language="javascript" type="text/javascript" src="jquery.js"></script> <script language="javascript" type="text/javascript"> function sayhello(){ var name = $("#name").val(); $.ajax({ type: 'post', url: '/WCFservice.svc/SayHello', contentType: 'text/json', data: '{"name":"'+name+'"}', success: function(msg) { var a = eval('('+msg+')'); if(String(a.d).length>0){alert(a.d);} else{alert("服务器超时");} } }); } </script> <style type="text/css"> #content{height: 181px;width: 549px;} #title{width: 544px;} </style> </head> <body> <form id="form1" runat="server"> <div> name:<input type="text" id="name" /> <br /> <input type="button" value="hello" onclick="sayhello();" /> </div> </form> </body> </html>
这里的一些注意事项你们能够但看dudu的文章(下面有连接)。这样,咱们就能够利用jQuery调用wcf了。
参考学习资料:
Robin:http://www.cnblogs.com/jillzhang/archive/2008/07/26/1252171.html
dudu:http://www.cnblogs.com/dudu/archive/2009/07/14/1523082.html
liulun:http://www.cnblogs.com/liulun/articles/1425382.html