"Hello World" in WCF (1)

Source: Internet
Author: User

WCF is a communication framework that integrates many functions such as security and transactions. There are many concepts on the Internet. The following describes how to deeply analyze the internal execution process of WCF using the classic "Hello World" example as the central axis. First, we need to present the original project.

The structure of the entire solution is shown below:

From the above, we can see that the entire solution includes four project.1.taylor. contract: contract design class (Class Library), which defines contract 2. taylor. service: service class, implementing the ihelloworld contract (Class Library) 3. taylor. HOST: the host of the WCF Service (Windows console) 4. taylor. client: the WCF client (Windows console) is a very simple solution, and its functions are also very simple. It is to get a string from the server. The code for the four projects is pasted out below. Taylor. Contract. Contract Design Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
// Project Step 1: Create a contract
Namespace Taylor. Contract
{
[Servicecontract]
Public interface ihelloworld
{
[Operationcontract]
String getstring (string input );
}
} Note that you must add using system. servicemodel; reference Taylor. Service: implements the contract, and the service class using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
Using Taylor. Contract;
// Step 2: implement the contract
Namespace Taylor. Service
{
Public class myservice: ihelloworld
{
# Region Implementation interface definition method (implementation contract)
Public String getstring (string input)
{
Return "your input string is:" + input;
}
# Endregion
}
}
Note that you must add a reference to the user's usystem. servicemodel; Taylor. Contract; file to the user's system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using Taylor. Contract;
Using Taylor. Service;
Using system. servicemodel;
// Step 3: create a service
Namespace Taylor. Host
{
Class Program
{
Static void main (string [] ARGs)
{
Beginrequest ();
}
Static void beginrequest ()
{
Using (servicehost host = new servicehost (typeof (myservice )))
{
Host. Opened + = delegate
{
Console. writeline ("server: begins to listen request ");
};
Host. open ();
Console. Read ();
}
}
}
}
Note that the app. config file of the Host referenced by system. servicemodel. Taylor. Contract. Taylor. service is as follows: <? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. servicemodel>
<Services>
<Service name = "Taylor. Service. myservice">
<Endpoint address = "" binding = "basichttpbinding" Contract = "Taylor. Contract. ihelloworld"> </Endpoint>
<Host>
<Baseaddresses>
<Add baseaddress = "http: // localhost: 9876/helloworld"/>
</Baseaddresses>
</Host>
</Service>
</Services>
</System. servicemodel>
</Configuration> it is very simple and has been described in detail on the Internet. Not to mention Taylor. Client: the client implements myclient. CS using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. servicemodel;
Using system. servicemodel. channels;
Using Taylor. Contract;
Namespace Taylor. Client
{
Class myclient: clientbase <ihelloworld>, ihelloworld
{
Public myclient ()
: Base ()
{}
Public String getstring (string input)
{
Return this. Channel. getstring (input );
}

}
}

Client Main Function

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace Taylor. Client
{
Class Program
{
Static void main (string [] ARGs)
{
Using (myclient client = new myclient ())
{
Console. writeline ("client:" + client. getstring ("Hello World "));
Console. Read ();
}
}
}
}

Client app. config implementation <? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. servicemodel>
<Client>
<Endpoint address = "http: // localhost: 9876/helloworld" binding = "basichttpbinding" Contract = "Taylor. Contract. ihelloworld"> </Endpoint>
</Client>
</System. servicemodel>
</Configuration> The following is the running result: Start the server: Start the client: A simple program. The internal execution process of this simple program will be analyzed step by step. Let people have a direct understanding of the internal running process of WCF. In this way, you can get twice the result with half the effort! Source code can be downloaded here/files/tylerlan/tyler.firstsolution.rar

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.