Create a simple WCF service using C #

Source: Internet
Author: User

First, the development environment

Operating system: Windows 10

Development environment: VS2015

Programming Language: C #

IIS version: 10.0.0.0

Ii. adding WCF services, Internet Information Services (IIS)

1. Go to "Control Panel", open "programs and Features", click "Enable or disable Windows features" in the upper left corner, select "WCF service" in the ". NET Framework 4.6 Advanced Services" sub-node, as shown in:

2. Find "Internet information Services" Again, and select the node as shown:

3. Click the "OK" button to install the services and components and wait for the installation to complete.

III. Create a new WCF service library

1. Use VS2015 to create a new WCF service library and change the project name to "Mywcfservice" as shown in:

2. In Solution Explorer, rename the "IService1" interface, "Service1" class to "Imywcfservice" and "Mywcfservice" respectively, as shown in:

VS2015 will create "IService1" interface, "Service1" class respectively, "IService1" interface with GetData, Getdatausingdatacontract method, and use "Service1" class to implement the interface, The code is as follows:

"IService1" Interface:

    //Note: Using the rename command on the Refactor menu, you can change the interface name "Imywcfservice" in code and configuration files at the same time. [ServiceContract] Public InterfaceIService1 {[OperationContract]stringGetData (intvalue);        [OperationContract]        Compositetype getdatausingdatacontract (Compositetype composite); //TODO: Add your service actions here    }    //use the data contract described in the following example to add a composite type to a service operation. [DataContract] Public classCompositetype {BOOLBoolvalue =true; stringStringValue ="Hello"; [DataMember] Public BOOLBoolvalue {Get{returnBoolvalue;} Set{Boolvalue =value;} } [DataMember] Public stringStringValue {Get{returnStringValue;} Set{stringvalue =value;} }    }

"Service1" class:

    //Note: Using the rename command on the Refactor menu, you can change the class name "Service1" in code and configuration files at the same time.      Public classService1:iservice1 { Public stringGetData (intvalue) {            return string. Format ("You entered: {0}", value); }         Publiccompositetype getdatausingdatacontract (Compositetype composite) {if(Composite = =NULL)            {                Throw NewArgumentNullException ("Composite"); }            if(composite. Boolvalue) {composite. StringValue+="Suffix"; }            returnComposite; }    }

3. Move the mouse over the project "Mywcfservice" in Solution Explorer and right click on the mouse, pop up the context menu, and in the menu select "Publish", the "Publish WCF Service" dialog box appears, as shown in:

Select "D:\WCF" in the target location, other press the default, click the "Publish" button, you can generate the file as shown in the "D:\WCF" folder:

Iv. Create a new WCF Service Web site

1, click Open IIS, create a new Web site, the site name is set to "Mywcfservice", the Physical Address select "D:\WCF", the port from the default of 80 to 81, as shown in:

2, click OK, that is, a new WCF Service Web site, we can enter "Http://localhost:81/MyWCFService.MyWCFService.svc" in the browser to verify, as shown in:

V. Create a new console client to test the WCF service

1, in the original solution to create a new project named "WcfTestClient" console program, as shown:

2, right click on the project "WcfTestClient", in the Popup context menu, select "Set as Startup Project" as shown in:

3, right click on the project "WcfTestClient" sub-node "Reference", in the Popup context menu, select "Add Service Reference" as shown in:

4, the "Add Service Reference" dialog will pop up, and after "address" Enter the new WCF Service website address (HTTP://LOCALHOST:81/MYWCFSERVICE.MYWCFSERVICE.SVC), click the "Go" button, the "service" The list of services that you just built is listed in the list, and after you select "Imywcfservice", the GetData, Getdatausingdatacontract methods are listed in the Actions list, as shown in:

5. After changing the namespace to "Mywcfreference" and then clicking the "OK" button, the "Service References" will be added to the project "WcfTestClient" child node, as shown in:

6. Add the reference to the WCF service above the program class and enter the following code in the main function:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingwcftestclient.mywcfreference;namespacewcftestclient{classProgram {Static voidMain (string[] args) {mywcfserviceclient Client=Newmywcfserviceclient (); Console.WriteLine (client. GetData (123456)); Compositetype CType=NewCompositetype () {stringvalue ="Hello world!", Boolvalue =true }; Console.WriteLine (client. Getdatausingdatacontract (CType).        StringValue); }    }}

Vi. running the client console program

Run the "wcftestclient" Client console program as shown in:

Create a simple WCF service using C #

Related Article

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.