In the previous post, I created a simple database that lives in the cloud, using the Microsoft SQL Azure platform. Now, because I want to have a Silverlight client using it, I will develop a couple of services that will be hosted in Windows Azure, to provide both read and write access to my data.

I will create both a traditional WCF service and a REST service using ADO.NET Data Services. Both are solid approaches to solving our problem with different characteristics. WCF services are what is typically refered to as Fixed Contract services, in the sense they provide a structured set of “black boxes” that receive a specific input and return an expected result. REST services are open-ended ways of interacting with data, awarding more flexibility in a less formatted way.

So, after installing the Windows Azure SDK, I open up Visual Studio and create a new type of project that is made available to me:

After that, I’m asked to create a project inside my solution, which relates to a Role inside Windows Azure:

In my solution explorer window I can see that Visual Studio is preparing the solution to be deployed in Windows Azure:

The first thing I need to do is find a way to gain access to the database from my service. I thought it would be really nice if I could do it through Entity Framework so I can speed up my development time. Turns out, the experience is exactly the same as if you were connecting to a SQL Server instance inside your organization. First, I start by adding a new ADO.NET Entity Data Model:

I specify that I want to generate a model from an existing database:

I create the connection to my database as follows:

I then ask to import all the tables in my database and create a model for them:

And the end result is as expected:

So, now I’m ready to use some Entity Framework magic and LINQ to quickly build a service that interacts with my database. In the interface class that describes my service (ITechResources.cs), I define these five methods:

[OperationContract]

List<Technology> GetTechnologies();

 

[OperationContract]

Technology GetTechnologyByName(string techName);

 

[OperationContract]

Technology GetTechnologyById(int id);

 

[OperationContract]

List<Resource> GetResourcesForTechnologyName(string techName);

 

[OperationContract]

List<Resource> GetResourcesForTechnologyId(int id);


I then go into the actual service implementation (TechResources.svc.cs) and implement this interface:

public class TechResources : ITechResources

{

 

    public List<Technology> GetTechnologies()

    {

        throw new NotImplementedException();

    }

 

    public Technology GetTechnologyByName(string techName)

    {

        throw new NotImplementedException();

    }

 

    public Technology GetTechnologyById(int id)

    {

        throw new NotImplementedException();

    }

 

    public List<Resource> GetResourcesForTechnologyName(string techName)

    {

        throw new NotImplementedException();

    }

 

    public List<Resource> GetResourcesForTechnologyId(int id)

    {

        throw new NotImplementedException();

    }

}


Let’s start by the first method GetTechnologies: using Entity Framework, the code is very simple:

public List<Technology> GetTechnologies()

{

    using (TechFAQEntities tech = new TechFAQEntities())

    {

        List<Technology> technologies = tech.Technologies.ToList();

        return technologies;

    }

}


I want to try if this is working, so I just press F5 in Visual Studio and it opens up the Windows Azure Simulation Environment for me, “deploys” the application and lets me test it AND debug it:

And the browser shows my service up and running:

Because this is a WCF service, I can’t really test it like this, I need some client to consume it. Before I get around to that, I’ll just build the other 4 methods in my service:

public Technology GetTechnologyByName(string techName)

{

    using (TechFAQEntities tech = new TechFAQEntities())

    {

        Technology technology = tech.Technologies.First(t => t.name == techName);

        return technology;

    }

}

 

public Technology GetTechnologyById(int id)

{

    using (TechFAQEntities tech = new TechFAQEntities())

    {

        Technology technology = tech.Technologies.First(t => t.id == id);

        return technology;

    }

}

 

public List<Resource> GetResourcesForTechnologyName(string techName)

{

    using (TechFAQEntities tech = new TechFAQEntities())

    {

        var resources = from technology in tech.Technologies

                        from resource in technology.Resources

                        where technology.name == techName

                        select resource;

       

        return resources.ToList();

    }

}

 

public List<Resource> GetResourcesForTechnologyId(int id)

{

    using (TechFAQEntities tech = new TechFAQEntities())

    {

        var resources = from technology in tech.Technologies

                        from resource in technology.Resources

                        where technology.id == id

                        select resource;

 

        return resources.ToList();

    }

}


Yes, it’s really that simple when you’re using the Entity Framework. It’s now time to create a client so I can get test the service locally before I deploy it to my Windows Azure account.

I created a console application inside the same solution, added a service reference, and used the Discovery button to find my local service:

In the Program.cs file, I added the following code:

static void Main(string[] args)

{

    TechResourcesClient client = new TechResourcesClient();

 

    Technology[] techList = client.GetTechnologies();

    client.Close();

   

    Console.WriteLine();

    Console.WriteLine("List of Technologies:");

 

    foreach (Technology t in techList)

    {

        Console.WriteLine("Name: " + t.name + " Url: " + t.url);

    }

}


After building this app, I launched it to check if everything is working:

Let’s review what is happening here: we are using a console application that calls a WCF service hosted on my machine, which uses Entity Framework to access a database that is hosted in Microsoft SQL Azure. Considering the very small amount of work I had to do, this is pretty impressive.

So, all that is left is deploying the service to my Windows Azure account. Let’s see how that goes:

When I click Publish, I’m taken to my Windows Azure account page:

Ok, so I didn’t configure anything in Windows Azure yet, so first step is creating a New Service:

Ok, I want to create a Hosted Service, so I just click on that:

I specify a name for my service and click Next:

I’m now asked to provide a public name for my service and choose a Region, which I did and it’s really as simple as that. Just click on Create and I’m ready to go:

It now asks me to upload the needed files for the service to work. When I clicked publish, Visual Studio generated the necessary files for me:

Click on Deploy, Windows Azure magic kicks in, and all we can do is wait for a bit:

After the package is deployed you should ask to Run the service, but it will take a while until the service is properly initialized. So go grab yourself a cup of coffee, relax and come back later J

In the end, you should see something like this:

However, clicking on the provided link for my service, returns a 404 error. Hmmm… pocking around on my files, I found that the Service Definition file is making the service available in port 8080 instead of the default http port 80. I will correct that later, but for now I tried accessing the service using the following Url:

http://techresourcesdemo.cloudapp.net:8080/techresources.svc

And yes, it does work. Time to update my console application so I can test against the deployed service, so I changed the endpoint address in my app.config file and re-ran the application.

Yep, it still works, so my application is officially live!

The whole process took me maybe 2 hours from initial project creation to “go-live” stage. This is the power of the cloud at its best. If I was a company that wanted to provide this service to the internet, I would have to go through the following steps:

·        Acquiring and installing a database server

·        Acquiring and installing an IIS server

·        Deploy my database and my WCF service to the servers

·        Expose my IIS server on the internet which would require me to get a static IP address, possibly a domain name and configure a DMZ to host my server.

This process would take some time and a lot of people involved. The option of having the cloud available with just a few clicks of the mouse, really empowers developers to create applications and get a really fast time to market, which is impossible to achieve in a traditional scenario.

This post is already too long, so I’ll take a little break here. In the next post I’ll configure a REST service as well and then I’ll start building my Silverlight client application.

Until then, have fun!

By the way, I already corrected the Http port, so now the service is available at http://techresourcesdemo.cloudapp.net/techresources.svc