Simple WCF Service Host

app.config

The configuration for a simple http WCF host is:

<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings></appSettings><system.serviceModel><services><service name="AssemblyNamespace.ServiceImplementation"behaviorConfiguration="ServiceBehavior"><endpoint address="ServiceName"binding="basicHttpBinding"contract="AssemblyNamespace.ServiceContractInterface" /><endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange" /><host><baseAddresses><add baseAddress="http://HostName:Port/"/></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior name="ServiceBehavior"><serviceMetadata httpGetEnabled="true"/></behavior></serviceBehaviors></behaviors></system.serviceModel></configuration>

An example:

<?xml version="1.0" encoding="utf-8" ?><configuration><appSettings></appSettings><system.serviceModel><services><service name="TestService.CalculatorService"behaviorConfiguration="CalculatorServiceBehavior"><endpoint address="CalculatorService"binding="basicHttpBinding"contract="TestService.ICalculator" /><endpoint address="mex"binding="mexHttpBinding"contract="IMetadataExchange" /><host><baseAddresses><add baseAddress="http://localhost:8000/"/></baseAddresses></host></service></services><behaviors><serviceBehaviors><behavior name="CalculatorServiceBehavior"><serviceMetadata httpGetEnabled="true"/></behavior></serviceBehaviors></behaviors></system.serviceModel></configuration>

The Service

Define the service in an interface

using System.ServiceModel;namespace TestService{[ServiceContract]public interface ICalculator{#region Instance Methods[OperationContract]double Add(double n1, double n2);[OperationContract]double Divide(double n1, double n2);[OperationContract]double Multiply(double n1, double n2);[OperationContract]double Subtract(double n1, double n2);#endregion}}

Then implement the interface

using System;namespace TestService{public class CalculatorService : ICalculator{#region ICalculator Memberspublic double Add(double n1, double n2) {double result = n1 + n2;Console.WriteLine("Received Add({0},{1})", n1, n2);Console.WriteLine("Return: {0}", result);return result;}public double Divide(double n1, double n2) {double result = n1/n2;Console.WriteLine("Received Divide({0},{1})", n1, n2);Console.WriteLine("Return: {0}", result);return result;}public double Multiply(double n1, double n2) {double result = n1*n2;Console.WriteLine("Received Multiply({0},{1})", n1, n2);Console.WriteLine("Return: {0}", result);return result;}public double Subtract(double n1, double n2) {double result = n1 - n2;Console.WriteLine("Received Subtract({0},{1})", n1, n2);Console.WriteLine("Return: {0}", result);return result;}#endregion}}

The Host

Lastly create a host for the service

using System;using System.ServiceModel;namespace TestService{internal class Program{#region Class Methodsprivate static void Main() {using (ServiceHost serviceHost = new ServiceHost(typeof (CalculatorService))) {serviceHost.Open();Console.WriteLine("The calculator service is ready.");Console.WriteLine("Press <ENTER> to terminate service.");Console.WriteLine();Console.ReadLine();}}#endregion}}
Previous
Previous

Azure Development Fabric and Visual Studio 2010

Next
Next

Installing Subversion on Windows