"ASP. Basic" WCF Getting Started tutorial one (what is WCF)?

Source: Internet
Author: User
Tags wsdl

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 using System.ServiceModel; 2  3 Namespace Wcfservice 4 {5     [ServiceContract] 6 Public     Interface Iuser 7     {8         [OperationContract] 9         string ShowName (string name);     }12 namespace WCFService15 {public     class User:iuser17 {All public         string showname (string name) 19< C10/>{20             String wcfname = string. Format ("WCF service, display name: {0}", name);             return wcfname;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"%> 2 3 & lt;! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 4 

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

"ASP. Basic" 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.