Learn how to call the WCF Service and call the wcf Service
1. Development Tools call WCF
This method is very convenient and simple, and a lot of work VS will help us complete. I believe everyone will not be unfamiliar with this method. Here is a brief introduction. Open VS and add service reference in the project: the node information about the service is automatically declared in config, so that VS creates a proxy for calling the service:
ServiceReference1.Service1Client poxy = new ServiceReference1.Service1Client (); poxy. The method in the service.
2. C # dynamically call WCF
This method is more practical. You can use tools or code to generate the proxy class generatedProxy. cs and the configuration file app. config to interact with WCF. Manual service reference is not required. Generate proxy class
In vs tool:
Tool -- svcutil:
Parameter:/language: cs/out: generatedProxy. cs/config: app. config http: // localhost: 9002/Service1.svc
With this proxy class, you can do a good job! You can use this proxy class to call WCF.
In this way, if the methods of multiple services are the same, the addresses are different (distributed on different servers ). This call is a good choice! In addition, we can use the channel factory to generate client service object instances, but the premise is still the help of the proxy class generated above. You can refer to Daniel Robin's article (the following link is provided ).
3. JS (jQuery) calls WCF
The idea of implementation here is similar to that of ASP. NET Ajax, but some work needs to be done by ourselves, and this method is flexible. FirstWCFOn: We need to make the following declaration before the class and method:
[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; } }
NextConfiguration File:
<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>
After the preparation is complete, you canFrontend callNow:
<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 (. d ). length> 0) {alert (. d) ;}else {alert ("server timeout") ;}}}</script> <style type = "text/css" ># content {height: 181px; width: 549px;} # title {width: 544px ;} </style>
You can read dudu's article (the following link is provided ). In this way, we can use jQuery to call wcf.
Reference learning materials:
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