Join Jess Chadwick for an in-depth discussion in this video Configuring and deploying to Windows Azure, part of ASP.NET MVC 4 Essential Training.
In the previous video, I showed you how to deploy your website to an IIS server; however, not everyone has access to an IIS server, nor do they care to manage their own hosting environment. Instead, more and more people are looking to cloud hosting services to host their sites. In this video, I will walk you through the simple steps for deploying your application to the cloud using Windows Azure. When we are finished, we will have a public website hosted in the cloud. Before you can deploy your website to the cloud using Windows Azure, you must first register for a Windows Azure account.
If you don't already have one, visit the Windows Azure website, windowsazure.com, find and click the link that says Free trial and create your new account. Once your account is created, navigate to manage.windowsazure.com to get to the Windows Azure Management Portal, the online portal for managing your cloud hosting services. To create a new website using the Windows Azure portal, click the New menu at the bottom left of the page and choose the WEB SITE option. Then click the CREATE WITH DATABASE option to open the new website wizard.
First, enter a name for your site, but remember that this name must be unique, for instance I am using the name MvcAuction, and once I do, you will not be able to use this name. Since our demo application requires a database to store its information, choose the Create new SQL database option from the DATABASE dropdown list. The next few steps will help you configure your new database. By the time you're watching this video, the steps may have changed, so just follow the wizard. When possible, try to use the defaults that the wizard recommends.
When you are done providing all the information for your new site and its database, click the check mark to create your new site. After providing Windows Azure with plenty of time to create and provision your new web application, click on the application's Name to begin managing it. Once you have created your Windows Azure website, it's time to deploy your application and Azure provides you with several ways to do this. My personal favorite way is to wire my Azure website up to my TFS or Git source control repository.
This way, every time I push code to my source control, my site automatically gets updated with the latest changes. You can set up source control publishing by clicking the Set up TFS publishing or Set up Git publishing links in the Azure portal. Unfortunately, configuring source control publishing is a bit too tedious to demonstrate in this video. So, you'll have to follow the instructions that Azure provides to get them up and running if you would like to use them. Besides, Windows Azure offers another publishing option that is quite frankly much more impressive to demonstrate.
That option is the ability to publish straight from Visual Studio. To configure Visual Studio publishing with Azure, you will first need to download what's called a publish profile. You can do this by clicking the Download publish profile link on the Azure management portal for your website. Once you've downloaded this file, switch to Visual Studio, right-click on your website, and click Publish, just as we did in the previous video when we published to IIS. But unlike publishing to IIS, we are not going to use the file system method this time, instead click on the Import button and show Visual Studio where to find the Azure publish profile that you just downloaded.
Once the profile is loaded, you can walk through the Wizard to configure plenty of deployment options for your application. Take, for instance, the database settings under the Settings tab. Visual Studio has inspected our application discovered both of the Entity Framework data context that we are using and automatically configure them for use in the cloud. There is one problem, however. Remember a few chapters ago when I said that you will want to switch to Entity Framework Code First, Code Migrations, before you released your application? Well, we are getting ready to release our application, so it's probably a good idea to do that now, not to mention the Deployment wizard is refusing to deploy our database changes without Code Migrations enabled.
Luckily, enabling Code Migrations is pretty easy. First, open the Package Manager Console by going to TOOLS > Library Package Manager > Package Manager Console. Then in your Package Manager Console, select the project that contains your data context. In our case, we have two data contexts, our Auctions data context in the Common project, and the User's data context in the main Auctions website. Because they are in two different projects, we'll have to enable each of these separately.
Let's start with the Auctions data context. First, change the Default project to Common, then type Enable-Migrations into the Console. Visual Studio will generate some code for you. This is your Code Migrations Configuration file. The details of Code Migrations are outside the scope of this course, but all you need to know right now is that Automatic Migrations are the easiest, especially early in a project. To enable Automatic Migrations, simply set AutomaticMigrationsEnabled to True.
After we are done with the Auctions data context, follow the same steps for the user context, switching first to the MvcAuction project where it lives. The Migrations tool can be a little bit picky, so if you get the error that you see here, try to recompile your project and also try to select one of the items within the MvcAuction project, then try to run your command again, and it should succeed this time.
Once Visual Studio is done generating the code for us, we'll want to enable Automatic Migrations for this context as well. When you are all ready, recompile your site to make sure that everything still works. Then open up the Publish Wizard again and navigate to the Database settings. This time we can choose the Execute Code First Migrations option to have the publishing tool generate our databases during deployment. Now, jump to the next step of the wizard where Visual Studio offers to provide us with a preview of what it's about to do.
Click the Start Preview button to have Visual Studio create a list of content and actions that it will execute in order to deploy your site to Windows Azure. Review this list, and once you're happy with what's going to happen, go ahead and click Publish, look in the Output tab and watch Visual Studio as it publishes your site to Windows Azure. When it's done, Visual Studio will open up a new browser and navigate to your new site.
And there you have it, our website has been deployed to the cloud.
- What is Microsoft ASP.NET MVC?
- Creating your first ASP.NET MVC project
- Understanding the MVC architecture
- Interacting with models and ViewData
- Applying advanced layout techniques
- Routing requests to controller actions
- Validating user input
- Authenticating registered users
- Caching pages
- Reducing bandwidth usage with bundling and minification
- Displaying mobile-optimized content
- Simplifying a complex application with areas
- Configuring and deploying to IIS or Windows Azure