Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

Creating and consuming WCF services with VS 2008 Express Editions - part one

A question that I've often read inside the Visual Basic Tips & Tricks community forums is: "how do I create and consume WCF services if I have only Visual Studio 2008 Express Editions?". So in this post and in the next one we'll see right how to accomplish this. In this first post we'll see how to use Microsoft Visual Web Developer 2008 Express Edition to create a WCF service and Visual Basic 2008 Express Edition to consume the service, while in the next post we'll see how to use just Visual Basic 2008 to reconstruct the WCF service, publish to IIS and, consume it and export our code to a reusable project template.

 

Therefore, the first thing to do is starting Microsoft Visual Web Developer 2008 Express Edition (administrative privileges are required for subsquently publishing to IIS). Our purpose is creating a project about a WCF Service Application (so, not a Web site), using the template and assigning the project name as shown in the following picture:

 

 

 

After a few seconds, VWD 2008 is ready and the new project appears as follows:

 

 

Notice how, in Solution Explorer, there is the interface IService1.vb (the Contract), the configuration file and the service file (Service1.svc). As you probably know, in Visual Studio 2008 each .svc file has a code-behind file, that effectively implements the contract and that will be called Service1.svc.vb. When you double click the .svc file, by default Visual Studio opens the code-behind file and not the markup one. Ok, we're ready to create a quite useful example, even if quite simple. Our service will expose a method capable to validate e-mail addresses, via regular expressions. So, Solution Explorer rename the IService1.vb file in IExpressService.vb. Completely remove the default VB code and type the following:

 

<ServiceContract()> _

Public Interface IExpressService

 

    <OperationContract()> _

    Function ValidateMailAddress(ByVal mailAddress As String) As Boolean

 

End Interface

 

Now rename Service1.vb in ExpressService.vb and remove all the code added by default by Visual Web Developer. The class will implement the contract as follows:

 

Public Class ExpressService

    Implements IExpressService

 

    Public Function ValidateMailAddress(ByVal mailAddress As String) As Boolean Implements IExpressService.ValidateMailAddress

        Dim validateMail As String = "^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-z]{2,4}|[0-9]{1,3})(\]?)$"

 

        Return Regex.IsMatch(mailAddress, validateMail)

    End Function

End Class

 

The ValidateMailAddress method will verify that the email address received as an argument is a valid mail address, returning True or False according to the check result. Now we must update the references to interface and class inside Web.config. First, localize the System.ServiceModel section, then modify the service attributes as follows, specifying our interface and class identifiers:

 

            <services>

                  <service name="WcfExpressService.ExpressService" behaviorConfiguration="WcfExpressService.Service1Behavior">

                        <!-- Service Endpoints -->

                        <endpoint address="" binding="wsHttpBinding" contract="WcfExpressService.IExpressService">

The last step consists in updating the markup in the service file ExpressService.svc. In Solution Explorer, right click this file and from the popup menu select View Markup. The code must be updated as follows:

 

<%@ ServiceHost Language="VB" Debug="true" Service="WcfExpressService.ExpressService" CodeBehind="ExpressService.svc.vb" %>

Now we're ready to compile the code and build the service. From the Build menu, select the Build command (is followed by the project name) accepting modifies to Web.config related to debugging.

When ready, the service must be hosted by an host application. A typical scenario is hosting services in Internet Information Services and Visual Web Developer 2008 gets the hands dirty for us (this didn't happen in VS 2005). From the Build menu let's click Publish. You'll see a window where you can specify the publish path, that can be IIS, a local folder or an FTP server. You can also click the Browse ("...") button to select the desired path or writing by yourself. Let's type the path for publishing as shown in the following picture, accepting the default settings:

 

 

Once VWD has finished publishing without errors, let's verify that our service has been hosted correctly. Let's start our Web browser and type the service URL. If everything works fine, the browser should appear as follows:

 

 

 

At this point the service is ready to be consumed. Let's start Microsoft Visual Basic 2008 Express Edition and create a new Console application as an example. We need to add a service reference, by utilizing the Project|Add Service Reference command. Now, when the Add Service Reference dialog appears, we just need to type the service URL and click Go, as shown here:

 

 

As you can see, Visual Basic will list service's metadata and this means that the service has been reached correctly. Change the namespace identifier as shown in the above picture, then click OK.

 

Now we have anything we need to consume the service. For example, the following (simple) code waits for the user to enter an email address, verifies that it's a valid address and then shows a message:

 

Imports ConsoleApplication1.ExpressServiceReference

 

Module Module1

 

    Sub Main()

 

        'Client instance

        Dim client As New ExpressServiceClient

 

        'An e-mail address must be entered

        Console.WriteLine("Type the mail address to validate:")

        Dim mailAddress As String = Console.ReadLine

 

        'Address validation via our WCF Service

        If client.ValidateMailAddress(mailAddress) = True Then

            Console.WriteLine("You entered a valid mail address")

        Else

            Console.WriteLine("The mail address you entered is invalid.")

        End If

 

        Console.ReadLine()

    End Sub

 

End Module

We can normally instantiate the service and call the method as from a typical class. If we try to run the application, we'll obtain a similar result:

 

 

 

As you can see, creating and consuming WCF Services with Microsoft Visual Studio 2008 Express Editions is a really simple task. In the next post we'll see how to reproduce the work with the only Visual Basic 2008.

 

Alessandro

Print | posted on venerdì 2 gennaio 2009 20:18 | Filed Under [ Visual Basic Windows Communication Foundation Visual Studio Express Editions ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 1 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET