4 min read

(For more resources on Microsoft, see here.)

Creating your first WCF application in Visual Studio 2008

You start creating a WCF project by creating a new project from File | New | Project…. This opens the New Project window. You can see that there are four different templates available. We will be using the WCF Service Library template.

Easy guide to understand WCF in Visual Studio

Change the default name and provide a name for the project (herein JayWcf01) and click OK. The project JayWcf01 gets created with the folder structure shown in the next image:

Easy guide to understand WCF in Visual Studio

If you were to expand References node in the above you would notice that System.ServiceModel is already referenced. If it is not, for some reason, you can bring it in by using the Add Reference… window which is displayed when you right click the project in the Solution Explorer.

IService1.vb is a service interface file as shown in the next listing. This defines the service contract and the operations expected of the service.

If you change the interface name “IService1” here, you must also update the reference to “IService1” in App.config.

<ServiceContract()> _
Public Interface IService1

<OperationContract()> _
Function GetData(ByVal value As Integer) As String

<OperationContract()> _
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType

' TODO: Add your service operations here

End Interface

' Use a data contract as illustrated in the sample below to add composite types to service operations
<DataContract()> _
Public Class CompositeType

Private boolValueField As Boolean
Private stringValueField As String

<DataMember()> _
Public Property BoolValue() As Boolean
Get
Return Me.boolValueField
End Get
Set(ByVal value As Boolean)
Me.boolValueField = value
End Set
End Property

<DataMember()> _
Public Property StringValue() As String
Get
Return Me.stringValueField
End Get
Set(ByVal value As String)
Me.stringValueField = value
End Set
End Property

End Class

The Service Contract is a contract that will be agreed to between the Client and the Server. Both the Client and the Server should be working with the same service contract. The one shown above is in the server. Inside the service, data is handled as simple (e.g. GetData) or complex types (e.g. GetDataUsingDataContract). However outside the Service these are handled as XML Schema Definitions. WCF Data contracts provides a mapping between the data defined in the code and the XML Schema defined by W3C organization, the standards organization.

The service performed when the terms of the contract are properly adhered to is in the listing of Service1.vb file shown here.

' NOTE: If you change the class name "Service1" here, you must also update the
reference to "Service1" in App.config.
Public Class Service1
Implements IService1

Public Function GetData(ByVal value As Integer) As _
String Implements IService1.GetData
Return String.Format("You entered: {0}", value)
End Function

Public Function GetDataUsingDataContract(ByVal composite As CompositeType) _
As CompositeType Implements IService1.GetDataUsingDataContract
If composite.BoolValue Then
composite.StringValue = (composite.StringValue & "Suffix")
End If
Return composite
End Function

End Class

Service1 is defining two methods of the service by way of Functions. The GetData accepts a number and returns a string. For example, if the Client enters a value 50, the Server response will be “You entered: 50”. The function GetDataUsingDataContract returns a Boolean and a String with ‘Suffix’ appended for an input which consists of a Boolean and a string.

The JayWcf01 is a completed program with a default example contract IService1 and a defined service, Service1. This program is complete in itself. It is a good practice to provide your own names for the objects. Notwithstanding the default names are accepted in this demo.

In what follows we test this program as is and then slightly modify the contract and test it again. The testing in the next section will invoke an in-built client and then later on we will publish it to the localhost which is an IIS 7 web server.

How to test this program

The program has a valid pair of contract and service and we should be able to test this service. The Windows Communication Foundation allows Visual Studio 2008 (also Visual Studio 2010 Express) to launch a host to test the service with a client.

Build the program and after it succeeds hit F5. The WcfSvcHost is spawned which stays in the taskbar as shown.

You can click WcfSvcHost to display the WCF Service Host window popping-up as shown. The host gets started as shown here. The service is hosted on the developmental server.

Easy guide to understand WCF in Visual Studio

This is immediately followed by the WCF Test Client user interface popping-up as shown. In this harness you can test the service.

Easy guide to understand WCF in Visual Studio

LEAVE A REPLY

Please enter your comment!
Please enter your name here