Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 35736

How to migrate an existing ASP.NET Website Project to Windows Azure

$
0
0

In this blog we will discuss how to migrate an existing ASP.NET Website project to Windows Azure.

Before we begin you will need the following prerequisites:

  • Windows 7
  • Visual Studio 2010 SP1
  • Download and install the Windows Azure SDK for .NET
  • SQL Server Express 2008 R2 (This is installed with Visual Studio)

We will perform the tasks below:

  1. Convert the ASP.NET Website project to an ASP.NET Web Application project
  2. Add Windows Azure Deployment Project to the ASP.NET Web Application
  3. Deploy to Windows Azure
  4. Migrate any SQL Server databases to SQL Azure
  5. Configure the Web Application to use SQL Azure database

Task1: Convert the ASP.NET Website project to an ASP.NET Web Application project

To convert an existing ASP.NET Website to an ASP.NET Web Application create a new ASP.NET Web Application. To create a new ASP.NET Web Application start Visual Studion 2010 in Administrator mode. You can do so by going to the Start menu -> right-click Microsoft Visual Studio 2010 -> click Run as Administrator. It is not required to start Visual Studio in Administrator mode, but we will need to be in this mode when publishing our application to Windows Azure.

Once you have Visual Studio 2010 open in Administrator mode from the File menu click New Project as shown below.


