Use C # To create a WCF Service Console Application,

Source: Internet
Author: User

Use C # To create a WCF Service Console Application,

This article is original. Please indicate the source for reprinting. Thank you!

 

I. Development Environment

Operating System: Windows 10

Development Environment: VS2015

Programming Language: C #

IIS Version: 10.0.0.0

2. Add the WCF Service and Internet Information Services (IIS)

1. Go to "Control Panel", open "programs and functions", click "enable or disable Windows functions" in the upper left corner, and go to ". select "WCF Service" for the subnode in NET Framework 4.6 advanced service, as shown in:

2. Find "Internet Information Services" and select the node, as shown in:

3. Click "OK" to install these services and components.

3. Create a WCF Service Library

1. Use VS2015 to create a WCF Service library and change the project name to "MyWCFService", as shown in:

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

VS2015 will create the "IService1" interface and the "Service1" class respectively. The "IService1" interface includes the GetData and GetDataUsingDataContract methods, and implements the interface using the "Service1" class. The Code is as follows:

"IService1" interface:

// Note: You can use the "RENAME" command on the "refactoring" menu to change the Interface Name "IMyWCFService" in the Code and configuration file at the same time ". [ServiceContract] public interface IService1 {[OperationContract] string GetData (int value); [OperationContract] CompositeType GetDataUsingDataContract (CompositeType composite); // TODO: add your service operation here} // use the data conventions described in the following example to add a composite type to the service operation. [DataContract] public class CompositeType {bool boolValue = true; string stringValue = "Hello"; [DataMember] public bool BoolValue {get {return boolValue;} set {boolValue = value ;}} [DataMember] public string StringValue {get {return stringValue;} set {stringValue = value ;}}}

"Service1" class:

// Note: You can use the "RENAME" command on the "refactoring" menu to change the class name "Service1" in the Code and configuration file at the same time ". Public class Service1: IService1 {public string GetData (int value) {return string. format ("You entered: {0}", value);} public CompositeType GetDataUsingDataContract (CompositeType composite) {if (composite = null) {throw new ArgumentNullException ("composite");} if (composite. boolValue) {composite. stringValue + = "Suffix";} return composite ;}}

3. move the cursor over the project "MyWCFService" in Solution Explorer and right-click it. The context menu is displayed. After "publish" is selected in the menu, the "Publish a WCF Service" dialog box is displayed, as shown in:

Select "D: \ WCF" in the target location, and press default. Click "publish" to generate the file as shown in the "D: \ WCF" folder:

4. Create a WCF Service Website

1. Click Open IIS, create a website, set the website name to "MyWCFService", select "D: \ WCF" as the physical address, and change the port number from 80 to 81 by default, as shown in:

2. Click "OK" to create a new WCF Service website. You can enter "http: // localhost: 81/MyWCFService. MyWCFService. svc" in the browser for verification, as shown in:

5. Create a console client to test the WCF Service

1. Create a console program named "WCFTestClient" in the original solution, as shown in:

2. Right-click the project "WCFTestClient" and select "set as startup project" from the context menu, as shown in:

3. Right-click the project "WCFTestClient" subnode "Reference" and select "add service reference" from the context menu, as shown in:

4. The "add service reference" dialog box is displayed. In "Address", enter the newly created WCF Service website address (http: // localhost: 81/MyWCFService. myWCFService. svc), click the "go to" button, and the created service will be listed in the "service" list. After "IMyWCFService" is selected, the GetData and GetDataUsingDataContract methods are listed in the "operation" list, as shown in:

5. Change the namespace to "MyWCFReference" and click "OK". "Service References" will be added to the "WCFTestClient" subnode of the project, as shown in:

6. Add a reference to the WCF Service above the Program class, and enter the following code in the Main function:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using WCFTestClient.MyWCFReference;namespace WCFTestClient{    class Program    {        static void Main(string[] args)        {            MyWCFServiceClient client = new MyWCFServiceClient();            Console.WriteLine(client.GetData(123456));            CompositeType cType = new CompositeType() { StringValue = "Hello World!", BoolValue = true };            Console.WriteLine(client.GetDataUsingDataContract(cType).StringValue);        }    }}

6. Run the console program of the Client

Run the "WCFTestClient" console program, as shown in:

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.