Developing a WCF Service Library and Hosting it as WCF Web Service Using VS2K8 - Creating a Windows Communication Foundation (WCF) Service Library
(Page 2 of 5 )
The following are the steps necessary to create a WCF Service Library:
- Open Microsoft Visual Studio 2008 Beta 2.
- Go to File || New Project.
- In the "New Project" dialog box, open "Visual Basic" project types and select WCF. The respective templates for WCF will be shown on the right side.
- Select the "WCF Service Library" template.
- Provide "WCFSampleService" as the name and provide the proper path as the location.
- Make sure that ".NET Framework 3.5" is selected at the top.
- Once everything looks like the following (fig 01), click OK.
- Rename "IService1.vb" to "IEmpService.vb."
- Rename "Service1.vb" to "EmpService.vb."
- Add a reference to "System.Configuration" as shown below (fig 02).

Figure 01

Figure 02
Creating a Windows Communication Foundation (WCF) Service Library: source code
Once the project is created (as shown above), modify "IEmpService.vb" to match the following:
<ServiceContract()> _
Public Interface IEmpService
<OperationContract()> _
Function GetNames() As List(Of String)
<OperationContract()> _
Function GetEmployeeInfo(ByVal EmpNo As Integer) As Employee
End Interface
<DataContract()> _
Public Class Employee
Private _Empno As Integer
Private _Ename As String
Private _Sal As Double
Private _Deptno As Integer
<DataMember()> _
Public Property Empno() As Integer
Get
Return _Empno
End Get
Set(ByVal value As Integer)
_Empno = value
End Set
End Property
<DataMember()> _
Public Property Ename() As String
Get
Return _Ename
End Get
Set(ByVal value As String)
_Ename = value
End Set
End Property
<DataMember()> _
Public Property Sal() As Double
Get
Return _Sal
End Get
Set(ByVal value As Double)
_Sal = value
End Set
End Property
<DataMember()> _
Public Property Deptno() As Integer
Get
Return _Deptno
End Get
Set(ByVal value As Integer)
_Deptno = value
End Set
End Property
End Class
Modify "EmpService.vb" to match the following:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration
Public Class EmpService
Implements IEmpService
Public Function GetEmployeeInfo(ByVal EmpNo As Integer) As Employee Implements IEmpService.GetEmployeeInfo
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("cnNorthwind").ConnectionString)
Using cmd As New SqlCommand("SELECT empno,ename,sal,deptno FROM dbo.emp WHERE empno=" & EmpNo, cn)
cmd.Connection.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader
If rdr.Read() Then
Return New Employee With {.Empno = rdr(rdr.GetOrdinal("Empno")), .Ename = rdr(rdr.GetOrdinal("Ename")) & "", .Sal = rdr(rdr.GetOrdinal("sal")) & "", .Deptno = rdr(rdr.GetOrdinal("Deptno")) & ""}
Else
Return Nothing
End If
End Using
cmd.Connection.Close()
End Using
End Using
End Function
Public Function GetNames() As System.Collections.Generic.List(Of String) Implements IEmpService.GetNames
Dim EmployeeNames As New List(Of String)
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("cnNorthwind").ConnectionString)
Using cmd As New SqlCommand("SELECT ename FROM dbo.emp", cn)
cmd.Connection.Open()
Using rdr As SqlDataReader = cmd.ExecuteReader
If rdr.HasRows Then
While rdr.Read
EmployeeNames.Add(rdr(rdr.GetOrdinal("ename")))
End While
Return EmployeeNames
Else
Return Nothing
End If
End Using
cmd.Connection.Close()
End Using
End Using
End Function
End Class
Next: Creating a Windows Communication Foundation (WCF) Service Library: application configuration >>
More ASP.NET Articles
More By Jagadish Chaterjee