WCF Learning Notes (1)-A complete example

Source: Internet
Author: User

First, the development environment

ide:vs2013

Os:win10

Iis:iis 10

II. development Process 1. Project structure

2. Add a WCF Program

3. Delete the two files automatically generated by the system IService1.cs and SERVICE1.SVC

4. Add a custom WCF service file

5. In the IUser.cs file, define the method name

Note:

[ServiceContract] To illustrate is a WCF interface, not added, cannot be called externally

[OperationContract] to indicate that the method is a method of a WCF interface, not to be called externally

namespace wcfservice{    //  Note: Using the rename command on the Refactor menu, you can change the interface name "Iuser" in code and configuration files at the same time.      [ServiceContract]    publicinterface  iuser    {        [ OperationContract]        string showname (string  name);}    }

6. In the User.svc.cs file, implement the specific method

namespacewcfservice{//Note: Using the rename command on the Refactor menu, you can change the class name "User" in Code, SVC, and configuration files at the same time. //Note: In order to start the WCF test client to test this service, select User.svc or User.svc.cs in Solution Explorer and start debugging.      Public classUser:iuser { Public stringShowName (stringname) {            stringWcfname =string. Format ("WCF service, display name: {0}", name); returnWcfname; }    }}

7. Set User.svc as the startup item and run the project, VS will automatically call the WCF Client Test tool

8. Click the name of the method you want to test and enter the value in the request body

9. Click Invoke and the return value of the method will appear in the corresponding window.

10. Click on XML to see the original data format of the transfer

(1) Request message

<S:envelopexmlns:s= "http://schemas.xmlsoap.org/soap/envelope/">  <S:header>    <ActionS:mustunderstand= "1"xmlns= "Http://schemas.microsoft.com/ws/2005/05/addressing/none">Http://tempuri.org/IUser/ShowName</Action>  </S:header>  <S:body>    <ShowNamexmlns= "http://tempuri.org/">      <name>Kimisme</name>    </ShowName>  </S:body></S:envelope>

(2) Response message

<S:envelopexmlns:s= "http://schemas.xmlsoap.org/soap/envelope/">  <S:header/>  <S:body>    <Shownameresponsexmlns= "http://tempuri.org/">      <Shownameresult>WCF service, display name: Kimisme</Shownameresult>    </Shownameresponse>  </S:body></S:envelope>
Third, the application

We deploy the above WCF program to IIS, and then implement server A's application to access the WCF program on Server B to implement server-side application communication

1. Running a WCF program on IIS

Note: Svcuti is located in the directory: C:\Program Files (x86) \microsoft Sdks\windows\v8.0a\bin\netfx 4.0 Tools\svcutil.exe

2. Create a new Web project and create a new generic handler wcftest.ashx
namespacewcfclient{/// <summary>    ///Summary description of WCFTest1/// </summary>     Public classWcftest1:ihttphandler { Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; Userclient User=Newuserclient (); stringmsg = user. ShowName ("Kimisme"); Context.        Response.Write (msg); }         Public BOOLisreusable {Get            {                return false; }        }    }}

Add a http://localhost:9003/User.svc?singleWsdl reference to a pair in the project at the same time

3. Run vs can see the following page

4. Reference Articles

Http://www.tuicool.com/articles/uAjMvyn

Http://www.cnblogs.com/iamlilinfeng/category/415833.html

WCF Learning Notes (1)-A complete example

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.