One of the WCF Service-Oriented Application series: Hello WCF

Source: Internet
Author: User

Windows Communication Foundation is a SOA architecture on the Microsoft platform. It is used to build distributed and interactive applications.Program. It unifies asmx,. Net remoting, And the development model of enterprise services. It provides a single programming model for various applications, such as configuration-driven protocol selection, message format, and process allocation.

Development Environment: Visual Studio 2010 + Net Framework 4.0.

In this chapter, we use a simple demo to create a WCF program.
1. Open vs2010, select create WCF program in C # language, select WCF Service library, change solution name to hellowcf and project name to helloservicelibrary, and click OK.
2. Delete the iservice1.cs and services1.cs files generated in the helloservicelibrary project.
3. Create an ihellowcf interface file,CodeAs follows:

  //Operationcontract is a service contract
[Servicecontract]
Public InterfaceIhellowcf
{
//Operationcontract is a method contract
[Operationcontract]
StringGetmessage (StringMSG );
}

 

4. Create a hellowcf file with the following code:

    Public     Class  Hellowcf: ihellowcf
{
Public String Getmessage ( String MSG)
{
Return String . Format ( " The server received ed message is: {0} " , MSG );
}
}

 

5. Modify the app. config file in helloservicelibrary:
Modify the service name to <service name = "helloservicelibrary. hellowcf">
Modify the client contract to <endpoint address = "" binding = "wshttpbinding" Contract = "helloservicelibrary. ihellowcf">
Modify the service address to <add baseaddress = "http: // localhost: 8732/design_time_addresses/helloservicelibrary/hellowcf/"/>

 

The configuration is as follows:

  <?  XML version  =  "  1.0  "  Encoding  =  "  UTF-8  "     ?>  
< Configuration >

< System. Web >
< Compilation debug = " True " />
</ System. Web >
<! -- When deploying the service library project, the content of the config file must be added to the host ' S
App. config file. system. configuration does not support config files For Libraries. -->
< System. servicemodel >
< Services >
< Service name = " Helloservicelibrary. hellowcf " >
< Endpoint address = "" Binding = " Wshttpbinding " Contract = " Helloservicelibrary. ihellowcf " >
< Identity >
< DNS Value = " Localhost " />
</ Identity >
</ Endpoint >
< Endpoint address = " MEX " Binding = " Mexico httpbinding " Contract = " Imetadataexchange " />
< Host >
< Baseaddresses >
< Add baseaddress = " Http: // localhost: 8732/design_time_addresses/helloservicelibrary/hellowcf/ " />
</ Baseaddresses >
</ Host >
</ Service >
</ Services >
< Behaviors >
< Servicebehaviors >
< Behavior >
<! -- To avoid disclosing metadata information,
Set The value below False And remove the metadata endpoint above before deployment -->
< Servicemetadata httpgetenabled = " True " />
<! -- To receive exception details In Faults For Debugging purposes,
Set The value below True . Set False Before deployment
To avoid disclosing exception information -->
< Servicedebug includeexceptiondetailinfaults = " False " />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
</ System. servicemodel >

</ Configuration >

 

6. Create a console application client, add a service reference, and change the namespace to helloservicelibrary.

7. Add code to the main function of the program class.

Code

    Static     Void  Main (  String  [] ARGs)
{
Console. writeline ( " ------------------ Hellowcfclient begin ------------------ " );
Helloservicelibrary. hellowcfclient Client = New Helloservicelibrary. hellowcfclient ();

Console. writeline ( " The client sent message is: Hello WCF " );
Console. writeline (client. getmessage ( " Hello WCF " ));

Client. Close ();

Console. writeline ( " ------------------ Hellowcfclient end ------------------ " );
Console. Readline ();
}

 

8. Run the debugging program F5. On the console, we will see the result returned by the client calling the WCF server.

 
  ------------------Hellowcfclient begin------------------
The client sent messageIs: Hello WCF
The server received ed messageIs: Hello WCF
------------------Hellowcfclient end------------------

 

So far, a simple WCF application has been created. The following chapter describes the Contract Design of WCF in detail.

Click to download the demo.

 

 
  Author: Xinhai julan
Source: http://Xinhaijulan.cnblogs.com
The copyright is shared by the author and the blog. You are welcome to reprint the copyright. However, you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

 

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.