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:
We will be using Visual Studio 2017.
IWeatherService
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
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;
}
}
}
See also:
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.
See also:
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.