Posts Tagged ASP.Net MVC

A limitation of shared hosting and a push toward cloud computing

Today I ran into one of the limitations of shared hosting.

I’ve been struggling for the last few weeks to handle large files in my application. I published to the server earlier this week but with no success. All my tests failed after 10 minutes with a ‘Request timed out’ error. I was testing with an 80mb file but with a slow broadband connection (thanks to Ice). I had increased the timeout and max request length for the upload handler like so in the web.config:

   <location path="Upload.mvc/Upload">
         <httpRuntime  maxRequestLength="2097151" executionTimeout="10800" />

I ruled out any setting that I had control over, so the next step was to talk to my hoster, Blacknight, to see if they could help me out. They replied saying that they could not support the uploading of large files stating that:

It ties up too much system resources to upload huge files like that through asp and would cause problems for other users on the shared server.

Of course they’re right and I can’t really complain but it’s bitterly disappointing none the less.

For the foreseeable future, a couple of file uploads a month is all I require so I don’t want to pay for more expensive hosting packages. So back to the drawing board…

Cloud computing

I’m already using the Amazons S3 service for the storage of the images and I think I can upload directly from the client to S3. So I just need a high spec PC for a few minutes to process the files after the user has uploaded them. Once processed I will only ever have to deal with smaller versions or tiled sections. This looks like an ideal candidate for the Amazon Elastic Compute Cloud (EC2). Where I can pay by the hour for a high spec machine.

I was hoping to dip my toe into some cloud computing at some point but I didn’t want to be pushed. This is going to be a lot more work than I hoped and will need some effort just to cordinate between the web server, client machine and a cloud computer.

I haven’t decided on the final architecture yet, just thinking out loud, but looks like a lot of fun (and hard work) ahead…

Tags: , , , , ,

Handling multiple Web.config files in Visual Studio

I use different settings for my application when it is running on the live server and when it is running on my development machine. To manage this I name my config files using the target name like so:

  • Web.Debug.config
  • Web.Release.config

Adding the following to the project file will cause MSBuild to automatically copy the correct file to ‘Web.config’ when you build the project in Visual Studio. In my case the ‘BeforeBuild’ target was already in the project file but commented out, I just needed to add the copy task and related items.

<Target Name="BeforeBuild">
   <ConfigSourceFiles Include="Web.$(Configuration).config" />
   <ConfigDestinationFiles Include="Web.config" />

So I select the right target from the toolbar and build/publish and the correct settings are used.

Where possible, to avoid duplicating settings, I create separate config files and point the main config files at it using the configSource attribute. For example the page settings for debug and release are the same so my pages element in both files look like this:

   <pages configSource="Configuration\System.Web\pages.config" />

Before implementing this I was manually changing or copying the config files before deploying to my server, so I hope this will help someone else save a bit of time.

Tags: , ,

Not mocking the HttpContext?

Someone asked me a question recently about mocking, and I had to say I didn’t have much experience with it. I found this answer a bit odd myself as I’ve been doing a lot of TDD with an ASP.Net MVC application. I’d seen lots of people talking about mocking the HttpContext and wondered “Why haven’t I had to do this?”.

The first time I started TDD, I was working on converting an existing ASP.Net application to ASP.Net MVC. Most of my controller actions checked the current security principle in the context, so I immediately ran into problems when trying to test these.
I didn’t know much about MVC or TDD so used what I did know. I knew about a powerful component of MVC called an action invoker, that could be overridden to handle some parameter stuff. I found that I could easily create an ActionInvoker that would populate parameters of a certain type (namely IPrincipal) using the following code:
public class ActionInvoker : ControllerActionInvoker
   protected override object GetParameterValue(System.Reflection.ParameterInfo parameterInfo)
      if (parameterInfo.ParameterType == typeof(IPrincipal))
         return HttpContext.Current.User;

      return base.GetParameterValue(parameterInfo);

And put the following into my controller constructors:
public MyController()
   ActionInvoker = new ActionInvokers.ActionInvoker();
This made it easy to test my controller actions. I just needed to mock a simple IPrincipal. I used a similar solution when my file upload controller needed access to HttpContext.Current.Request.Files.
At the moment my code only depends on a few simple objects that come from the HttpContext and not the context itself. Surely this is better, if not please comment. I’m going to change the above to conform with everyone else by mocking the HttpContext but the only reason I see for doing this, is that the above action invoker is not testable. Is this the only reason?
Before you point it out to me, I have recently moved most of my security code into AuthorizeAttributes. Now my unit tests for invalid calls (security wise) to controller actions all fail but that’s for a future post.

