Copyright This digest since Http://www.cnblogs.com/iamlilinfeng himself only to be a porter
Reprint please indicate the source:Http://www.cnblogs.com/iamlilinfeng Thanks moderator Li Lin Share
I. Overview
Windows Communication Foundation (WCF) is a set of data communication application development interfaces developed by Microsoft, which can be translated as a Windows communication interface and it is. NET Framework is part of the. Introduced by the. NET Framework 3.0.
The ultimate goal of WCF is to send and receive messages between customers and services through a process or a different system, through a local network, or over the Internet.
WCF incorporates the capabilities of Web services,. NET Remoting, Message Queuing, and enterprise services and integrates them in Visual Studio.
WCF is dedicated to service-oriented development.
Application development and service-oriented development based on ASP.
in ASP. NET-based application development, we access the application server from the client's browser, then connect to the database server through the database connection in the application server, read or manipulate the data, and sometimes one more file server. As you can see, basically all applications are placed on a single server, but for one, it is difficult for one server to support all applications due to business needs (such as interacting with external systems). Let's look at the following figure:
The client uses a browser to access server A, and server A is deployed in Servers B, C, D ... for business needs with a variety of other applications. And then communicate with each other through WCF technology, visit each other ... However, the benefits of service-oriented are not only here, but it also provides interoperability for different operating systems in different languages. Since this article is not an article about SOA, interested students can see: SOA
Third, the first WCF program
1. Create a new blank solution and create a new project in the solution with the project type: WCF Service Application. After Setup is complete as shown:
2. Delete the system-generated two files IService1.cs and service1.svc.
3. Add a custom WCF "service file" User.svc, at which point vs2010 will automatically generate the WCF interface file IUser.cs, we define the WCF method ShowName in Iuser, and implement the method of the interface in User.svc.cs.
The code is as follows:
1UsingSystem.ServiceModel;23NamespaceWcfservice4{5[ServiceContract]6PublicInterfaceIuser7{8[OperationContract]9String ShowName (Stringname);10}11}121314NamespaceWcfservice15{16PublicClassUser:iuser17 {18 public string showname ( name) 19 {20 string wcfname = string. Format ( "WCF service, display name: {0} 21 return wcfname;< Span style= "color: #008080;" >22 }23 }24}
As you can see, the difference between an interface in WCF and a normal interface is only two contexts, and the rest is the same as our normal learning interface. Define this context to add a reference to the System.ServiceModel.
[ServiceContract], to indicate that the interface is a WCF interface, if not added, it will not be called externally.
[OperationContract], to illustrate that the method is a WCF interface method, without adding the words ibid.
At this point our first WCF service program is set up, User.svc "set as Start Page", and then F5 run it, as shown, VS2010 automatically calls the WCF Client Test tool so we can test the program:
We double-hit the ShowName () method, which appears as:
Enter the parameter "your name" in the value in the request window, then click "Call", in the response window will appear the return value "WCF service, display name: Your name", indicating the success of the test, click the following XML can also see the XML data transmission. We have now established the application and business logic for the service, which is a very simple way to print names, and the tests have been successful. So how do we use it?
Four, the scene
The scenario we designed is a scenario that is often applied in production, hosting a WCF program on top of IIS. Suppose the scenario is as follows: a server and B server. We have just built the WCF program "deployed" on the B server (this tutorial, A/b server is put on my own machine), our goal is to access the server-a application of the B-server WCF program, to implement server-side application communication.
V. Host the WCF program over IIS on Server B
Let's first publish the WCF application and then deploy it on the B server's IIS, as shown in:
The right mouse button browses the uesr.svc and appears as shown in the viewer, stating that the service deployment was successful.
The http://localhost/User.svc?wsdl in is the service address that we want to refer to.
Vi. creating a reference to a service on the client [a server]
Here we take the Web application as an example, to establish the physical address of the native, but you can imagine that B server is a remote computer, localhost for a different IP address.
A new solution is created, and the project for the ASP. The name is: WCFClient, as shown in:
(1) Create a new ASP. NET page named: wcftest.aspx.
(2) Add a reference to the service deployed in step fifth. As shown in the following:
The form that adds the service reference is popped up, as shown in:
We're going to write the address service path for the WCF service that we hosted on IIS, here: http://localhost/User.svc?wsdl, fill in the namespace wcfservice[this namespace is referenced in the following client] and then click "Go" -"OK" button. At this point we have completed a reference to the service. We look at the solution again, there are more service references folder, through the Explorer open after the more files, these files for the client to the service side of the call, now do not control him.
Seven , using the WCF service-side approach
The code for Wcftest.aspx is as follows:
1 <%@ Page language="C#"Autoeventwireup="True"Codebehind="WCFTest.aspx.cs"Inherits="Wcfclient.wcftest"%>23 <! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en""Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">4 "Http://www.w3.org/1999/xhtml">5 "Server">6 <title></title>7 8 <body>9 <form id="Form1"Runat="Server">Ten <asp:textbox id="Txtname"Runat="Server"></asp:textbox><br/><asp:button id="Btnsubmit"Runat="Server"Text="Testing WCF Services"Onclick="Btnclick"/></form></body>1516UsingSystem;17UsingSystem.Collections.Generic;18UsingSystem.Linq;19Usingsystem.web;20UsingSystem.Web.UI;21stUsingSystem.Web.UI.WebControls;2223//Referencing the namespace of a WCF service24UsingWcfclient.wcfservice;2526NamespaceWCFClient27{28PublicPartialClassWCFTest:System.Web.UI.Page29{30Protectedvoid Page_Load (Objectsender, EventArgs e)31{3233}3435 protected void Btnclick (object sender, EventArgs e) 36 {37 userclient user = new Userclient (); string result = user. ShowName (this Response.Write (result) }41 }42}
The Userclient class above is the client proxy class for the server user class that is generated when the reference is added, and the generic client proxy class name will be **client. Let's run a look at the effect.
In the example above, we have completed the invocation of the application of a server to the method provided by WCF in the B server. This example is relatively simple, for the students who often develop B/s structure applications are better understand.
WCF tutorial easy to get started