Deploy your first ASP.Net MVC App to AppHarbor

If you’ve been developing ASP.Net MVC apps lately you might be thinking some online or cloud based app hosting platform available as PaaS for Microsoft technologies specially for hosting ASP.Net MVC apps just like OpenShift, Heroku and other platforms are available for technologies like Ruby, Python, PHP, Node.js and even supporting CMS like WordPress. The good news for ASP.Net developers is that there is a PaaS platform available which you might already know. The platform is AppHarbor. AppHarbor runs over Amazon AWS and has some nice features that I won’t go into the details of. If you are interested in knowing how AppHarbor works you can see their page here.

Regardless of AppHarbor provides a decent service, new developers might still face some difficulty in deploying their applications to AppHarbor. Specially, if you are developing apps based on new Visual Studio 2015 templates like MVC. Their are different ways to deploy but I would follow below approach which in my opinion is good and provide auto deployment or in other words Continuous Integration (CI).

What you need?

We will be using following tools and accounts.

  • Visual Studio 2015 (any edition, I used Professional version)
  • ASP.Net MVC app created from VS2015 MVC template
  • GitHub repo for the app/project
  • Local git repo for app with remote repo set as your GitHub app repo
  • AppHarbor app

What’s not covered?

Our focus today is deployment of our ASP.Net MVC app to AppHarbor. Therefore, we won’t be going into the details of how application is created or its architecture, what’s new in Visual Studio 2015, what is MVC, what is Git and GitHub, and how to connect your GitHub repo to AppHarbor etc. We will assume that you already have all the pre-requisites and we will just focus what problems can we come across during deployment and how to fix them.

Deployment Steps

  1. Initialize a Git repo and connect it with your GitHub repo.
  2. Create AppHarbor app from your GitHub repo. Whenever we commit/push our changes to our GitHub repo AppHarbor will automatically fetches the latest push and build it. Upon successful build it will deploy the app on its server otherwise it will keep last successful build. This makes things very easy.
  3. Create an ASP.Net MVC application using Visual Studio 2015 MVC template.
  4. Add the packages folder to .gitignore
  5. Enable NuGet Package Restore. In VS2015 click Tools>Options and then select NuGet Package Manager and make sure both checkboxes are checked in this section.
  6. In VS2015 right click on project and click properties, then goto Build Events tab. In the Post-build event command line text area paste the following command.
    1. if not exist “$(WebProjectOutputDir)\bin\Roslyn” md “$(WebProjectOutputDir)\bin\Roslyn”
      start /MIN xcopy /s /y /R “$(OutDir)roslyn\*.*” “$(WebProjectOutputDir)\bin\Roslyn”
  7. Now commit and push your changes to GitHub.
  8. That’s it! AppHarbor will automatically fetch the latest version changes and build it and you can check your AppHarbor application on its URL.

What’s Next?

There are few things that you need to take care of specially related to security. This MVC app uses SQL Server Compact which isn’t a good option for production level apps. Secondly, your connection string or password to database must not be committed to public GitHub repo.

This post will just give you smooth start without any difficulties which I faced among other people that you can see in the resources section below.

If you think this was helpful or if I have missed anything please do let me know in the comments below.

Happy coding!

Resources

  1. https://blog.appharbor.com/2012/02/06/use-nuget-package-restore-to-avoid-pushing-packages-to-appharbor
  2. https://support.appharbor.com/discussions/problems/79727-error-msb3202-when-building
  3. https://support.appharbor.com/discussions/problems/78633-cant-build-aspnet-mvc-project-generated-from-vstudio-2015-enterprise#comment_37577678

How can I test if mod_rewrite on my server is enabled and working?

Note that mod_rewrite can only be used with web server Apache. Follow the instructions below to check whether module mod_rewrite is installed and correctly configured on your server.

Create the file .htaccess and add these two lines

RewriteEngine on
RewriteRule ^testing.php$ modrewrite.php

This tells the web server to load modrewrite.php when testing.php is requested.

Create the file modrewrite.php with this line

<?php echo "mod_rewrite works"; ?>

Create the file testing.php with this line

<?php echo "mod_rewrite does not work"; ?>

Now use your web browser to load testing.php. If you see “mod_rewrite works” your server has a working mod_rewrite instance. If you see anything else, including an internal server error, your server is not properly configured for mod rewrite.

What J talked about in 2013

The WordPress.com stats helper monkeys prepared a 2013 annual report for this blog.

Here’s an excerpt:

The concert hall at the Sydney Opera House holds 2,700 people. This blog was viewed about 37,000 times in 2013. If it were a concert at Sydney Opera House, it would take about 14 sold-out performances for that many people to see it.

Click here to see the complete report.

 

2012 in review

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

4,329 films were submitted to the 2012 Cannes Film Festival. This blog had 24,000 views in 2012. If each view were a film, this blog would power 6 Film Festivals

Click here to see the complete report.

 

Installing MS Web Deploy on different port

Web Deployment is a tool from Microsoft to simplify migration, management, and deployment of IIS web servers, web applications, and web sites. When you install Web Matrix it is installed by default. When installed it listens on port 80. If you are using other server like Apache on your system that also listens on port 80 then you won’t be able to start Apache due to conflict in the port. The solution is either use a different port for Apache like 8080. If you want to do that then other solution is to remove the web deploy and install it from command line. Here are the steps of doing this.

1. Uninstall Web Deploy from Add/Remove programs.

2. Download Web Deploy from

v2: http://www.iis.net/download/webdeploy

v3: http://www.microsoft.com/download/en/details.aspx?id=27430

3. Run the command prompt in Administration mode.

4. Run the following command:

#> msiexec /I <msi filename> /passive ADDLOCAL=ALL LISTENURL=http://+:<port>/MsDeployAgentService2/

e.g #> msiexec /I WebDeploy_x86.msi /passive ADDLOCAL=ALL LISTENURL=http://+:85/MsDeployAgentService2/

Thats it. Now you can use Apache on port 80 and Web Deploy will use the different port.

How do I: check if DLL has been loaded

Few days back I developed an ISAPI filter and when I installed on IIS server and tried to test it was not running. To troubleshoot I thought how would I know that DLL has been loaded by the IIS server successfully and there is no error. If you used Windows Task Manager it does not tell you which DLL has been loaded. There is a cool command line utility to check this. That is tasklist. According to its description:

“This tool displays a list of currently running processes on either a local or remote machine.”

To check whether a DLL has been loaded or not use the following command.

> tasklist /m yourdll*

The /m switch lists all tasks currently using the given exe/dll name. It also support wildcards. If the module name is not specified all loaded modules are displayed.

So, I used this command to check if my DLL has been loaded. It’s a really handy command to check some other stuff as well. Try to use /? to get complete list of switches and their description.

What kind of IIS documentation is this from Microsoft?


IIS documentation blank template
IIS documentation blank template

Today I was searching online MSDN library for IIS ISAPI filter development, and you know what? I came across a MSDN page which was a bare template and didn’t have any content. Look at the snapshot taken on August 15, 2011 🙂

If page is still not updated then you can visit now:

http://msdn.microsoft.com/en-us/library/ee532515.aspx