Deploying your ASP.NET App on AppHarbor

As web developers, we often want our applications to be available on the Internet, out from the confines of our dev machines. Traditionally the only way to do it is to get some hosting space and setup you app on your hosting provider. This often involves setting up a build/deploy process or manual code deployments and hacks to get things working.

Enter AppHarbor!

Their tag line is “Where .NET Apps grow and prosper”. Before you dismiss them as yet another hosting provider, what would it be like to have a provider that syncs code from your code repository, does the build and deployment and gives you a full report of the failures? Sounds great right? AppHarbor is a complete ASP.NET platform as a service. Best of all, you can get started for free for their basic plan. Wondering what it takes to get started? Well, we will walk through the process in this article.

Registering at AppHarbor

Registration is as simple as it gets, pick a name, and provide a valid email address and a password. They will send out a validation email, click on the link and you are good to get started.

Creating your First Application

Again, it’s a single step process provide a name, click on “Create New” and you are good to go. AppHarbor will try to use this name for the URL of your app so choose carefully. However there is no guarantee you will get the name. If not available, AppHarbor will append a number at the end.

create-new-app-appharbor

Linking to Source Repository

Now that your app is ready, time to link it up to your favorite source control repository. In this post, we will use a GitHub repository. You can easily bind to a BitBucket repository too. Steps are nearly identical.

Getting the AppHarbor API Key

In your application console, there is a “Build URL” button. Click on it to copy the URL to your clipboard (requires Flash plugin to be installed).

copy-build-url

Now paste the URL on NotePad or Stickies. The authorization parameter of the URL is your API Key.

https://appharbor.com:443/applications/funwithsignalr-1/builds?authorization=<TheApiKey>

Setting up GitHub to allow AppHarbor to pull code

Login to GitHub and select your repository. Click on ‘admin’

github-repository-admin

Select “Service Hooks” on the left and then click on “App Harbor”.

  • Give the ApplicationName as Application Slug, and the API Key that was extracted earlier as the ‘Token’.
  • Check the ‘Active’ box to enable push to AppHarbor.

Hit Update.

If your repository is private, give read access to the user ‘apphb’. If you repository is public, no need.

github-add-service-hooks

This sets up the required hooks between GitHub and AppHarbor to pull code and build on every commit.

Next let us setup some SQL Server space for ourselves.

Setting up Database on AppHarbor

Database service is available as an Add-on. So from your Application console on AppHarbor, select the ‘Addon’ menuitem.

Scroll down to find SQL Server

sql-server-add-on

Click on ‘See more’ to see the plan details

sql-server-options

Pick a plan that suits your needs and Install.

On successful install, you will navigate back to the Application Console with a success message and the Add-on will get listed

successful-addon

Click on the SQL Server link to Navigate back to the Add On Page.

SQLServer-addon-page

Click on ‘Go To SQL Server’ to get the connection information (all sensitive information obfuscated below).

sql-server-database-details

Click on the ‘Edit alias’ link and add the ‘Name’ that you use to specify the connection string in your web.config

edit-database-alias

connection-string-web-config

Now SignalR will overwrite the connection string in your web.config with the connection string of the database on AppHarbor.

Almost set.

If you are using EntityFramework with the default DropAndCreateDatabaseEverytime strategy, time to change it. Change it to CreateDatabaseIfNotExists. You do this in your ‘DBContext’ class as follows

db-context-strategy

I also have a fallback for schema name configuration in case you don’t get the default dbo schema access. Fortunately AppHarbor gives you dbo access. So we don’t have to change anything else.

Commit to Deploy

Okay, now we are all set let’s commit our code to GitHub and watch it get deployed on AppHarbor

deployment-in-progress

Once the deployment completes, the App will be ready. Click on ‘Go to your application’ to navigate to the application as deployed.

app-deployed-to-app-harbor

Debugging Tips

For ASP.NET MVC Applications disable the default error handler by commenting out the following line in Global.asax

// filters.Add(new HandleErrorAttribute());

Also set <customerrors=off /> in web.config to enable yellow screens of death in case of deployment issues. Once you get the application going you can enable them.

This app is deployed at http://funwithsignalr.apphb.com/

Conclusion

With that we have just deployed our first app on AppHarbor, a really cool ASP.NET Platform as a service. Now your hobby apps have a place to prosper on the web!






13 comments:

Unknown said...

I can see that CreateDatabaseIfNotExist worked for you but it didn't work for me can't see why. So I went and created tables using SQL and now the app works just fine. Can you think of any reason ?

Anonymous said...

Hi Bilal,
Good catch and my apologies.
CreateDatabaseIfNotExists just ensures that EF doesn't drop your database because you don't have DB creation rights so after the drop database won't get created.

To get the tables in, the easiest way is to run the schema script like you did. I should add that to the article explicitly.

Anonymous said...

Thank you for sharing your appharbor experience with us. This is awesomesauce!

Walter Ball said...

AppHarbor is really great and the good thing is that it is a free platform to asp.net application. To AppHarbor still some features are missing but still they are getting better as they are progressing.

rohit said...

Hi Sumit,

It gave me idea about apphb.



Thanks,
Rohit

Siddu 37 said...

hi
i was successful in uploading my build to appharbor but on clicking deploy it says deploy for 2356gggggggggggdhdhdhhd is queued

but i didnt get it active
site link say 404 error

how can i check if everything is fine all files are uploaded

Siddu 37 said...

hi
i am able to upload the build to appharbor but when i try to deploy
it says deploy is queued and i am not able to success it using link it gives 404 error

how to solve it

is there a way to check if all files are uploaded or not

Anonymous said...

Hello Siddu,
Queued usually means a build failure. Are you getting a Green Check mark or a Red crossed out circle in the Build Status?
Thanks and Regards,
Sumit.

Anonymous said...

Hello Siddu,
Queued usually means a build failure. Are you getting a Green Check mark or a Red crossed out circle in the Build Status?
Thanks and Regards,
Sumit.

Siddu 37 said...

its just a circle i was trying to deploy a mvc 2 it said queue
when i tried to deploy mvc3 its fine its deployed and published

how can i sove the MVC 2 problem

Anonymous said...

Hello Siddu,
I am having a little trouble with MVC 4 RC (beta was working fine) on the other hand myself. So both our problems could be with the reference libs.
Even though I have Nuget Package Restore installed in my solution AppHarbor says dll not found. I will try a few things like setting copy local to true and referencing the dlls. Will circle back and let you know my workaround that works finally.

Thanks and Regards,
Sumit.

Andrew said...

Thanks for this detailed post. I have tried all steps and some of them worked fine for me but some doesn't.

Unknown said...

It's exactly im looking for. Would compare on my current custom app by appmerge.com This is quite interesting and detailed.