Home / Archive by category "C#"

Unity 4.6 UI and nested ScrollRect

I wanted to have nested ScrollRects with different scrolling axis using the new Unity UI 4.6. By default this cannot be achieved currently.

My goal was to be able to swipe through 4 pages of the main menu of my game (green axis) and then on one of the pages be able to swipe through a list of items (red axis):


So I created a subclass of ScrollRect that routes the drag events to all parent ScrollRects in case a simple rule checked in OnBeginDrag is true. For my simple case I just wanted to route events to the parent in case a horizontal drag was being initiated but the scroll rect is a vertical one, or a vertical drag was being initiated and the scroll rect is a horizontal one. This is my subclass:


Mono Running at 100% CPU Usage After Some Time (Under Load)

I am maintaining the backend of an online soccer app that is hosted on several Ubuntu servers in the Amazon cloud, running on Mono and ASP.NET with FastCGI on the Cherokee web server. I spent quite a while figuring out why after some time, actually only under load, the smaller servers would be stuck at 100% cpu usage consumed by the Mono process. On a medium machine this happened only once yet. In the AWS console it looked like this:

Before altering settings


A server is running fine for a while, then suddenly jumping to 100% cpu usage and not recovering from there even though over night the load drastically decreases. It would sit there forever, still serving requests though. After restarting Mono everything would be back to normal for some time until it’s back at 100%. The peak load is around 20 requests per second.

I tried to check a complete stack dump for the Mono process but without any finds.

My latest effort was to play with some environment variable settings for Mono. Setting MONO_THREADS_PER_CPU to a high number of 2000 actually fixed the problem with the cpu going to 100%.

Since I am currently using the Cherokee web server this is an excerpt of my cherokee.conf that configures the FastCGI source:

I actually had to include other environment variables that are normally inherited by mono from the web server as well (PATH, …), otherwise the application would not work.

I will continue to watch if the problem really vanished. Hope this helps somebody since it is hard to find a really good best practice guide or anything to setup and run production servers with Mono on Linux.


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.