I got assigned a rather annoying bug today whereby when a user using Firefox 7 went to a page that streamed a file for download, Firefox immediately stopped processing the page and returned the following error

Corrupted Content Error

A little bit of GoogleFu brought me to Firefox Bug #681140 - Corrupted Content error due to multiple Content-Disposition header field instances, new in Firefox 7.

There are a couple of reasons why this error will get thrown but the bottom line is Firefox thinks this is a form of response-smuggling and blocks it, in fact it doesn’t just block it, it denies any knowledge of anything to do with it, I could not for the life of me get Firefox to give me the response headers no matter what extension I used. In the end fiddler revealed the issue

content-disposition: attachment
Content-Disposition: attachment; filename="results.xls"

Fairly simple issue here, there are two Content-Disposition headers being sent to the browser and Firefox 7 throws it’s toys out of the pram if there is more than one.

Now, the cause.

I loaded up the controller that was generating these downloads and right enough, I found

Response.AddHeader("content-disposition", "attachment");
return File(content, format, filename);

I think you can see where I am going here. MVC is pretty good at removing a lot of the boiler plate we normally have to write and this is one of those cases. When you use the Controller.File method a number of headers are automatically set for you on the Response stream, if you provide a file name then one of those headers is Content-Disposition.

Ta-da, the source of the duplication.

So, when using FileResult in MVC, be very careful which additional headers you send out, check them in something like fiddler and don’t just assume that browsers in the future will accept malformed or duplicate headers.

 

Update: Jarek has pointed out that the solution presented won’t be necessary in the final version which is good news.

Update: (18/04/11): This is no longer required as of the latest NLog 2.0 nightly build, see http://nlog-project.org/2011/04/14/simplifying-nlog-extended-dll-usage.html for more details!

I recently upgraded a web application to use NLog 2.0 beta 1 (from 1.0 refresh) and part of my layout makes use of ${aspnet-request} in order to get things like IP and request URL. After upgrading, the application refused to start and would throw the following exception.

