DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Creating WCF Service of Temp. Conversion in Console VB

This tutorial will show you how to create a Console Application of a WCF Service that will convert Fahrenheit to Celsius (and vice versa), and how to Consume that Service in a Client Console Application. VB

In this tutorial, we are going to look at using WCF (Windows Communication Foundation) to create a Service and a Client in a Console application using VB.NET. We will first create a Console Application that will act as our WCF Service, and then we will consume that within another Console Application, which will be our Client. In order for us to use the Service from the Client, it is required that the Service be running.

This tutorial assumes you are working with Visual Studio 2008, with Windows SDK installed. You can download Windows SDK directly from Microsoft here. We will be using the CMD Shell from SDK to build our Client Application after we have created our Service.
To start, let's create a new project in Visual Studio, Console Application. The first thing we need to do is add a reference to the System.ServiceModel.dll - right-click the Project in Solution Explorer, and choose to Add Reference. Navigate to the System.ServiceModel entry in the .NET list, and click Ok. Once you've done that, add the following using directives:

Imports System.ServiceModel
Imports System.ServiceModel.Description

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Now we need to creat our Interface for the WCF Service. We do this with the ServiceContract attribute and by declaring an interface. Then we create a class that inherits from this interface, also creating our two methods:

<ServiceContract(Namespace:="http://DotNetTutorials.Service/")> _
Public Interface ITemperatureConvert
<OperationContract()> _
Function FahrenheitToCelsius(ByVal toConvert As Integer) As Integer
<OperationContract()> _
Function CelsiusToFahrenheit(ByVal toConvert As Integer) As Integer
End Interface

Friend Class TemperatureConvert
Implements ITemperatureConvert
Public Function FahrenheitToCelsius(ByVal toConvert As Integer) As Integer Implements ITemperatureConvert.FahrenheitToCelsius
Dim fToC As Integer = (toConvert - 30) / 2
Console.WriteLine("Processed: {0}°F to {1}°C.", toConvert, fToC)
Return fToC
End Function

Public Function CelsiusToFahrenheit(ByVal toConvert As Integer) As Integer Implements ITemperatureConvert.CelsiusToFahrenheit
Dim cToF As Integer = (toConvert * 2) + 30
Console.WriteLine("Processed: {0}°C to {1}°F.", toConvert, cToF)
Return cToF
End Function
End Class

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

Next, we use the default Module to initialize the Service when it is run, in the Main method. We create an EndPoint, and start the service ready for the Client to consume it.

Module Module1

Sub Main()
Dim baseAddress As New Uri("http://localhost:8000/WCF_Fahrenheit_Celsius_vb")
Dim localHost As New ServiceHost(GetType(TemperatureConvert), baseAddress)

Try
localHost.AddServiceEndpoint(GetType(ITemperatureConvert), New WSHttpBinding(), "TemperatureConvert")

Dim smb As New ServiceMetadataBehavior()
smb.HttpGetEnabled = True
localHost.Description.Behaviors.Add(smb)

localHost.Open()
Console.WriteLine("TemperatureConvert Service initialized.")
Console.WriteLine("Press the ENTER key to terminate service.")
Console.ReadLine()

localHost.Close()
Console.WriteLine("Service Terminated. Exiting..")
Catch ex As CommunicationException
Console.WriteLine("Oops! Exception: {0}", ex.Message)
localHost.Abort()
End Try
End Sub

End Module

Our next step is to create the Client. Right-click the Solution in Solution Explorer, and choose to Add > New Project, Console Application. Name it TempClient or something similar. Now before we do any coding on the client, we need to build the generatedProxy.vb and the app.config files from the Service. To do this, we will use the CMD Shell and the svcutil tool. Goto the Start Menu > All Programs > Windows SDK > CMD Shell. You should have a Command Prompt up. We need to navigate to the directory of our Client project we just added. To do this, use the Change Directory command: example, cd "C:\Users\Fred.Bloggs\Documents\Visual Studio 2008\Service\ClientApp"

Once in the Client directory, we want to execute the svcutil.exe with the following modifiers:
svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config http://localhost:8000/WCF_Fahrenheit_Celsius_vb

Doing this will generate the generatedProxy.vb and app.config files for us in our Client directory. Once successfully completed, close the CMD Shell, go back to Visual Studio and right-click on the Client Project in Solution Explorer, then choose to Add > Existing Item. Add both newly-generated files. We will also want to Add Reference to the System.ServiceModel for our Client Project, as we did with our Service. Then Imports System.ServiceModel.

Finally, we add the logic to the Module.vb of the Client:

Sub Main()
Dim client As New TemperatureConvertClient()

Dim fahrenheit As Integer
Console.WriteLine("Enter Fahrenheit to convert to Celsius:")
fahrenheit = Convert.ToInt16(Console.ReadLine())
Try
Dim toCelsius As Integer = client.FahrenheitToCelsius(fahrenheit)
Console.WriteLine("{0}°F is about {1}°C", fahrenheit, toCelsius)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

Dim celsius As Integer
Console.WriteLine("Enter Celsius to convert to Fahrenheit:")
celsius = Convert.ToInt16(Console.ReadLine())
Try
Dim toFahrenheit As Integer = client.CelsiusToFahrenheit(celsius)
Console.WriteLine("{0}°C is about {1}°F", celsius, toFahrenheit)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

Console.WriteLine()
Console.WriteLine("Press the ENTER key to terminate client.")
Console.ReadLine()

Try
client.Close()
Catch
End Try
Console.WriteLine("Client Terminated. Exiting..")
End Sub

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

First, we create a new instance of the Client class which was generated for us in the generatedProxy.vb, and then we call each method from the Service, requesting user input to convert the temperatures. We also use Try..Catch blocks to handle errors.
NOTE: The service must be running in order for the Client to communicate with it. To do this, right-click on each project in Solution explorer and choose Rebuild. This will build the .exe in the projects bin folder (either debug or release sub-folder). Then we can first execute the Service exe, then the Client exe second.

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!