Archive for July, 2010

0

Inception review – 5 stars

Saw Inception last night, directed by Christoper Nolan (Memento, The Prestige, Batman Begins, The Dark Knight) and starring Leonardo Di Caprio, Joseph Gordon Levitt, Ellen Page, Tom Hardy and many others (even Michael Caine).

Put simply, the technology exists to join someone’s dream, and whilst in that dream, steal secrets if you’re good enough. Leonardo’s character is this best there is, but he is hired to implant an idea, rather than steal something.

To be honest, that’s as much as I’m going to say about the story, as it is something that should be allowed to unfold with you knowing as little as possible up front; we’re way beyond spoilers here ;)

The film has been incredibly crafted, and I don’t mean the actors’ performances or the special effects, both of which are excellent. The story is layered well, and covers a very important strand of Caprio’s character’s past. No-one’s character is one dimensional (although Michael Caine is only in the film briefly, so is not given much chance to evolve). Cillian Murphy does well in the film, I really felt his emotions over the relationship with his father, but again, I don’t want to give anything away.

In short, see it, I recommend it highly with 5 stars.

0

C# – converting an IEnumerable item’s single property to a CSV

I recently had to extract some items from an IList and pass their Guid identifiers to a legacy SQL Server stored procedure that required a list of comma-separated values.

Rather than loop round the list, appending items to a string with a comma at the end, then stripping the last comma off, or adding the chosen property to a IList, converting it to an array and then String.Join-ing that array, I came up with the following:

string divisionsCSV = String.Join(",", ((List)divisions).ConvertAll(d => d.DivisionID.ToString("b")).ToArray());

(There’s a casting of divisions to an List<IDivisionView> because a) divisions is an IList and b) ConvertAll is on the concrete implementation of IList)

I'm using ConvertAll to, er, convert a single property of every item in the list, in this case the Guid identifier of the division to a string. Incientally, I'm keeping the Guid parentheses thanks to the "b" parameter of the ToString call. The ToArray call then stuffs it all into an array ready for the String.Join call.

Any thoughts, comments or improvements?

0

A better app_offline.htm for ASP.Net

As we know, the presence of an app_offline.htm in an ASP.Net site will take the site down for maintenance or update, displaying whatever is in the file, e.g. an image, and some informative blurb. As ScottGu says, make sure the file is larger that 512 bytes to avoid the IE6 “friendly errors” feature.

Whilst it’s a convenient way to bring down a site, it brings it down a bit harshly, shutting down the App Domain and all sessions, which is really nice if your visitors are half-way through a financial transaction. I wanted a way to keep active users in their session, send new visitors to the site offline page and track how many people are still connected to the site.

My solution is two-fold: a simple HttpModule and a PerfMon graph. First, the HttpModule:

  1. Create a new class, calling it SiteUnavailable and make sure it inherits from IHttpModule.
  2. Use the smart tag to implement the IHttpModule members, which should give you Init and Dispose.
  3. In the Init, add the following:
    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
  4. Add the following method:
    void context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;
    HttpContext context = app.Context;
    if (System.IO.File.Exists(context.Server.MapPath("~/site_unavailable.htm")) && context.Session.IsNewSession)
    {
    context.Response.Redirect("~/site_unavailable.htm");
    }
    }
  5. In the Dispose method, remove the NotImplementedException and replace with a pithy comment of your choice ;)
  6. Add the following line to your web.config file in the httpModules section:
    <add name="SiteUnavailable" type="YourNamespace.SiteUnavailable"/>

That’s it – just create a file called site_unavailable.htm in the site root, filling it with an informative text, a logo, etc., and make sure it’s over 512 bytes. Now, when a vistor comes to the site and that page exists, they’ll be redirecetd to it. If you create / rename the file when users are active, they’ll still get to use the site until they end their session and try to visit the site again.

How’s it work? Well, the code speaks for itself: it checks for the site unavailable file and whether the session is a new one. If it is, then the user is redirected. The PreRequestExecuteHandler event is used because the Session object is available at that point, but main processing hasn’t really started yet. Yes, it’s looking for a hard-coded filename, but that’s what you’ve got with app_offline.htm as well.

That’s half the problem solved, now how do we see how many users are connected to the site? Enter Perfmon…

  1. Run Perfmon, and then right-click the graph and select Add Counters…
  2. For the Performance object, select Web Service
  3. Select the Current Connections counter, and then select your website from the “Select instances from list”, er, list
  4. Click Add, and then marvel at the metrics as they show the number of users connected to your site.

Now, when you enable the siteunavailable.htm page, you can watch the perfmon website connection counter go down as users finish their sessions. When it gets to zero, you can go ahead with your update, then rename the siteunavailable.htm page (to _siteunavailable.htm, for example) and hey presto, your site’s back and updated.

As an aside, it may help the perfmon connection counter reach zero quicker if the content of your siteunavailable.htm page says something like “please close this page to enable us to complete the upgrade quicker”, and it would be helpful to provide an estimated time of completion.

Thoughts? Comments?