Step by step from getting started to being proficient in building a simple getting started example of WCF (suitable for reading by friends who are not at all)

Source: Internet
Author: User

Windows Communication Foundation (WCF) is a unified framework for creating secure, reliable, and interactive distributed transaction processing applications.Program.

 

When I was learning a new technology, I always liked to know how to use and configure it from the very beginning, and then I learned more about it. Next we will build a simple WCF program step by step through the image (this example is taken from Microsoft and is very simple ):

 

My environment is: vsts2008 SP1

 

1. Create an empty solution named WCF, right-click it, and add a"WCF Service LibraryProject

 

The result is as follows:

 

 

2. There are two default files in the WCF project: iservice1.cs and service1.cs. We will slightly modify the two files below.

Iservice1.cs

 

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. runtime. serialization; <br/> using system. servicemodel; <br/> using system. text; </P> <p> namespace wcfservicelibrary <br/> {<br/> // Note: If you change the Interface Name "iservice1", you must update the app. references to "iservice1" in config. <Br/> [servicecontract] <br/> Public interface iservice1 <br/> {<br/> [operationcontract] <br/> string getdata (string value ); </P> <p> [operationcontract] <br/> compositetype getdatausingdatacontract (compositetype composite); </P> <p> // task: add service operation here <br/>}</P> <p> // use the data protocol described in the following example to add a composite type to the service operation <br/> [datacontract] <br/> public class compositetype <br/>{< br/> bool boolvalue = false; <br/> string stringvalue = "friend "; </P> <p> [datamember] <br/> Public bool boolvalue <br/>{< br/> get {return boolvalue ;} <br/> set {boolvalue = value ;} <br/>}</P> <p> [datamember] <br/> Public String stringvalue <br/>{< br/> get {return stringvalue ;} <br/> set {stringvalue = value ;}< br/>}< br/>

 

Service1.cs

 

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. runtime. serialization; <br/> using system. servicemodel; <br/> using system. text; </P> <p> namespace wcfservicelibrary <br/> {<br/> // Note: If you change the class name "iservice1", you must update the app. references to "iservice1" in config. <Br/> public class service1: iservice1 <br/>{< br/> Public String getdata (string value) <br/>{< br/> return string. format ("You entered: {0}", value); <br/>}</P> <p> Public compositetype getdatausingdatacontract (compositetype composite) <br/>{< br/> If (composite. boolvalue) <br/>{< br/> composite. stringvalue + = "Hello! "; <Br/>}< br/> return composite; <br/>}< br/>

 

Two methods are defined here for demonstration, calls of common type parameters and return values, and calls of passing and returning compound types.

3. Click F5 to run the"WCF ServiceLibrary, Vs will start the "WCF debugging client" to debug the WCF Service

On the right side of the debugging window, we can see some basic information about our "WCF Service library", including the corresponding configuration file information. Double-click the method to open the method in the window on the right. Enter relevant parameters and click "call" to get the returned results in the response.

 

 

4. We will add a web application for this solution as a demo to call this WCF Service.

The Web application is added. Right-click the project and click "add service reference"

 

 

In the pop-up window, click "Discover" to find the defined WCF Service. Select it and click "OK ".

 

 

5. Drag four controls, one text box, one tag, and two buttons into the web application page. The buttons are named "call" and "Compound call" respectively"

 

 

Add a click event for the two buttons,CodeAs follows:

 

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; </P> <p> namespace webapplication <br/> {<br/> Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{</P> <p >}</P> <p> protected void button#click (Object sender, eventargs E) <br/>{< br/> servicereference1.service1client client = new servicereference1.service1client (); </P> <p> label1.text = client. getdata (textbox1.text); <br/>}</P> <p> protected void button2_click (Object sender, eventargs E) <br/>{ <br/> servicereference1.service1client Client client = new servicereference1.service1client (); </P> <p> servicereference1.compositetype paramct = new webapplication. servicereference1.compositetype (); <br/> paramct. boolvalue = true; <br/> paramct. stringvalue = textbox1.text; </P> <p> servicereference1.compositetype returnct = client. getdatausingdatacontract (paramct); <br/> label1.text = returnct. stringvalue; <br/>}< br/>

 

Now, set the web application as the startup Item and click F5 to test the program results. The results and file structure are as follows:

Enter content in the text box and click "call". The returned results are displayed in the label.

 

 

From: http://www.cnblogs.com/TerryFeng/archive/2009/03/30/1424766.html

 


Copyright 2010 Feng ruitao (terryfeng)

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.