Tags: , ,

What's up with the Tadmap project?

If you’ve been following the Tadmap blog then you might be wondering why there haven’t been any updates since November. Well here’s why.

Before Christmas I decided to try out the new ASP.Net MVC framework, for no other reason than wanting to learn more about it. My initial impression was that it would be ideal for Tadmap, so I decided to go for it. The change over meant refactoring all the pages I already had into controller and views, and I got 90% there very quickley. As I had never used MVC before I started following Rob Conery’s MVC Storefront series to make sure I was doing things right. This may or may not have been a mistake.

First, he was using a fancy new version of the Repository pattern which I liked the look of very much, this lead to even more refactoring and learning more about LINQ on the way.

I tried to ignore the Test Driven Development aspect of what Rob was doing, but eventually I downloaded Nunit just to try my hands at writing an automated test. Straight away I was hooked on TDD. I tried limiting TDD to just new code but to make the new code testable meant refactoring the old code to reduce dependencies.

It’s taken a lot longer then I wanted but I am having a lot of fun doing it. My code is clean and loosely coupled and I have over a hundred unit tests and a bunch of integration tests that I can fire with one click.

Even though I have no boss and as yet no users, I have still run into the age old problem of doing it right versus getting it done. There is the ultimate balance to be made between making things perfect while achieving the goals and real world features that help our moral and motivation. It’s starting to bother me that I haven’t been able to add any new features in over a month.

So in the next few weeks I hope to get an, albeit imperfect, ASP.Net MVC version of the application up on the live server. I’ll then be in a position to iron out the last few post refactoring bugs or implement a cool new feature when I need the motivation to keep going.

Tags: , , ,

More blogging in 2009

It’s a new year, a time to start those good habits again. One thing I want to start doing this year is to write more blog posts. Four blog posts in November, that’s pretty good going for me but then nothing in December. The reason I hesitate when it comes to publishing stuff is that I’m not very good at writing. But that’s not going to change with out some practice.
The other reason not to publish something is the feeling that it’s not worth saying unless it’s going to contribute something that other people actually want. But as you can see from this post, I don’t care about that anymore.  I’m just going to say stuff and get things off my chest.
So what am I going to write about? I will try to keep it somewhat technical in nature. Professionally I work with ASP.Net 2.0 but on my Tadmap project I’m getting to work with some cool new stuff like C# 3.0, MVC, LINQ and Test Driven Development. All of which may be food for future blog posts.
As you can see on my blog page, I have added my twitter updates. I’ve had a twitter account for a while now but just started using it lately. This is just another way for me to practice writing. I think this whole twitter lark maybe just a fad but I’ll give it go for while and see what happens.

Tags: , , ,

ASP.Net MVC on Blacknight

Update: SP1 is now installed for ASP.Net 3.5. So now the only DLL you need to upload is ‘System.Web.Mvc.dll’.

I was reading a little bit about the new ASP.Net MVC stuff. I think it would be ideal for my Tadmap (image hosting) project. Before I started any major development re-work I needed to check that my hosting provider, Blacknight, could host it.
So I uploaded the sample MVC application. Of course it didn’t work at first but I don’t give up that easily and it is now up and running. I think the following steps will work for anybody else trying the same.
As Blacknight do not have SP1 installed for ASP.Net 3.5 you have to upload the new DLLs when you deploy. Simple instructions can be found here, Bin Deploying ASP.NET MVC.

My windows account on Blacknight is using IIS 6.0 which does not automatically support MVC. In order to handle URLs the MVC way you will need to follow the instructions here, ASP.NET MVC on IIS 6 Walkthrough. Basically, you just need to grab the code for the Global.asax.cs file and add a ‘.mvc’ file mapping using your Blacknight control panel.
To add the file mapping, just go to the ‘Web’ tab of your domain and add ‘mvc’ to the ASP.Net 2 mappings. It should look like the image below and make sure that ‘Script Engine’ is ‘On’ and ‘Verify File Exists’ is ‘Off’.

One small (cosmetic) problem is that the URLs aren’t as clean as they should be because the ‘mvc’ extension is needed. So the URL for the About action of the Home controller that should look like ‘‘ instead looks like ‘‘. I have seen solutions to this problem as well but that’s for another day. Right now I have the not so small task of converting an existing ASP.Net application to an ASP.Net MVC application.

Tags: , ,