Once you see the New Project dialog box, expand the node for a programming language of your choice (in my case it is C#), select Web, and then select ASP.NET Web Application. Provide a name for your Web Application in the Name textbox and click on OK as shown in the figure below. I chose to name my application "MigrateASPWebsiteAzure"

The next step is to copy all the files from your ASP.NET Website to the newly created ASP.NET Web Application. In the process override any files created by default within your ASP.NET Web Application.

To copy files to the new Web Application right-click the root project folder in Solution Explorer -> click Add -> click Existing Item as shown in the figure below.

For simplicity I only have one Default.aspx file in my old ASP.NET Website. I will add it to my new ASP.NET Web Application.

Just a note when you click on Add -> Existing Item, Visual Studio will open the Add Existing Item dialog box and specify the path for the current ASP.NET Web Application project directory. You should change the path for the websites directory where you have stored your ASP.NET Website files. In my case the directory for my old ASP.NET Website is My Documents -> Visual Studio 2010 -> Websites -> LabA. This is shown in the figure below.

 I select the Default.aspx file and click on Add as shown in the figure below.

 

Once I click Add I am prompted to replace the Default.aspx file in my current ASP.NET Web Application. Select Apply to all items check box and then click Yes as shown below.

Also click on Yes to All button when prompted next as shown below to realod the Default.aspx file.

If you have any assembly references in your old ASP.NET Website add them again to your current  ASP.NET Web Application. Also, do not store class files in the App_Code folder. If you store them in the App_Code folder the classes will be compiled two times, the first time the class files will be complied as part of the Visual Studio Web application project assembly, and the second time will be at run time by ASP.NET. You should store class files in any folder other than the App_Code folder.

The next step is to convert pages and classes to use partial classes in a ASP.NET Web Application project.  We now right-click the root project folder in Solution Explorer -> click Convert to Web Application. When you see the message "This action will add designer and code behind files for the selected items, which is required for converting Web site project files to Web application project files. Do you want to continue?" click Yes

Now, build the project to see if there are any compilation errors. For me I got errors indicating missing connection string for the SQL Server connection. I reconfigured my SqlDataSource to add the SQL connection string. I then checked to make sure the connection string was added in the <connectionStrings> section of the Web.config file.

I then rebuild and test my ASP.NET Website Application by pressing F5 and confirm it works correctly as shown below.

Thus we have sucessfully converted the ASP.NET Website project to an ASP.NET Web Application project. The next task is to add Windows Azure Deployment Project to the ASP.NET Web Application.

 

Task 2: Add Windows Azure Deployment Project to the ASP.NET Web Application

To add a Windows Azure Deployment project to the current ASP.NET Web Application right-click the root project folder in Solution Explorer -> click Add Windows Azure Deployment Project as shown in the figure below.


Visual Studio 2010 creates Windows Azure Deployment Project. For me project MigrateASPWebsiteAzure.Azure was created as shown in the figure below.

Now we run the solution using the Windows Azure compute emulator, we press CTRL-F5 to run the solution.

The homepage appears as shown below. Note that the URL is different than the one shown earlier after we migrated the site to ASP.NET Web Application.

To deploy our project to Windows Azure we right-click MigrateASPWebsiteAzure.Azure project in Solution Explorer and click on Publish as shown below.

 Next, when the Publish Windows Azure Application wizard appears. If you do not have a subscription  click the Sign in to download credentials link as shown below.

 

Use your Live ID credentials to login the Windows Azure Platform sign-in page. After logging in you will be directed to a page which starts downloading the .publishsettings file as shown in the figure below. Save the file in a location of your choice. We will need this file in the next step.

Now we go back to Visual Studio. In the the Publish Windows Azure Application wizard click Import, then select the subscription file we downloaded and click next as shown in the figure below.

Next we the Windows Azure Publish Settings. If you do not have any hosted services defined for your Windows Azure account, Visual Studio will automatically prompt you to create one. 

If you have hosted services defined and wish to create a new hosted service or if the Create Windows Azure Services dialog box does not appear from the Hosted Service drop down list select Create New as shown below.


 

Enter a unique name for the hosted service in the Create Windows Azure Services dialog box. The reason the name must be unique is because it will be part of the URL that you'll use to access the service over the Internet. In my case I chose MigrateASPWebsiteAzure. Select a region. I selected the South Central as I am located in this region.  Click OK when you are done. See the figure below.

 Now in the Management Portal for Windows Azure check to see if the hosted service was created. In the navigation pane on the left click Hosted Services, Storage Accounts & CDN then click Hosted Services as shown in the figure below.

 

Now we switch to Visual Studio again. In the Windows Azure Publish Settings dialog box from the Environment drop down lost select either Production or Staging. In my case I will deploy to Production. Then we click Next as shown below.

 

 In the Windows Azure Publish Summary dialog box click the save icon as shown to save profile. The profile name will be the Hosted Service name concatenated with the Environment name. Finally click on the Publish button.

The Windows Azure Activity Log shows the deployment progress of the application in Windows Azure. See figure below.

After the deployment is completed we go back to the Management Portal on Windows Azure and check the hosted service MigrateASPWebsiteAzure. Under the MigrateASPWebsiteAzure hosted service we can see the deployment in the Production environment as shown below.

Before we access the application, we must migrate our SQL Server databse to Windows Azure and configure the web application to use the database from SQL Azure.

Task 3: Migrate SQL Server databases to SQL Azure

To migrate SQL Server database used by the ASP.NET Web Application to SQL Azure we use SQL Server Inteegration Services. This is demonstrated on my blog Migrating an on-premise database from SQL Server to SQL Azure using SSIS.

After the SQL Server database has been migrated to SQL Azure we configure the Web Application to use SQL Azure database as shown next.

Task 4: Configure the Web Application to use SQL Azure database

Before we begin the configuration of the Web Application to use SQL Azure database we need to install ASP.NET Universal Providers. To install ASP.NET Universal Providers under Solution Explorer right click -> MigrateASPWebsiteAzure project -> click Add Library Package Reference as shown below.

 

When the Add Library Package Reference dialog box appears, enter Microsoft.AspNet.Providers in the search box and press enter. From the results select the Microsoft.AspNet.Providers package and click Install as shown below.

 

 

Now we are ready to configure the Web Application to use the SQL Azure database, we need the connection string for the SQL Azure database we just migrated.

To get a copy of the connection string click on Database in the navigation pane on the left in the Management Portal.Then select the node for the Subscriptions and expand the node for the database server which contains the migrated database. We now select the database we just migrated. With the database we migrated selected on the right hand pane select the ellipses button from the Connection Strings box. See figure below.


 

Then from the Connection Strings dialog box, copy the ADO.NET connection string as shown below.  

 

 

Now we go back to Visual Studio and under Solution Explorer expand the Web.config files and select the Web.Release.config file. See the image below.

In the Web.Release.config file we remove the comments from the sample connection string. We replace the connection string with the one we copied from the Management Portal. Also change the connection string name, in my case it is SyncDBConnectionString. See the image below.

 

 

We need to change the "myPassword" in the connection string with the actual password.

To deploy our project to Windows Azure we right-click MigrateASPWebsiteAzure.Azure project in Solution Explorer and click on Publish as shown earlier.

When the deployment is finished we return to the Management Portal and display the MigrateASPWebsiteAzure hosted service as shown below.We select the MigrateASPWebsiteAzure deployment in Production environment. Then click the link in the DNS name box to launch our Web Application. See the image below.

 

 

 Using the link from the DNS name box we launch the application from Windows Azure as shown below.

 

Hence we have successfully migrated an ASP.NET Website project to Windows Azure.

References: http://www.asp.net/web-forms/tutorials/deployment/deployment-to-windows-azure/hosting-an-aspnet-web-forms-application-on-windows-azure


Viewing all articles
Browse latest Browse all 35736

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>