WCF in 10 minutes : lesson 3
November 16th, 2007 by eknowledger
This article applies to .Net Framework 3.0 ,Visual Studio 2005, WCF ,Windows Vista SDK ,C# 2.0.
WCF in 10 minutes : lesson 3
Hosting your Service in web based servers
At the last 2 lessons we discussed the concept of the WCF and how to build a simple WCF Service and host it on a Console based application , today we’ll try to host the same service on Web Based Server, for quick review please check lesson 2 of the series .
Create the Server Application :
Step 1 :
Open the visual studio and choose create new web project and select the following options
- Project Template : WCF Service
- Location : File System (this will use the integrated web server with the Visual Studio 2005 )
- Language : C#
And press ok . As u can see the IDE will generates a predefined template for you consist of the following files :
-
-
- Web.Config
- Service.svc
- App_Code\Service.cs
-
These file is the main 3 files u need to run wcf service hosted in the web based servers , but we’ll modify the generated template to suite our case .

Step 2 :
We‘ll replace the generated service.cs code with our old AntiquesService Contracts and implementation which define the contracts for the IAntiquesService and provide the implementation for our contracts .
The Service Contracts :

The Service Implementaion :


Notice: when you host your application in web based servers there is no need to create the startup method to start up your hosting application instead of this you need to create the svc file which tell the hosting Web Server to startup your application .
Step 3:
Now we need to modify the generated Service.svc to run our AntiquesService , the generated Service.svc is very simple as that :
<% @ServiceHost Language=C# Debug=”true” Service=”MyService” CodeBehind=”~/App_Code/Service.cs” %>
In the service attribute place the name of your service implementation class in our case “EgyAntiques.Store.AntiquesService“ fully qualified class name , and in the CodeBehind attribute place the path to the code file for your service in our case “~/App_Code/Service.cs“. after modifying the svc file it should look like that
<% @ServiceHost Language=C# Debug=”true” Service=”EgyAntiques.Store.AntiquesService” CodeBehind=”~/App_Code/Service.cs” %>

Step 4 :
This is the final step in developing our server application , Modify the configuration file for our service .
This step should be very easy for you if you had read the past lesson we spend much time in explaining the concepts WCF service configuration files .
Open the generated web.config ,and find the tag <system.serviceModel> replace that tag with the following code

Run your WCF Web based service to make and make sure that your service metadata published to the explorer like in the following picture

The Client :
The process of creating the client is very easy and we goes in its details in lesson 2 , there is no different between creating a WCF client for console hosted service and web server hosted service , We will create a console based client to interact with our service :
Add new console project to your solution name it client

Run your service then in the client right-click references and choose “ add service reference” then in the dialog copy the service URL in my case is “http://localhost:4819/EgyAntiques/Service.svc” and put it in the service URI field then write in the service reference name “AntiquesClient” this will be the service reference name , then press ok .

If you notice the WCF Framework generates for you the “AntiquesClient.map” file which is the proxy file that referencing your WCF Service ,also generates the app.config file for you which contain the client configurations for the service , and finally adds references to the
-
-
- System.ServiceModel
- System.Runtime.Serialization
-

Finally add the following simple piece of code to your Main method in the client :
static void Main(string[] args)
{
Console.WriteLine(”Please press enter to get the list of our antiques :”);
Console.ReadLine();
AntiquesClient.AntiquesClient client = new AntiquesClient.AntiquesClient(”ep1″);
try
{
Console.Write(client.DisplayAntiquesList());
Console.WriteLine(”\n\n\nPlease select your Antique # to know it’s price :”);
Console.WriteLine(”Your Antique Price is : ” + client.GetAntiquePrice(int.Parse(Console.ReadLine())));
Console.WriteLine(”\npress enter to terminate the program … “);
Console.ReadLine();
}
finally
{ }
}
Finally run the service , then start new instance of our client application , try the client and enjoy the magic

Resources :
Hope this Helps,
Ahmed








