Windows Communication Foundation is released as. NET Framework 3.0. Therefore, only version 2008 and later can be used to create a WCF application.
WCF is an integration of existing distributed communication technologies, including COM/DCOM,. Net remoting, web services, WSE (upgraded version of web services), and MSMQ.
Now I decided to study WCF, So I practiced it with my own reference to http://www.cnblogs.com/jiagoushi/archive/2013/03/15/2962351.html in my blog.
1. Create a WCF project. We organize it according to the project structure actually used in the project.
Let me explain the structure of these projects.
- Consolehosting is a console application that carries the WCF Service. What do you say is bearer? To put it bluntly, you can access the WCF Service. Because a WCF application is created, it is equivalent to a robot. But if you don't give him a battery, he can't walk, and the bearer is equivalent to giving him power, it can run. The bearer methods of WCF can be divided into self hosting and IIS (access through IIS just like accessing a web site ).
- The contracts project is a class library project, which is used to store the WCF contract, that is, some interfaces.
- Services is also a class library project. It is used to store services that implement the contract. It is a class that implements interfaces.
- WebClient is a web project using MVC 4. It is used as a client to call the WCF Service.
- Webhosting is a web project that uses mvc4 to carry the WCF Service. Maybe you will say that there are two projects that carry the WCF, redundant. Yes, my purpose here is to explain that there are multiple bearer methods for the WCF.
First, add an interface to the contracts project, and then add two numeric calculation methods.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. servicemodel; namespace contracts {/* contract class * // define the WCF Service Agreement [servicecontract (name = "ical", namespace = "http://www.Chinaer.com")] public interface ical {[operationcontract] int add (int x, int y); int sub (int x, int y );}}
Note: The operationcontract operation contract is added to the add method, but this attribute is not added to the sub method.
Implement this contract in the service class, and add a class to services to implement this interface.
Using contracts; using system. collections. generic; using system. LINQ; using system. text; namespace services {/* service class */public class calservices: ical {public int add (int x, int y) {int num = x + y; return num ;} public int sub (int x, int y) {return x-y ;}}}
After implementing the service class, you need to host the WCF Service. This WCF is relatively simple and does not use the configuration file. It is carried by programming. Of course, we generally do not recommend this in actual projects.
First, it is carried through the console. In fact, the console carries the same programming code as the web, but the project type is different.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. servicemodel; using system. servicemodel. description; using contracts; using services; namespace consolehosting {class program {static void main (string [] ARGs) {using (servicehost host = new servicehost (typeof (calservices ), new uri ("http: // 127.0.0.1: 8081") {// serviceendpoint summary point contains Address bingding Bind the contract Contract ABC host. addserviceendpoint (typeof (ical), new wshttpbinding (), "calservice"); // Add service summary point if (host. description. behaviors. find <servicemetadatabehavior> () = NULL) {// determines whether the configuration file defines the element summary point servicemetadatabehavior metadata = new servicemetadatabehavior (); metadata. httpgetenabled = true; metadata. httpgeturl = new uri ("http: // 127.0.0.1: 8081/calservice/metadata"); // Add the metadata summary point host. descri Ption. behaviors. Add (metadata);} host. Opened + = delegate {console. writeline ("WCF has been started. Please press any key to finish !! ") ;}; If (host. State! = Communicationstate. Opened) {Host. open () ;}console. Read ();}}}}
After adding a host, you can view the service metadata in a browser. To view metadata, you must first start the console program.
Enter the service metadata address in the browser to access the metadata. If the code is correct, you can see the following results.
When the above metadata result is displayed, it means that WCF can be called normally. The WCF Service is released through metadata, while servicemedatabehavior is the behavior of metadata release.
Since the service has been released successfully, we can call the service on the client to check whether the result can be obtained.
You can see that the WCF Service is successfully called. I will introduce some simple processing in MVC.
First, I added an add method in homecontroller (the corresponding method will be called according to the Request Method) to call the WCF Service.
[HttpPost] public ActionResult Add (string first,string second) { CalServices client = new CalServices(); int number = client.add(Convert.ToInt32(first),Convert.ToInt32(second)); return Json(number,JsonRequestBehavior.AllowGet); } [HttpGet] public int Add () { return 0; }
The layout of the interface and the use of AJAX
The effect is as follows (the style is not pasted out)
Home Page
In this way, a simple program for accessing WCF is complete. The range of WCF is very wide. We will discuss it later. If you want to download source code: http://pan.baidu.com/share/link? Consumer id = 458336 & UK = 1610729480