Technology, training, learning and development blogs
Hosting a WCF service
If you launch your service from within Visual Studio 2008 it is hosted by WcfSvcHost. However, when you deploy your service you will want something else to do the hosting. For example, you could use a Windows application, a Windows service or IIS. For simplicity, I am going to host my service using a Console application.
Create the self hosting project
In Visual Studio I add a new Console Application called FlightServiceHosted to my solution (the FlightService solution from my earlier posting).

I then copy the following files from the FlightService project into the FlightServiceHosted project.

As I shall be using a self hosting service, I no longer need the FlightService project. And so I remove it from the solution.

As the files (App.config, IService1.cs and Service1.cs) contain references to a namespace called FlightService that no longer exists, I rename all instances of FlightService in these files to FlightServiceHosted.
Example:

I ensure that the FlightServiceHosted project contains a reference to System.ServiceModel.

In the Main method in Program.cs, I declare a variable of type ServiceHost and instantiate it with the type of my service (FlightServiceHosted.FlightService).

I complete the Main method to call both the Open and Close methods of my ServiceHost. To permit an administrator/user to start and stop the service I added WriteLine and ReadLine prompts. Note: in a production environment you would be most unlikely to use a Console application. Rather, you might use IIS or a Windows service to host your WCF service.

The service is just about ready to go. However, I want to change the endpoint that was auto generated in my previous post when the FlightService was first created.
And so, I update the endpoints in both the App.config file in the FlightServiceHosted project and the App.config file of the WinClient project as follows:
FlightServiceHosted: App.config

WinClient: App.config

Time to test
That’s it. My self-hosted service and it’s client are good to go.
However, I shan’t run them from within Visual Studio as I don’t want the WCFScvHost to launch and I also want to illustrate their independence from Visual Studio.
Firstly, I use Windows Explorer to locate and execute FlightServiceHosted.exe in the FlightServiceHosted/bin/debug folder.

Once the console application launches, I press Enter to start the service.

Now that my self-hosted service is now running, it is time to run the client application.
I use Windows Explorer to locate and execute WinClient.exe in the WinClient/bin/debug folder.

Once the client application launches, I enter two airports and invoke the service which returns the appropriate flight details.

That’s it. How simple was that ?
As long as FlightServiceHosted.exe is executing and the service is running, the WinClient will be able to call it’s methods.
When I’m done, all I need to do is close the client application and then press Enter once again within the console application in order to terminate the service.
What else ?
In my next post, I will show you how easy it is for you (or better still and administrator) to change a service’s bindings.
Training ?
Training ?
Should you require training in WCF, we (Xpertise Training Limited) currently offer the following course: Complete Windows Communication Foundation (Premium Series)

