No nonsense WCF Getting started tutorial one [what is WCF]

Source: Internet
Author: User

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:

1 usingSystem.ServiceModel;2 3 namespaceWcfservice4 {5 [ServiceContract]6      Public InterfaceIuser7     {8 [OperationContract]9         stringShowName (stringname);Ten     } One } A  -  - namespaceWcfservice the { -      Public classUser:iuser -     { -          Public stringShowName (stringname) +         { -             stringWcfname =string. Format ("WCF service, display name: {0}", name); +             returnWcfname; A         } at     } -}

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"%>2 3<! 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>78<body>9<form id="Form1"runat="Server">Ten<asp:textbox id="txtname"runat="Server"></asp:textbox><br/> One<asp:button id="btnsubmit"runat="Server"text="Testing WCF Services"onclick="Btnclick"/> A</form> -</body> - the  - usingSystem; - usingSystem.Collections.Generic; - usingSystem.Linq; + usingsystem.web; - usingSystem.Web.UI; + usingSystem.Web.UI.WebControls; A  at //referencing the namespace of a WCF service - usingWcfclient.wcfservice; -  - namespacewcfclient - { -      Public Partial classWCFTest:System.Web.UI.Page in     { -         protected voidPage_Load (Objectsender, EventArgs e) to         { +  -         } the  *         protected voidBtnclick (Objectsender, EventArgs e) $         {Panax NotoginsengUserclient user =Newuserclient (); -             stringresult = user. ShowName ( This. txtName.Text); the Response.Write (result); +         } A     } the}

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.

Eight, note: because the online introductory tutorials are relatively few, this tutorial only to do the introductory, will continue to talk about some other things to get started, in-depth please see the garden: Artech elder brother's article.

Nine, code download:

  WCF Tutorials

X. Copyright

  Reproduced in the original source:Http://www.cnblogs.com/iamlilinfeng

No nonsense WCF Getting started tutorial one [what is WCF]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.