LayoutRenderer cannot be found: ‘aspnet-request’
at NLog.Config.Factory`2.CreateInstance(String name) in c:\NLogBuild\src\NLog\Config\Factory.cs:line 170
at NLog.Layouts.LayoutParser.ParseLayoutRenderer(ConfigurationItemFactory configurationItemFactory, Tokenizer sr) in c:\NLogBuild\src\NLog\Layouts\LayoutParser.cs:line 210
at NLog.Layouts.LayoutParser.CompileLayout(ConfigurationItemFactory configurationItemFactory, Tokenizer sr, Boolean isNested, String& text) in c:\NLogBuild\src\NLog\Layouts\LayoutParser.cs:line 78
at NLog.Layouts.SimpleLayout.set_Text(String value) in c:\NLogBuild\src\NLog\Layouts\SimpleLayout.cs:line 113

Now one of the changes in NLog 2.0 is that a number of layout renderers have been moved to NLog.Extended.dll, and I had made sure that my application referenced the new DLL as well as NLog.dll. When I ran the application I could confirm that both DLLs were present and loaded any yet my application still crashed out.

I posted this problem on the NLog Forums and while someone else is having the same problem, I never got a solid answer from the post. Today, 2 weeks later, I was perusing the forums just in case there were others reporting this problem and I came across a post where someone was describing similar issues, amongst the replies was a solution from Jarek Kowalski, the creator of NLog. Jarek points out that to use the layout renderers in NLog.Extended you need to add a few lines to your NLog config.

<nlog>
  <extensions>
    <add assembly="NLog.Extended" />
  </extensions>
  ...
</nlog>

You also need to make sure that NLog.Extended.dll is in the same folder as NLog.dll.

Once I saw this it made perfect sense but I never saw this anywhere in the documentation relating to changes in NLog 2.0. I added the snippet to my application’s config file and I was once again able to use ${aspnet-request}.

Happy logging

 

I have been doing some Test-After Development unit testing on an ASP.NET MVC app and hit code that uses the cache through HttpContext.Current.Cache (I bet @serialseb would just love that! :P ) which as you may know is a royal pain in the arse for getting at or mocking without a web server.

As most sane people do I binged (that just does not sound right!) for an answer to my problem and came across a few interesting things but the closest I came initially was a post from Phil Haacked on his HttpSimulator cache where someone mentioned that using HttpRuntime got around the issue. I tired, it failed and I went back to Bing and came up with a post on Scott Hanselman’s blog where he details a conversation about simply including System.Web along with System.Web.Caching in non-web applications. I was skeptical but people have noted success with this method so I tried it and what did I find, it sodding well worked!

Granted the articles I found are old but this is the first time I have tried to unit test the Cache before so this was quite useful.

In other news I have ordered a copy of Working Effectively with Legacy Code by Michael Feathers from Amazon after @chriscanal recommended it, looking forward to having a read.

 

Well, I was going to be writing up a review of DDD7 but after seeking out a good 3 column layout that I actually liked for WordPress last night (and failing) I have decided to take a new course of action.

I have been wanting to write a blog with ASP.NET MVC for quite some time, just never really found the time. I am now however, faced with a 7+ hour drive home from Reading and I think this would be the perfect time to write one, if nothing else just to see if I can actually do that in 7 hours :)

The tools I have with me are:

  • Laptop
  • Mobile Broadband Dongle
  • Visual Studio 2008
  • SQL Server 2008 Express
  • ASP.NET MVC
  • ADO.NET Entity Framework
  • Endless antics from Gary Short
  • Time

Update:

I knew it was adventurous and it was far more fun to be sociable in the car and help Colin Mackay out on the Developer Day Scotland website than it was to work on the blog. Saying that however, I think I am going to continue on with the blog and turn it into an MVC tutorial (or perhaps “Yet Another MVC Tutorial” better suits) which would allow me more time to develop it and actually provide something useful on the actual blog as well.

So while I don’t actually have a new blog yet, this has given me the idea to develop my own blog system in MVC and publish a tutorial along the way. Neat huh? (Well I think so)

DDD7 post will follow in the next couple of days but in the meantime, a big thanks to everyone who helped organise and run Developer Day 7 and a huge shout out to everyone who attended and made it what it was!

 

Update: Some how I now have a twitter account, i’m saying nothing on how that happened.

First, sorry for the lack of updates and also the fact the blog had to be rolled back, there was a problem during an upgrade and the database decided to throw a wobbler so I had to restore from backup. I am also in the middle of my 3rd year degree examinations (3 down 1 to go) so time has been pretty scarce of late.

I start my position at NCR on the 19th and between now and then I seem to have been filling up a rather busy schedule between both work and social life. I guess I want to make the most of my last few days of “freedom” before my summer of corporate life begins.

Developer Day Scotland

Anyway, one of my lighter items is tomorrow. I will be attending Developer Day Scotland at Glasgow Caledonian University which has been very kindly organised by Colin Mackay. The event will see speakers from across Scotland talking on a variety of .NET and SQL Server related topics.

Barry Carr (a ex-colegue of mine) will be delivering his talk on Regular Expression (which I have actually yet to witness and I wonder if the Macbook will be aiding him this time) and another North East Scotland .NET founder Gary Short who will be presenting on The Web Client Software Factory. I will also be attending Tom Wardill’s talk on the ASP.NET MVC Framework which is of great interest to me since I have been doing quite a bit of work with the framework and I do like the features it brings to the ASP.NET dinner table.

So it’s late and I have some last minute things to catch up on before I rest, I just wanted to post this last minute item so that when I return and do a write up it wont be so out of the blue.

Anyone attending tomorrow I will possibly see there.

© 2011 Andy Gibson
Header image courtesy of Don Solo
Suffusion theme by Sayontan Sinha