No-nonsense WCF getting started tutorial 1 [What is WCF]

Source: Internet
Author: User

I. Overview

  Windows Communication Foundation (WCF) is a group of data Communication application development interfaces developed by Microsoft. It can be translated as Windows Communication interfaces, which are part of the. NET Framework. Introduced by. NET Framework 3.0.

The final goal of WCF is to send and receive messages between customers and services through processes or different systems, through a local network, or through the Internet.

WCF combines the functions of Web Services,. net Remoting, message queue, and Enterprise Services and integrates them in Visual Studio.

WCF is specially used for service-oriented development.

2. Application Development and service-oriented development based on Asp.net

In Asp.net-based application development, we access the application server through the client's browser, and then connect to the database server through the database connection in the application server to read or operate data, sometimes there may be one more file server. It can be observed that basically all applications are placed on one server, but for one, due to business needs (such as interaction with external systems ), it is difficult for a server to support all applications. Let's look at the figure below:

The client uses A browser to access server A. server A is deployed on server B, server C, and server D for business needs .... then communicate with each other through the WCF technology and access each other... however, the benefits of service orientation are not only here, but also the interactivity of different operating systems in different languages .. since this article is not an article about SOA, if you are interested, refer to: SOA

3. The first WCF Program

1. Create a new blank solution and create a project in the solution. The project type is: WCF Service Application. As shown in figure:

2. Delete the IService1.cs and Service1.svc files generated by the system.

3. add a custom WCF [service file] User. svc. In this case, vs2010 automatically generates the WCF interface file IUser. cs, we define the WCF method ShowName in IUser, in User. svc. cs implements the method of this interface.

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 ); 10} 11} 12 13 14 namespace WCFService15 {16 public class User: IUser17 {18 public string ShowName (string name) 19 {20 string wcfName = string. format ("WCF Service, display name: {0}", name); 21 return wcfName; 22} 23} 24}

As you can see, the difference between an interface in WCF and a common interface lies in only two contexts, and the other interfaces are the same as those we learn normally. Define the context to add a reference to System. ServiceModel.

[ServiceContract] indicates that the interface is a WCF interface. If it is not added, it cannot be called externally.

[OperationContract] to indicate that this method is a method of the WCF interface. If it is not added, it is the same as above.

In this case, our first WCF Service Program has been set up. svc "set as start page", and then F5 run it, as shown in, VS2010 automatically calls the client test tool of WCF so that we can test the program:

Double-click the ShowName () method in, as shown in:

Enter the parameter "Your name" in the value in the request window, and click "call". In the response window, the returned value "WCF Service, display name: Your name" appears ", the test is successful. Click XML below to see the XML data transmission. Now we have established the application and business logic of the service, that is, a very simple method to print the name, and the test is successful. So how can we use it?

Iv. Scenarios

The scenario we designed is a scenario that is often used in production, and the WCF program is hosted on IIS. The following is A scenario: Server A and server B. We "deploy" the WCF program we just created on server B (both server A and server B in this tutorial are on my own machine ), our goal is to access the WCF program of server B in the application program of server A to implement application program communication on the server.

5. host the WCF program on the IIS server B.

  First, release the WCF application and deploy it on IIS of server B, as shown in:

 

Right-click Uesr. svc and check that the service is successfully deployed, as shown in figure.

Http: // localhost/User. svc? Wsdl is the service address to be referenced.

6. Create service reference on Client [server]

  Here, we use Web applications as an example. The physical address is the local host, but we can imagine that server B is a remote computer and localhost is another IP address.

Create a solution and a project for creating an ASP. NET Web application. Name it WCFClient, as shown in:

 

(1) Create an Asp.net page named WcfTest. aspx.

(2) Add a reference to the Service deployed in step 5. As shown in:

The form for adding a service reference is displayed, as shown in:

In the address, enter the address service path of the WCF Service that we host on IIS. Here: http: // localhost/User. svc? Enter WCFService in the namespace field [reference this namespace in the following client] and then click "go" --> "OK. At this point, we have completed the reference to the service. Let's check the solution again. There is a Service References folder in it, and some files are added after being opened through the resource manager. These files are used for client-side calls to the server. Now you don't have to worry about them.

 VII.Methods for using the WCF Server

  The WcfTest. aspx code 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 

The above UserClient class is the Client proxy class of the server User class generated when a reference is added. Generally, the Client proxy class name is ** Client. Let's run it to see the effect.

In the preceding example, the method provided by the Application of server A to the WCF of server B is called. This example is relatively simple. It is easier for students who often develop B/S structure applications.

8. Description:Because there are few entry-level tutorials on the Internet, this tutorial only serves as an entry-level course. I will continue to talk about other entry-level things later. For more information, see Artech's article in the garden.

9. Download the Code:

  10 WCF tutorial

Copyright

  Reprinted please indicate the source: http://www.cnblogs.com/iamlilinfeng

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.