Developing a WCF Service Library and Hosting it with a Custom App Using VS2K8 - Build the project
(Page 4 of 5 )
Developing a custom application to host the WCF: creation and configuration
To host the previous WCF Service Library, I want my own custom application to be developed using Windows Forms. The following is the walk through:
Go to File || Add || New Project.
Select "Windows Forms Application" as template, provide "Name" as "NorthwindServiceHost" at the same solution location as shown below (Fig 04).
Design the form with two buttons (for Start and Stop service) and a label (for messaging).
Add an "app.config" file using "Add new Item" as shown below (Fig 05).
Copy the same <system.serviceModel> element from the previous "App.Config" (of the "NorthwindService" project) into the current one.
Within the project properties, open the Settings tab and add a new setting with the name "cnNorthwind,", "Type" as "String," "Scope" as "Application" and "Value" as required connection string (to connect to database) as shown in previous sections.
Add a reference to "NorthwindService" project (fig 06, fig 07).
Add a reference to "System.ServiceModel" (fig 08).

Figure 04

Figure 05

Figure 06

Figure 07

Figure 08
Developing a custom application to host the WCF: coding and testing
The following is the source code for both the "Start" and "Stop" buttons designed in previous section:
Imports System
Imports System.ServiceModel
Public Class Form1
Dim host As ServiceHost
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStart.Click
host = New ServiceHost(GetType(NorthwindService.ProductService))
host.Open()
Me.lblMsg.Text = "Started"
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnStop.Click
host.Close()
Me.lblMsg.Text = "Stopped"
End Sub
End Class
Set the "NorthwindServiceHost" as the startup project as shown below (Fig 09).

Execute the application and hit the "Start" button. It should start the service as follows (Fig 10). Always stop the service before closing the application.

Go to the "bin" folder of "NorthwindServiceHost" and execute the "NorthwindServiceHost.exe" independently (without using Visual Studio). It should respond without any difference as shown in the following image (Fig 11).

Testing the WCF custom hosted Service: creating the WCF Client using Windows Forms
To test the WCF service hosted in previous sections, we need to develop a WCF client. In this case, for simplicity, I would like to work with Windows Forms.
Add a new project (to the existing solution), select "Windows Forms Application" as the template, provide the name "NorthwindClient" and hit on OK.
If not already started, start the service independently from Windows Explorer (as shown in the previous section).
Add a service reference by right clicking on the "NorthwindClient" project and selecting "Add Service Reference" (fig 12).

In the "Address" field, type http://localhost:8181/ProductService and hit GO. Before you hit GO, make sure that the service is already started independently (outside Visual Studio) as shown in previous section.
Once the service is found, provide the namespace as "ProductService" and click OK (Fig 13).

Design a form that looks like the following (Fig 14):

Next: Testing the WCF custom hosted Service: source code >>
More ASP.NET Articles
More By Jagadish Chaterjee