Join Michael Sullivan for an in-depth discussion in this video Deploying to AppHarbor, part of ASP.NET MVC 5 Essential Training.
- Another option for hosting your applications in the cloud is AppHarbor. In this video, we'll talk about how to get set up with an account and deploy an application in a matter of minutes. We'll continue with our ATM project where we left off at the end of the last video. AppHarbor is similar to other platform as a service providers like Heroku or Google's App Engine. It has a much simpler UI than Azure, and for many developers it can feel a lot less bloated and overwhelming. You can do a lot with it for free, and even use a small amount of database storage until you need to scale up.
Signing up is simple, and once you've set up an account, you can start by entering a name for your application. You can deploy from a variety of repository hosting services, like Bitbucket, CodePlex or GitHub, which, by the way, you can do with Azure as well. For our deployment, we are just going to use Git from our local machine. But before we do that, we'll want to make sure we're set up to use SQL server, and that's actually an add-on that we'll have to install for this app.
So let's click on Add-on Catalog here. Scroll to the bottom of this page, and look for Shared Microsoft SQL server. And then click the icon or the See More link. So, under the Yacto plan, I can get 20 megabytes of storage, you won't go too far with that but it's perfect just to have something to play with. So let's go ahead an install that. Once that's done, click on it, and then click Go To SQL server up here.
At the bottom of this page, we see the connection string, and it says, "This is the connection string "that will be injected by AppHarbor." So, we won't need to change anything in our web dot config to switch from SQL server Local DB to this database that's provided by AppHarbor. However, we do need to tell it where we want it to be injected, and we can do that by setting the connection string alias to the name of our connection string in our web dot config, which is Default Connection.
So click Edit Database Configuration, and set the alias. Entity Framework metadata doesn't apply for us because we're doing Code First Development, but I would want to set that if I had been using an Entity data model with Database First Development. Multiple Active Results Sets refers to executing multiple batches of statements with a single connection. Feel free to read up on that, but it won't affect what we're doing right now. One thing you won't get from AppHarbor is an offer to execute Code First migrations.
But we can manually recreate what Azure was doing behind the scenes for us when it did so. In our application DbContext class, I'm gonna want to override a method called OnModelCreating. You can find the code for this method in the exercise files in the Chapter Eight Start Folder in this text file called OnModelCreating. So I'm gonna copy this, and paste into our application DbContext class. So this sets up a particular type of Database Initializer.
Specifically, the Migrate Database to Latest Version type. There are a few other types you can use that will automatically drop and recreate the database or only do so when the model changes, or only create the database if it doesn't already exist. But Migrate To Latest Version is a good choice that you can use for your first deployment as well as subsequent deployments after your model changes. The default implementation of this method does some setup work for ASP dot NET Identity models, so we need to make sure we call Base dot OnModelCreating as well.
This configuration class reference refers to the configuration class in the migrations folder, so you'll want to bring in that namespace if this is invalid for you. It's also possible to set this Initializer in your Application Start method, but like Application Start, this method here will only be called once when the context is first referenced, and then it will cache the entire model for this context. A third way, which is the way it's done behind the scenes during an Azure deployment, is to configure it in web dot config, under the Entity Framework Context element.
You can see an example of this also in the Chapter Eight start folder in Azure web dot config. At the bottom, inside the Entity Framework element, we have the context element, and here we see a Database Initializer, which references the Migrate Database To Latest Version type, but I find it to be a bit more pleasant to do this with code. And now we're ready to deploy, which we're gonna do with Git. If you don't have Git installed already, go ahead and grab it from Git dash SCM dot com.
I'm not gonna say too much about Git now. You can find plenty of training resources on Git on lynda dot com and elsewhere. So we're just gonna do the simplest possible Git deployment to get set up on AppHarbor. So Git comes with command prompt support, GUI tools, and also Git Bash, which is a lightweight UNIX Bash shell, which I like to use for committing and pushing code. And since we're about to push code to AppHarbor, let's just make sure that these changes are saved.
Okay, and now we can navigate to our solution directory. By the way, make sure there's only one solution in this directory, otherwise AppHarbor won't know what we're trying to deploy. Next, we'll type git init to initialize a new Git repository, and then git add dot to add all files to it. We won't get into a discussion of line endings now, but let me just say that, in a real world scenario, you'll want to set it up to ignore some of the Visual Studio files and generated folders, but for now, I just want to demonstrate the actual deployment.
Next, we'll perform out first commit, using git commit dash m, for message, and we'll just give it init for the initial commit message. One last thing we have to do before deployment, is link this repository to our remote AppHarbor repository. And I can get that URL from my application page, by clicking the Repository URL button, which copies it to my clipboard. To be able paste into Git Bash, or the Windows command prompt, you'll need to Right click the title bar up here, select Properties, and then make sure Quick Edit mode is selected.
And then we'll use git remote add appharbor, which will be the repository name, and then Right click to paste the URL. And finally, we can do git push appharbor master, to push our local Master branch to the remote Master branch of the AppHarbor repository. And here I'm just entering the password I used when I signed up for AppHarbor. And once that's done, if we go to our MVC ATM application page on AppHarbor and refresh, we can see that it's actually in the process of doing a build.
And when it's done building, it's also going to run our unit tests, so when the build is complete, we see this unhappy looking icon, and the reason is because one of our tests has actually failed. And it tells us which one. It's the Contact Form Says Thanks test, and if we click that, it also shows us the error, which I think is some really nice visibility, although we might not be able to tell exactly what's going on without looking at the code. So let's do that. So in our unit test class, the problem is that we changed this Contact Form method to return a partial view result when we were experimenting with AJAX and partial page updates.
So let's just change that to a partial view result here. We'll save, and go back to Git Bash, to add these changes, commit with a message of fixed unit test, and then push to appharbor again. Now, back on the status page, when this is finished building, we see the succeeded icon this time.
And if we refresh, we see a link that says "Go To Your Application". So let's click this to check it out. And if I can log in with my admin account, then I know that the database was updated properly, and my seed method was executed. So with AppHarbor, we've already seen a second way to quickly get your application deployed to a scalable cloud platform.
- Exploring ASP.NET routing
- Applying action selectors
- Working with layouts
- Employing HTML helpers
- Displaying and validating model properties
- Generating database objects with Entity Framework
- Adding transactions
- Authenticating users
- Unit testing
- Performing partial page updates with Ajax and jQuery
- Deploying ASP.NET MVC applications