Home / Archive by category "MVC"

Mono Key Duplication Error in High Traffic Environment Workaround

After setting up the Auto Scaling feature in the Amazon cloud to automatically fire up and terminate virtual machines, depending on how much traffic there is, I ran into some issues with Mono.

In my environment I run Mono 3.4 using the Cherokee web server on Ubuntu 14.

Apparently there is a bug (that actually should be already fixed according to http://lists.ximian.com/pipermail/mono-bugs/2013-May/113692.html) which leads to erroneous Mono instances when there is high incoming traffic during startup of that instance. The bad thing is that this instance of Mono will then not recover during subsequent calls, only killing the process helps. I saw two kinds of this error, one with key duplication for key “roleManager” and one for key “sessionState”.

The stack trace of the error looks like this:

In my web.config neither role manager config entries, nor session state entires were present. But each web.config used in your own projects derives from a global web.config (just like on IIS and Windows). The location for that global web.config on Ubuntu for .NET 4 web apps is /usr/etc/mono/4.5/web.config.
Commenting role manager and session manager in the httpModules section will actually fix the problem.

So the pitfall of this workaround is that you can only use it if your web app does not use sessions and is also not using the role manager / membership functionalities.

Thanks for reading, hope this helps somebody to get Mono very stable in a high traffic production environment.

RESTful Web Services and MVC3 – simplified route mapping

There are many possibilities to create restful web Services using MVC. In one of my latest Projects I used MVC’ IConstraint interface to enable two kinds of calls for a single controller:

1. with an ID, e.g. GET http://project.programmierstube.com/api/users/123/info
2. without an ID, e.g. POST http://project.programmierstube.com/api/users/register

When setting up the route mapping (e.g. in an area registration or global.asax.cs) you can specify a constraint for a placeholder in the routing url:

Notice the assignment of a RestServiceConstraint instance to the id parameter. Now when we have an incoming a request and this routing is selected due to the match of /api/users/ the constraint is being asked if the “id” parameter makes a valid match as well. Also using the constraint we can modify the assigned routing parameters.
When we don’t have an id, we want action to take the value previously assigned to id and if indeed an id is present we don’t want to change the values at all.

This is how the constraint implementation looks like:

On thing to note though is that I do make an assumption as how the id parameter looks like. In this case I assume it is an integer, and I know that I won’t have controller methods named as integers ;-) If for example you have GUIDs or MongoDB Object IDs you could check for the length of the ID parameter since it’s also unlikely to have 40 or 24 character long controller method names.

I found this approach to be simple, easy to set up and perfectly fit the needs for my projects.