Steps to Create a WCF Service
- Define a Service Contract
- Optionally define a Data Contract
- Implement the Service Contract
- Create endpoints for the service
- Run the service
Let us go to step 1
Define a Service Contract
A WCF Service Contract can be created in many ways. Lets us use the most common approach, using a CLR interface annotated with relevant attributes.
Snippet 1:
using System.ServiceModel;
namespace Sendhil.Samples.HelloWorld
{
[ServiceContract()]
public interface IHello
{
[OperationContract()]
string SayHello(string name);
}
}
Notice the ServiceContract and OperationContract attributes annotating the interface definition. ServiceContract is targeting the interface and OperationContract is targeting the method declaration. Other than that this a plain CLR interface.
Contrast this with the all public non-static methods are exposed stuff in (.NET Remoting). You need to define the boundary explicitly in Windows Communication Foundation.
Step 1 is complete; since there are no Data Contracts to define we'll directly move to Step 3.
Implement the Service Contract, there is no complication here, Let us just implement the interface
Snippet 2:
namespace Sendhil.Samples.HelloWorld
{
public class HelloComponent : IHello
{
public string SayHello(string name)
{
return "Hello " + name;
}
}
}
Snippet 3:
using System.Reflection;
[assembly: AssemblyVersion("1.0.0.0 ")]
Compile this into a Class Library (say Sendhil.Samples.HelloWorld.dll).
Create a Folder called HelloWorld with three sub folder Server, Host and Client.
Save Snippet 1 as HelloWorldContract.cs, Snippet 2 as HelloWorldComponent.cs, Snippet 3 as AssemblyInfo.cs in the Server sub folder
From the Windows Managed Build Environment Command Prompt run the following command to compile the sources into a class library.
csc /t:library /out:Sendhil.Samples.HelloWord.dll /r:System.ServiceModel.dll HelloWorldContract.cs HelloWorldComponent.cs AssemblyInfo.cs
Step 3 is complete, Let us move the Step 4.
Create endpoints for the service
Defining an endpoint can be done in code or configuration, let us do it the configuration way. An endpoint has the following key attributes
Or ABC in short
Address is a WS-Addressing compliant address on which the service is exposed. (Where)
Binding defines how do I talk to the service (Do I use HTTP, HTTPS, TCP, MSMQ Transport), and also the QoS parameters. (How)
Contract is what the service exposes (What)
More on these as we tread along.
We are going to use the TCP transport and host it on the local machine.
Snippet 4:
<?xml version="1.0" encoding ="utf-8" ?>
<configuration xmlns=" http://schemas.microsoft.com/.NetConfiguration/v2.0 " >
<appSettings>
<add key="baseUri1" value=" http://localhost:8080/HelloWorld" /> <!-- For MEX Support -->
<add key="baseUri2" value="net.tcp://localhost:9090/HelloWorld" />
</appSettings>
<system.serviceModel>
<services>
<service type="Sendhil.Samples.HelloWorld.HelloComponent , Sendhil.Samples.HelloWorld">
<endpoint address="" binding="netTcpBinding" contract=" Sendhil.Samples.HelloWorld.IHello , Sendhil.Samples.HelloWorld" bindingConfiguration="HelloWorldServiceTcpBinding" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding configurationName="HelloWorldServiceTcpBinding" />
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
As you can see most of the configuration goes into the system.serviceModel element. The services element is a container element for service elements. The service element defines the service type. It also contains the endpoint element with is abc attributes. The address used is relative in this case. The bindingConfiguration refers to the netTcpBinding element defined later in the config. All of these can be pretty overwhelming we will see more on addressing and bindings as we move along.
We need to host this stuff somewhere, we can host in a Windows App, Console App or a Windows Service. We can also host in IIS if the transport is Http or Https.
We will use a Console Application.
Snippet 5:
using System.Configuration;
using System.ServiceModel;
using System;
namespace Sendhil.Samples.HelloWorld
{
public class HelloWorldHost
{
public static void Main()
{
Uri baseUri1 = new Uri(ConfigurationManager.AppSettings ["baseUri1"]);
Uri baseUri2 = new Uri(ConfigurationManager.AppSettings ["baseUri2"]);
using(ServiceHost host = new ServiceHost(typeof(HelloComponent), baseUri1, baseUri2))
{
host.Open();
Console.WriteLine("Hit Enter to exit");
Console.WriteLine("");
Console.ReadLine();
host.Close();
}
}
}
}
As you can see the code is pretty much simple. The key things to note are the creation of ServiceHost using the base addresses and component type. Such a service is called a self hosted service. The following code sets up a listener on the specified port and sets up a whole lot of WCF Plumbing.
Compile this into a Console App (say Sendhil.Samples.HelloWorld.Host.exe).
Save Snippet 4 as Sendhil.Samples.HelloWorld.Host.exe.config and Snippet 5 as HelloWorldHost.cs into the Host folder.
Compile using the following command from the Windows Managed Build Environment Command Prompt.
csc /t:exe /out:Sendhil.Samples.HelloWorld.Host.exe /r:System.ServiceModel.dll,"..\Server\Sendhil.Samples.HelloWorld.dll" HelloWorldHost.cs
Run the Service.
Applies to: WinFx September 2005 CTP.