Creating a WCF Service Console Application using C

Creating a WCF Service Console Application using C#

In this article, we will guide you through the process of creating a WCF service console application using C#.

First, the Development Environment

  • Operating System: Windows 10
  • Development Environment: VS2015
  • Programming Language: C#
  • IIS Version: 10.0.0.0

Adding WCF Services and Internet Information Services (IIS)

To begin, we need to enable WCF services and IIS in our development environment.

  1. Enable WCF Services: Open the “Control Panel” and navigate to “Programs and Features”. Click on “Enable or Disable Windows Features” in the upper left corner and select “WCF Service” under “.NET Framework 4.6 Advanced Services”.
  2. Enable IIS: Find “Internet Information Services” again and select the node.
  3. Click the “OK” button to install these services and components, and wait for the installation to complete.

Third, Create a New WCF Service Library

Now that we have enabled WCF services and IIS, we can create a new WCF service library.

  1. Create a New Project: Use VS2015 to create a new WCF service library and change the project name to “MyWCFService”.
  2. Rename the Interface and Class: Rename the “IService1” interface and “Service1” class to “IMyWCFService” and “MyWCFService” in the Solution Explorer.

The “IService1” interface contains the following code:

// Note: Use the "Rename" command on the "Refactor" menu to change the interface name "IMyWCFService" in both the code and the configuration file.
[ServiceContract]
public interface IMyWCFService
{
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

// Add the composite type to the service operation using the data convention illustrated in the example below.
[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; }
    }
}

The “MyWCFService” class contains the following code:

// Note: Use the "Rename" command on the "Refactor" menu to change the class name "MyWCFService" in both the code and the configuration files.
public class MyWCFService : IMyWCFService
{
    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;
    }
}

Publishing the WCF Service

To publish the WCF service, move your mouse over the Solution Explorer project on “MyWCFService” and right-click to bring up the context menu. Select “Publish” in the menu, and a “Publish WCF Service” dialog box will appear. Select the target location as “D:\WCF” and click the “Publish” button.

Fourth, Create a New WCF Service Website

Now that we have published the WCF service, we can create a new WCF service website.

  1. Create a New Website: Click to open IIS and create a new website. Set the website name to “MyWCFService” and select the physical address “D:\WCF”. Change the port from the default 80 to 81.
  2. Verify the Website: Click OK to create the new WCF service website. We can verify this by entering “http://localhost:81/MyWCFService.MyWCFService.svc” in our browser.

Fifth, Create a New Console Client to Test WCF Services

To test the WCF service, we need to create a new console client.

  1. Create a New Project: Create a new original solution in a project called “WCFTestClient” console application.
  2. Set as StartUp Project: Right-click on the project “WCFTestClient” and select “Set as StartUp Project” in the pop-up context menu.
  3. Add Service Reference: Right-click on the project “WCFTestClient” child node “References” in the pop-up context menu and select “Add Service Reference”. Enter the WCF service website address (http://localhost:81/MyWCFService.MyWCFService.svc) and click the “Go” button.
  4. Select the Service: Select “IMyWCFService” in the “Services” list and click the “OK” button.
  5. Add the Service Reference: Add the WCF service reference in a class above the Program and enter the following code in the Main 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);
        }
    }
}

Six, Run the Client Console Program

Run the “WCFTestClient” client console program, and we should see the following output:

You entered: 123456
Hello World!Suffix

This article was created in collaboration with the Tencent Cloud Media Self-Sharing Program.