This blog demonstrates how to create a single WCF service that is self-hosting, that is it’s hosted not inside neither IIS, WAS nor exposed as a Windows service. This solution includes three projects to clearly demonstrate the separation of responsibilities and concern of the service, the hosting environment, and lastly how the client is setup to call the service.
It’s important to note that if you’re running Windows Vista, you must run Visual Studio 2008 under Administrator privileges by right-clicking its shortcut and select “Run as Administrator” else you will have to register an HTTP address on your machine to listen for requests. Non-administrator accounts must grant permission for HTTP namespaces where an Administrator account already will have this permission granted.
The project solution is available here.
Creating a Service and its Contract
Create a brand new solution in Visual Studio 2008 and name the solution “SampleService” for example. Select your project type as a Class Library and name it “Service”. Note that I’m not using the WCF Service Library project for demonstration purposes here only otherwise it could be used to create both your service and contract.
Right click on the Service project and go to Properties. Change the Default namespace to something of your liking. In my case I’m setting it to “Blog.Joubin”.
Add a new “WCF Service” item to this project by right-clicking on the Service project and selecting Add –> New Item. Name this item “CalculatorService.cs”. Note that this will automatically add the reference to the System.ServicesModel.dll to your project. This action will also generate three files:
- ICalculatorService.cs: This file is the service interface that is decorated as a ServiceContract and one OperationContract. The method declared as OperationContract will be replaced with other methods.
- CalculatorService.cs: Inherits and implements ICalculatorService.cs. There are no attributes in this file declared.
- App.config: contains the ABCs (address, binding and contract) configuration for your first WCF service.
Open the ICalculatorService.cs file. Modify the ServiceContract declartion from [ServiceContract] to [ServiceContract(Namespace=”http://blog.joubin.samples”)].
Open the created App.config file. Search for the baseAddress attribute of the baseAddresses element as shown below:
<services>
<service behaviorConfiguration="Blog.Joubin.CalculatorServiceBehavior"
name="Blog.Joubin.CalculatorService">
<endpoint address="" binding="wsHttpBinding" contract="Blog.Joubin.ICalculatorService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/Blog.Joubin/CalculatorService/" />
</baseAddresses>
</host>
</service>
</services>
Replace <add baseAddress="http://localhost:8731/Design_Time_Addresses/Blog.Joubin/CalculatorService/" /> with <add baseAddress="http://localhost:8080/CalculatorService" />
Replace the single OpertionContract method from ICalculatorService interface and replace with the following. In this step you are defining the service contract.
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);Open the CalculatorService.cs file and remove the DoWork method and replace it with the following. In this step you are implementing the service contract operations.
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Received Add({0},{1})", n1, n2);
// Code added to write output to the console window.
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;
}
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 Divide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Received Divide({0},{1})", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
At this point build your solution and make sure it builds successfully.
Creating the hosting environment for your WCF service
Add a Windows Console Application to your solution. Name it “Host”.
Add a reference to the “System.ServiceModel.dll” assembly to your project.
Open the Program.cs file and add import the System.ServiceModel and System.ServiceModel.Description and Blog.Joubin namespaces as follows:
using System.ServiceModel;
using System.ServiceModel.Description;
using Blog.Joubin;Add a reference to the “Service” project.
Right-click on the “Host” project and select “Set as Startup Project”. Hit F5 and you should get a screen like the following.
Since in this blog a separate host is used to host the WCF service, the App.config in the “Service” project needs to be moved to the “Host” project.
Move the App.config file by dragging and dropping it into the Host project. This will only copy it. Then you can delete it from the Service project if you want to.
Creating the service client
Add another Console application to your existing project and name it “Client”. Add a reference to assembly “System.ServiceModel.dll” to this project.
Open the Program.cs file in this project and add the namespace “System.ServiceModel”
using System.ServiceModel;In this step I’m going to create the proxy class and client configuration file for the CalculatorService. Build your solution and run your project such that the command line screen with text “The calculator service is now ready.” is displayed.
Go to a “Visual Studio 2008 Command Prompt” and navigate to or create a temporary folder under which you will create the intended files that will later be imported into Visual Studio 2008. Run the following command:
svcutil /language:cs /out:CalculatorServiceProxy.cs /config:App.config http://localhost:8080/CalculatorService
The above command will generate two files. The first file is the proxy C# class. Only when this class file is included in your Client project you can program against it. The second file is the client configuration file necessary to connect to your WCF service in the Service project.
Go back to your Visual Studio .net 2008 solution and add the above generated files to the Client project.
Open the Program.cs file and replace the Main method with the following:
static void Main(string[] args)
{
ICalculatorService calculatorService = new CalculatorServiceClient();
double value1 = 100.00D;
double value2 = 15.99D;
double result = calculatorService.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = calculatorService.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = calculatorService.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = calculatorService.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
Console.ReadLine();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
Running your solution
In order to run both the service/host and the client to access the service, you will have to run both the Host and the Client project in this order.
Go to the Properties of your solution and then to the “Startup Project” tab as show below. Select the “Multiple Startup projects”. Order your project as follows and press OK.
| Project name | Order | Action |
| Host | 1 | Start |
| Client | 2 | Start |
| Service | 3 | None |
Save your solution and Build all. Press F5. You should see two command line windows:
References
- Download the full solution
- Note that this blog is based on the “Getting Started” article on MSDN.
