WCF tutorial series (1)-create the first WCF Program

Source: Internet
Author: User

As one of the three core technologies of Microsoft. NET 3.5, WCF does not have the beautiful appearance of WPF.
However, it is distributedProgramPowerful Tool
However, currently, there is very little information about WCF.
I hope this seriesArticleTo help you get started as soon as possible
Let's introduce my development environment first.
Operating System: Windows Vista Business
Compiler: Visual Studio 2008 (English Professional Edition)
The three core components of WCF are ABC.
That is, a represents address-where (where is the object)
B indicates binding-How (through which the object is obtained)
C stands for contact (contract)-What (what is the Defined Object and how to manipulate it)
For other theoretical knowledge, see Programming WCF Service.
Or the essential windows commmunication Foundation, which was just published in March.
Now we use the In Action Method to teach you how to create the first WCF program.
Create an empty solution as shown in

Right-click solution hellowcf, select Add-> new project, select console application template, and select project name host (server side)

Right-click the host project, select Add-> new item, and select the WebService template (the file is named hellowcfservice)

The ihellowcfservice. CS, hellowcfservice. CS, and App. config files will be created.
The ihellowcfservice. CS code is as follows:

Using system. servicemodel;

Namespace host
{
[Servicecontract]
Public interface ihellowcfservice
{
[Operationcontract]
String hellowcf (string message );
}
}

Hellowcfservice. CSCodeThe implementation is as follows:

Using system;

Namespace host
{
Public class hellowcfservice: ihellowcfservice
{
Public String hellowcf (string message)
{
Return string. Format ("message you received at {0}: {1}", datetime. Now, message );
}
}
}
In principle, you do not need to change the app. config file, but the address is too long.
(The default value is baseaddress = http: // localhost: 8731/design_time_addresses/host/hellowcfservice /)
To baseaddress = http: // localhost: 8731/hellowcfservice/
And modify the program. CS file
Using system;
Using system. servicemodel;

Namespace host
{
Class Program
{
Static void main (string [] ARGs)
{
Using (servicehost host = new servicehost (typeof (host. hellowcfservice )))
{
Host. open ();
Console. Readline ();
Host. Close ();
}
}
}
}
Compile and generate the host.exe File
Next, create the client program as the console application project client.
Start the host.exe File
Right-click the client project and choose add service reference...
Enter the server address (the baseaddress address we set earlier) in the textbox of address, and click Go
The services on the target server are displayed, as shown in

This step allows you to create a client proxy (using client. hellowcf;) and the configuration file app. config.
Modify the client program as follows:
Using system;
Using client. hellowcf;

namespace client
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> hellowcf. hellowcfserviceclient proxy = new hellowcfserviceclient ();
string STR = proxy. hellowcf ("welcome to the WCF village! ");
console. writeline (STR);
console. readline ();
}< BR >}< br> you can get the Server Object.

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.