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 two

In my previous post we saw how to utilize Microsoft Visual Studio 2008 Express Editions to create and consume Windows Communication Foundation services: Visual Web Developer 2008 for the first task and Visual Basic 2008 for the second one, observing how VWD does all the "dirty" work to publish the service to Internet Information Services.

 

In this post we want to use exclusively Visual Basic 2008 Express, reconstructing from zero the project. After starting VB 2008 Express, the first step to cover is creating a Class Library project that we'll call WcfExpressTemplate.

Once ready, to the project must be added two assembly references: System.ServiceModel.dll (the WCF assembly) and System.Runtime.Serialization.dll. Once we've done this, we have to write the contract, the interface that characterizes the service. We can also reuse the code file we created last time with VWD or add a new file called IExpressService.vb whose code is the following:

 

<ServiceContract()> _

Public Interface IExpressService

 

    <OperationContract()> _

    Function ValidateMailAddress(ByVal mailAddress As String) As Boolean

 

End Interface

 

The class that implements the contract is defined inside a file called ExpressService.svc.vb (we can reuse the previous from VWD) whose code is always this:

 

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

To complete this file, we need the .svc file that identifies the services via markup code. To add a .svc file to the project we call the Project|Add new item command but we need to use the text file template, because VB Express doesn't offer a .svc file template. As we said before, the file will be called ExpressService.svc and will contain the following markup:

 

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

The last step needed to create our service is adding a configuration file. For the same reasons we discussed about the .svc file, we need to add it using the Text file template. So let's add a Web.config file. Here we have a simplified code, because we're not in a Web application but in a class library, so some sections are not necessary:

 

<?xml version="1.0" encoding="utf-8" ?>

 

<configuration>

 

  <system.serviceModel>

    <services>

      <service

          name="WcfServiceTemplate.ExpressService"

          behaviorConfiguration="MyServiceBehavior">

        <endpoint address=""

                  binding="wsHttpBinding"

                  contract="WcfServiceTemplate.IExpressService" />

      </service>

    </services>

 

    <behaviors>

      <serviceBehaviors>

        <behavior name="MyServiceBehavior">

          <serviceMetadata httpGetEnabled="true" />

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

 

Substantially we specified just the ServiceModel section, that is the one related to the WCF service. Select Build|Build... to compile the project and verify that no exceptions are thrown. Our project is so cool that we want to transform it into a reusable project template for WCF each time we start Visual Basic 2008 Express, so we decide to export the project to a template. From the File menu, select the command Export template. The following two pictures show the two Wizard steps needed to export the project, underlining the template type (project template) and the various identifier to assign.

 

 

 

 

Don't you believe it's troue? J Try to create a new project and you'll see the new template among the available ones:

 

 

Now we have to face the most complex step, publishing the service to Internet Information Services. We need a temporary folder, in my pc I created one called C:\Temporary\ExpressSvc. In the mentioned ExpressSvc folder we must copy the Web.config and ExpressService.svc. Inside the folder we need then to create another subfolder called Bin, where we'll copy the WcfExpressTemplate.dll, which is effectively the WCF service.

 

Once done this, let's start Internet Information Services Manager from Windows' Administrative tools. Expand the tree structure until you see the default web site, right click it and select the Add Application command as shown here: 

 

 

 

In the next window, let's specify the path where we manually deployed the service by assigning an Alias as shown here:

 

 

 

Now we can verify that the service has been correctly hosted typing the service URL in the addresses bar. Our browser will appear as follows:

 

 

 

This means that everything is working fine! Now we can reference our service from whatever .NET application. The following picture shows the Add Service Reference dialog, underlining how the service's metadata are reached and shown correctly:

 

 

 

And that’s all! J Perhaps a long series of steps, but I hope that it can be useful to clarify any doubts about creating and consuming WCF services with Visual Studio 2008 Express Edition.

 

Alessandro

Print | posted on venerdì 2 gennaio 2009 20:43 | 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 2 and 6 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET