Tag Archives: .Net

Background Tasks in ASP.Net with HostingEnvironment.QueueBackgroundWorkItem

.Net 4.5.2 was released on May 5th, you can read all about it on the .Net Framework Blog or MSDN Library release notes. It includes HostingEnvironment.QueueBackgroundWorkItem, which lets you queue background threads from within an ASP.Net web application. This is useful for long running tasks that don’t need to complete before returning a response to the user.

elmah

ELMAH on MVC

ELMAH is a library you can drop into any ASP.Net WebForms or MVC applicaiton and automatically capture and record every unhandled exception in your application. User get a 404 looking for a page that doesn’t exist? Recorded. Try to access a member of a null object? Recorded. Database query throw an error caused by gremlins? Recorded.

There are other sources to tout how great ELMAH is (Scott Hansleman’s initial piece and again when NuGet was released) so I won’t go into that here. This post is about the minimum steps to getting ELMAH running on ASP.Net MVC.

ELMAH is pretty well documented, in fact it was initially just supposed to be sample code demonstrating ASP.Net features for an MSDN Article, however, however integration with MVC is sorely lacking in the official docs. In tradational ASP.Net you would set customErrors off in web.config to show a custom page after an error instead of the Yellow Screen of Death.

<?xml version="1.0"?>
<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

The crux of the problem is MVC uses a global action filter named HandleErrorAttribute for the same purpose. But because HandleErrorAttribute takes care of the error and never passes it along to ELMAH. Oddly, the “official” solution to this is a question on StackOverflow. Essentially the suggested solution from the author of ELMAH is to inherit HandleErrorAttribute and override the OnException member so that the exception is passed to ELMAH.

But it seems odd that this wouldn’t be a core part of the ELMAH package given how many people want to use it on MVC. Shouldn’t this be turned into a DLL that can be dropped into a project like ELMAH? It should. And it has.

The ELMAH.MVC NuGet package by Alexander Beletsky takes care of the HandleErrorAttribute and even adds some configurable authorization to the ELMAH controller. It’s all nicely documented on the projects GitHub page. Simply install the NuGet package, delete any existing HandleError filter you have and you’ve got ELMAH working in MVC.

(One quick note about authorization in ELMAH.MVC. If you look at the source code you’ll see an [Authorize] attribute on the controller. This isn’t the core ASP.Net attribute, it’s a custom ELMAH.MVC attribute that works with the web.config settings.)

However there was one configuration I missed the first time around. I had ELMAH deployed to a test environment and hit an error. Thinking I was so smart for having taken the 90 seconds to install ELMAH I went to https://myapp/ELMAH only to receive a 403 authentication error. I hadn’t configured the ELMAH.MVC authorization yet so I wasn’t sure what was going on. Only after a little googling did I find my missing configuration, allowing remote access.

<elmah>
    <security allowRemoteAccess="1" />
</elmah>

If you do allow remote access, make sure you also turn on the authorization.

MySpace logo

In Defense of MySpace

It’s easy to dump on MySpace. It was the twenty-first century equivalent of GeoCities, but with an even lower barrier to entry. When MySpace lost the social network race to Facebook very few people were sad to let go of, what Seth Meyers once called, ‘abandoned amusement park of the internet.’

But for a certain group, it’s time to look a little closer MySpace. That group is .Net developers.

For a brief period, MySpace was the most visited site on the internet and .Net technologies were powering it. Virtually no other company, Micrsoft itself excluded, has ever run .Net at the sort of scale that MySpace was innovating at the bleeding-edge of the CLR to keep up. And with little fanfare, they open-sourced a number of their projects. That work is still ahead of it’s time in many ways, and it’s time .Net devs took notice.

qizmt

Qizmt logoMapReduce is the framework that lets Google scale it’s data to massive commodity clusters in a reliable way. This method for distributing computation has been so successful Yahoo!, Facebook, LinkedIn and countless others are now using an open source competitor, Hadoop. By far my favourite project to come out of MySpace was qizmt. qizmt is an open source MapReduce framework for .Net. It allowed you to horizontally scale as your computational load increased. The source is hosted on Google Code at http://qizmt.myspace.com/.

MSFast

Many web developers will be used to tools like Firebug or the WebKit Inspector, but if you are using Internet Explorer, especially a pre-8 version, you’re mostly out of luck. MySpace built their own browser debugger that they released for free. It brought IE 8 very close to feature parity with the above mentioned tools, but it could also be used with browsers as old as IE 6! The plugin is available from their site with links to the project on Google Code, http://msfast.myspace.com.

Data Relay

My company recently built their own framework to speed up intercommunication between systems. Had we known about Data Relay, we may not have needed to reinvent the wheel. Data Relay is a middle-tier framework to facilitate message passing at scale. This is the sort of heavy weight architectural project that seems to thrive in the non-.Net open source community, but doesn’t get the same focus in Microsoft shops because it’s not provided out of the box. This project isn’t as well documented, but you can find the code on Codeplex http://datarelay.codeplex.com/. There’s also an interesting mention of it by a former MySpace engineer on his blog, http://www.metafilter.com/104479/Im-not-quite-certain-who-this-space-belongs-to-any-more#3752867

It’s easy to look at a loud profile page on MySpace and assume the engineering behind the scenes was equally frightening. But the fact is you don’t rise to the top spot on the Alexa rankings without some pretty serious brainpower on the development team. I think it’s time developers, and the .Net crowd in particular, paid their dues.