Create an interface decorated with a ServiceContract attribute and member functions decorated with the OperationContract attribute.
namespace Service
{
[ServiceContract]
interface IExample
{
[OperationContract]
string Echo(string s);
}
}
Create a concrete class implementing this interface and you have the service.
namespace Service
{
public class Example : IExample
{
public string Echo(string s)
{
return s;
}
}
}
Then create the host where the service will run from. This can be any type of application. Console, service, GUI application or web server.
namespace Console
{
using Service;
class Console
{
Servicehost mHost;
public Console()
{
mHost = new ServiceHost(typeof(Example));
}
public void Open()
{
mHost.Open();
}
public void Close()
{
mHost.Close();
}
public static void Main(string[] args)
{
Console host = new Console();
host.Open();
Console.Readline();
host.Close();
}
}
}
The ServiceHost class will read the configuration file to initialize the service.
<system.serviceModel>
<services>
<service name="Service.Example">
<endpoint name="netTcpExample" contract="Service.IExample" binding="netTcpBinding" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:9000/Example" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>