ASP.net 4.5 applications on Docker Container

Docker makes application deployment easier than ever before. However, most of the Docker articles are often written how to containerized application that runs on Linux boxes. But since Microsoft now released windows containers, the legacy (yes, we can consider .net 4.5 as legacy apps) .net web apps are not left out anymore. I was playing with an ASP.net 4.5 web application recently, trying to make a container out of it. I have found the possibility exciting, not to mentioned that I have enjoyed the process entirely. There are few blogs that I have found very useful, especially this article on FluentBytes. I have however, developed my own scripts for my own application, which is indifferent than the one in FluentBytes (thanks to the author), but I have combined few steps into the dockerfile – that helped me get going with my own containers.
If you are reading this and trying to build container for your asp.net 4.5 applications, here are the steps: In order to explain the process, we’ll assume our application is called LegacyApp. A typical asp.net 4.5 application.

  • We will install Docker host on windows.
  • Create a directory (i.e. C:\package) that will contain all the files needed to build the container.
  • Create the web deploy package of the project from Visual studio. The following images hints the steps we need to follow.

Image: Create a new publish profile

Image: Use ‘Web Deploy Package’ option

Important note: We should name the site in the following format Default Web Site\LegacyApp to avoid more configuration works.

  • Download the WebDeploy_2_10_amd64_en-US.msi from Microsoft web site
  • I have ran into an ACL issue while deploying the application. Thanks to the author of FluentBytesarticle, that provides one way to solve the issue by using a powershell file during the installation. We will create that file into the same directory, let’s name it as fixAcls.ps1. The content of the file can be found here
  • We will create the dockerfile into the same directory, with the content of this sample dockerfile

At this moment our package directory will look somewhat following:

  • Now we will go to the command prompt and navigate the command prompt to the directory (i.e. C:\package) we worked so far.
  • Build the container
     c:/> docker build -t legacyappcontainer .
  • Run the container
    c:/> docker run -p 80:80 legacyappcontainer 

Now that the container is running, we can start browsing your application. we can’t use the localhost or 127.0.0.0 on our host machine to browse the application (unlike Linux containers), we need to use the machine name or the IP address in our URL. Here’s what we can do:

  • We will run the inspect command to see the container IP.
     C:\> docker -inspect  
  • Now we will take the IP from the JSON and use the IP on our URL to navigate to the application.

The complete dockerfile can be found into the Github Repo.

jQuery Autocomplete inside partially rendered user control?

Yesterday a very small issue put me into the hell for almost an hour. Finally I was able to figure out a workaround for me.

I am not claiming this is the best approach to solve this problem or stuff like that, but definitely it worked for me nice, and I did not find any downside of this yet.

Well, let’s get into the story, I was using the awesome jQuery Autocomplete plug-in inside my ASP.net MVC application. Initially I put this autocomplete textbox into the site.master page and it worked superb! I used the HtmlHelper extension model to accomplish my task. John Zablocki has written a wonderful post regarding this approach. I basically used his approach with subtle modifications. For those who has no idea what jQuery Autocomplete plugin is, it’s a regular html text input control that is registered by the jQuery autocomplete plugin library and when user tries to write something in it, it made an ajax request to the server to get the possible values for the given characters use has been typed. Again, please read the John’s blog for a good idea about this.

My problem begins when I moved this control from the master page and put inside a view user control in my ASP.net MVC application- that renders partially. To explain this more clearly, my user control has the following code

Now, from the Index.aspx (I am talking about the Home\Index.aspx -which generates for a hello world ASP.net MVC application), I am rendering this user control using AJAX. And I noticed that the jQuery is not working. Why??

Well, after spending few minutes I understood that, the script that used to register this control with jQuery is not executing when the DOM is modified using AJAX. Which means that if you write a simple ascx control, with only the following script code,

alert(“Hi”);>/script>

And render this ascx using Ajax. You will not see anything where I was expecting to see the message box saying “Hi” to me. Well, when you modify the DOM dynamically if there are any scripts that are not executed automatically, unless you do something to force this. This is the reason why my jQuery plugin was not working. So what I did is, forced the browser to load all the scripts that returned by the server during the AJAX call. And I loaded the javascript in window level- it actually depends on you-where it bests suit you.

So I was able to make this running-with a OnSuccess event handler for my AJAX call, and once it finished the AJAX invocation, I did a force load of all the scripts block resides into the ascx. Voila!