Sunday, April 1, 2012

Hosting ASP.NET Web Api on Windows Azure Platform

In this post I am going to show you how to Host your ASP.NET Web Api Services on Windows Azure, to demonstrate this I am going to use my application which I have created in my previous couple of Post :

CRUD operation using ASP.NET Web Api and MVC 4 – Part 1

CRUD operation using ASP.NET Web Api and MVC 4 – Part 2

Before hosting my application I have to perform the following steps.

1. Add an Cloud Application from Add New project

image

2. Select ASP.NET MVC 4 Web Role, since I have used ASP.NET MVC 4 to create my Web Api project.

image

3. From the following Screen I have to select cancel, since I am adding this to my existing Web Api Project, and don’t want to create a new Applications.

image

4. Once the project creation is successful, right click on the Roles folder from the WebRole.Azure project and Select Add, from the add menu select Web Role Project in solution  and Select the WebApi Project listed in the Popup windows. This will add my Web Api project in the Roles folder of Azure project as given in the screen below.

image

image

5. Now our Azure project is configured to publish my WebApi service on Web, but before we publish we need to configure the Database connection which is currently pointing to my local system, we need to change this to the SQL Azure database. Since SQL Azure setup and configuration is out of scope of this Post, so I am not going to cover that here, but don’t worry I have given that instruction in my another post, which will help you to configure the Entity Framework EDMX with SQL Azure.

6. Now once your application is ready with the SQL Azure connection, we will publish our Application on Windows Azure. But before we continue, we may have to configure publish settings in my system, sometime back I have written couple of articles where you can find how to publish your website on Windows Azure. But simplest way to do the same I have given below. Right click on your Windows Azure project and Select Publish, You will be prompted with the screen below. Now Select the option “Sign in to download credential”

image

7. This link will take you to the windows azure site, where you will have to provide your credentials and then you can download the publishsettings files, using the Save as option. Save this file to your system.

image

8. Now select the import button and locate the downloaded publishsettings file, this will auto populate the Subscription dropdown as given below, I have used my 3 months free subscription to demonstrate this. You too can subscribe to 3 months free trial from here.

image

9. Provide your Name and Location details.

image

10. Select Ok then Next, you will land up in the below screen, where you can Say next.

image

11. Finally you will provided with the Publish Summary settings, where you can review your settings and it everything looks good then you can click on publish.

image

12. You can see the progress here, or if you want you can see the same in your Windows Azure console also.

image

Windows Azure console.

image

13. Once ready you will be come to the following screen, where you can see the status as Ready.

image

And that’s it we are ready to test our application. You can get the DNS name (URI) of your Web Api Service from the console once your applications are ready. Lets try out Api Services, in the browser using Developer ToolBar. Following are the list of Methods and URI are exposed to web.

image

Lets try with the Get (/api/values) and check in Dev Toolbar, So I am getting the Responsed code 200 and in the detailed view –> Response Body I can see my result returned from my Api Service.

image

image

Now lets try from from the Client screen where I have written few lines of jQuery to demonstrate the CRUD operation, details of which you can refer in my previous post.

CRUD operation using ASP.NET Web Api and MVC 4 – Part 1

CRUD operation using ASP.NET Web Api and MVC 4 – Part 2

GetById (/api/values/1) and GetAll (/api/values)

image

image

The codes for these sample can be downloaded from here https://docs.google.com/open?id=0BzIjFd_Ps-MSUm4zMklvYkxUMXlFZzdOWVBsaHJvQQ

This is the same code which I have already provided in my previous post : CRUD operation using ASP.NET Web Api and MVC 4 – Part 2, You will just have to do the configurations which I have mentioned in this post. Hope this helps.

Generate Entity Framework using SQL Azure Database

In this example I am going to show you how to generate your Entity Model edmx using SQl Azure database.

Before we start, first lets create a Sample database named called ContactDetail on SQl Azure database.

To create a SQL Azure database you can either Login directly to your SQL Azure account through web or you can Login to your Windows Azure Account, and from the Homepage you can navigate to the Database. In my example below I am using the Second option.

1. Login to your Windows Azure Account and Navigate to the Database, this will give you the below screen.

image_thumb

2. Now Select the Create Button to Create a new database, this will give you the following screen where you can enter your region and provide a new credentials which you will use with this new database.

image_thumb[1][1]

image_thumb[2][1]

3. Once done, Select Next this will provide you with the below screen where you can provide the firewall rule, this is just for extra security. Once you have provided the IP Address Range, you will not be able to connect from any other IP Address which does not fall in this Range.

image_thumb28[1]

4. If you are connecting through any other application which is Hosted on any other Windows Azure accounts then make sure to Select this option, this will allow your Windows Azure applications to connect to this SQL Azure Database.

image_thumb[3][1]

5. Once completed you can see your fully qualified server name in the Dashboard provided. This Server name is used to connect remotely to your Azure DB from your application or from VS 2010 Server Explorers, etc.

image_thumb26

6. Now lets create some sample database which we are going to use to generate our Entity Model, to create a new database you will have to first select the newly created server from the left hand side of the Windows Azure console, and select Create from the Menu, this will present you the windows where you have to provide your Database information. Based on your requirement you can select the Edition and Size, I am keeping my Database names as ContactDetail and Leaving rest as default and Select Ok.

image

7. Now I can see my New database in the Azure Console, select manage to manage the database objects like SP, Tables, etc. This will open the SQL Azure console.

image

8. SQL Azure console will prompt you for credentials, once you are thru, you will get the SQL Azure Dashboard, where once you have to select the Contact Detail Table –> Design – New Table

image

9. In the New Table I have provided the following information, for my Contact table and Click on Save, this will create my ContactDetail Table.

image

Now I am ready with the SQL Azure table, my next task will be to configure my Entity Framework to use this table, to use in my Application. Please note if you want to use any scripts which is used for your SQL Server database, then this will not work with your SQL Azure database, you can read more in the following link: http://blog.sqlauthority.com/2010/06/04/sql-server-generate-database-script-for-sql-azure/

10. Now lets get back to my application, where I am going to create a entity framework entity model, which I am going to configure with this SQL Azure DB, to perform this I am opening my .NET application where I want to add Entity Model, and Select Add New Item – Data –> ADO.NET Entity Data Model.

image

11. Select Next, in the Wizard, Select New Connection and enter the fully qualified Server name, and the credentials and Select Ok and then Next with default options selected.

image

12. You will get the list of your tables, views and Stored procedures, select the desired Database objects with the default options selected and Select Finish, you will get the Designer with the tables, stored procedures or views you have selected.

image

And that’s it, once done you can work like any other Entity Framework you have used to work with your SQL Server 2008/2005 databases.