Developing Business Logic using the WCF Service Library with VS2K8 and ASP.NET 3.5 - Understanding the WCF Service host
(Page 4 of 5 )
Every WCF Service must be hosted. There are several ways of hosting (depending on the requirements) a WCF Service. In this article we focused on hosting a WCF Service using a custom application.
The custom application (or a simple service host utility) manages the service. The main two operations for any service to manage are "start" and "stop." A separate Windows forms application (NorthwindBLLServiceHost) is developed specifically for this purpose.
The application simply contains a form with two buttons (for "start" and "stop") and finally a label to show the status of the service. The following is the source code for both "Start" and "Stop" buttons:
Imports System
Imports System.ServiceModel
Public Class Form1
Dim hostEmpService As ServiceHost
Private Sub btnStartNorthwindEmpService_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStartNorthwindEmpService.Click
hostEmpService = New ServiceHost(GetType
(NorthwindBLLService.EmpService))
hostEmpService.Open()
Me.lblEmpServiceStatus.Text = "Service Started Successfully" End Sub
Private Sub btnStopNorthwindEmpService_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btnStopNorthwindEmpService.Click
hostEmpService.Close()
Me.lblEmpServiceStatus.Text = "Stopped" End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
If hostEmpService.State = CommunicationState.Opened Then
hostEmpService.Close()
End If
Catch ex As Exception
End Try
End Sub
End Class
Once the application is executed and you have hit the "Start" button, it should start the service as follows:

Always start the service before working with any application which tries to access the service.
Next: Understanding the WCF Client: ASP.NET 3.5 web site >>
More ASP.NET Articles
More By Jagadish Chaterjee