Showing posts with label AppHarbor. Show all posts
Showing posts with label AppHarbor. Show all posts

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!