WCF - A simple example

This article will show you how to create, deploy and configure a WCF service (and client) in less than 5 minutes, in 4 easy steps:

  1. Create the service
  2. Deploy to IIS
  3. Configure IIS
  4. Create the client

We will be using Visual Studio 2017.

1 Create the service
  1. File New Project
    Visual C#
    WCF
    WCF Service Application
    Name: WeatherServiceApp
    OK
  2. Modify the sample code so that it looks like the two files below:

    IWeatherService.cs (refactored from IService1.cs)

    using System.Runtime.Serialization;
    using System.ServiceModel;
     
    namespace WeatherServiceApp
    {
        [ServiceContract]
        public interface IWeatherService
        {
            [OperationContract]
            WeatherReport GetWeather(Location location);
        }
     
        [DataContract]
        public class Location
        {
            [DataMember]
            public int ZipCode { get; set; }
        }
     
        [DataContract]
        public class WeatherReport
        {
            [DataMember]
            public int ZipCode { get; set; }
            [DataMember]
            public string Text { get; set; }
        }
    }

    WeatherService.cs (refactored from Service1.cs)

    using System;
     
    namespace WeatherServiceApp
    {
        public class WeatherService : IWeatherService
        {
            public WeatherReport GetWeather(Location location)
            {
                if (location == null)
                    throw new ArgumentNullException("location");
     
                var weather = new WeatherReport
                {
                    ZipCode = location.ZipCode,
                    Text = "It will be dry and sunny today."
                };
     
                return weather;
            }
        }
    }
2 Deploy to IIS
  1. Build Publish WeatherServiceApp
    Folder: c:\inetpub\wwwroot\wcf\
    Publish
3 Configure IIS
  1. Open IIS Manager, right click on the directory to which you deployed your service, and select Convert to Application. Accept the defaults, and click Okay.

4 Create the client
  1. File New Project
    Visual C#
    Windows Classic Desktop
    Console App (.NET Framework)
    Name: WeatherClient
    OK
  2. Project Add Service Reference
    Address:
    http://localhost/wcf/WeatherService.svc Go
    OK

    Program.cs

    using System;
    using WeatherClient.ServiceReference1;
     
    namespace WeatherClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                var client = new WeatherServiceClient();
                var location = new Location()
                {
                    ZipCode = 12508
                };
                var weather = client.GetWeather(location);
                Console.WriteLine(weather.Text);
            }
        }
    }

Program output:

It will be dry and sunny today.

Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath