Sunday 21 July 2013

Setting up HttpHandler in IIS 7 or IIS 8 integrated mode

I spent hours troubleshooting a 404 problem with it and it turned out to be one of those doh! moments when I finally figured it out. I thought it was worth posting something about it here because during my research into the matter I found a lot of scattered and contradicting information about it on the web. Some said you have to strongly name and sign your assembly and deploy to the GAC in order to use a custom httphandler with IIS, which is not true, and others had some downright way of going about it.

In any case it's very very simple. All you need to do is declare your handler under web.config under <system.webserver> like this:

<system.webserver>
 <add name="MyHandlerClass" path="MyHandlerMethod" verb="GET" type="FullNamespace.ClassName, FullNamespace.SyncServer" resourceType="Unspecified" preCondition="integratedMode" />
</system.webserver>

Notes:

1)You must declare this inside the <system.webserver> not <systerm.web> and in fact make sure you do not include a declation for the same handler unser <system.web> otherwise it'll get confused

2)the name must match the name of your HttpHandler class

3)the path is the method in your httphandler class that you want to call which is also part of the path when invoking it

4)type is just the full assembly name for your custom header including the class name for your custom httphandler

5)resourceType is the equivalent to "verify that file exists"in II 6. leaving it as undefined prevents IIS from checking for a file specifically which may be what you need particularly if your handler just maps to a path rather than serving a file

6)this tells IIS to run the handler in integrated mode

now the most important one, for me, anyway and what kept me troubleshooting this for hours:

7)If it's an MVC project... MAKE SURE TO ADD IT TO YOUR IGNORE ROUTES CONFIGURATION!

Tuesday 2 July 2013

How to calculate the closest date to now in a collection using C#

there are a lot of unnecessarily over-complicated ways out there showing different ways of doing this so I thought I'd post what I think is the simplest and best solution for this.

Just a bit of context first: my scenario is that I want to find out which files in the collection that I have the closest publishing date to today based on the date set on the filet set they belong to.

 var now = DateTime.Now;
                
var closestDiff  = activeFiles.Min(x => Math.Abs((x.Fileset.PublishDate - now).Ticks));

                files = activeFiles.Where(x => Math.Abs((x.Fileset.PublishDate - now).Ticks) == closestDiff  ).ToList();

one important pointer:

make sure to save the DateTime.Now or whatever date you're comparing to in a variable first if you are using a dynamic date like that.

Don't forget that milliseconds count when dealing with datetime so you if you just use that statement inline the code will never match anything since the Ticks result will be different in the next code line and the closestDiff won't match anything