Posts

Showing posts from August, 2012

SpinLock SynchronizationLockException - The calling thread does not hold the lock

Here is the code from a console app. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication48 { class Program { static readonly SpinLock SpinLock = new SpinLock(); static void Main( string [] args) { bool lockTaken = false ; try { SpinLock.Enter( ref lockTaken); if (lockTaken) Console.WriteLine( " Lock taken " ); } finally { if (lockTaken) SpinLock.Exit(); } Console.WriteLine( " Done " ); } } } So why would this single-threaded app tell me that the thread trying to call SpinLock.Exit() doesn’t hold the lock? SpinLock is a value type.  When you mark a field referencing a reference type as readonly you are not only making the field unassigna

C# scripting in .NET

Based on a couple of days of experimenting it seems that I now have C# scripting working in my application to a level that I am satisfied.  Because I couldn’t have achieved this without looking at other people’s examples I thought it was only fair that I share what I now have.  Firstly I want to say that my purpose was to give the user the ability to write procedural code rather than object code.  Although it is still possible for the user to write OOP source code my requirement was to let the user define a function which returned a specific return type. public interface ICompiledFunction < T > { T Execute(Dictionary < string , object > variables); } A typical script might look something like this public decimal string Main() { return " Bob Monkhouse " ; } To create an instance of the ICompiledFunction<T> I use ICompilerService, which is defined like so public interface ICompilerService { bool Compile < T > ( string [] scripts,

Why I dislike DLR

It took me hours last night to work out why I was getting a null result from the Value property in the following code when accessed via IronPython public interface ISomeInterface { decimal Value { get ; } } How could a non nullable type possibly return a null (or “None”)?  It turns out that when I set my variable using the .NET scripting API ScriptScope.SetVariable(variableName, (ISomeInterface)value); The scripting engine still works on the implementing object rather than the interface.  This means that if my object has a method “DoSomethingThatScriptingShouldNotHaveAccessTo()” then scripting has access to it! In my case this bit me because I had a Nullable<decimal> property called “Value”, and the Value property implemented explicitly for the interface looked like this decimal ISomeInterface.Value { get { if ( this .Value == null ) CalculateValue(); return this .Value; } } In this case I expected the script to access the variable’s “Value” propert

How many times does a day occur within a date range?

Given two dates I need to know how many times each day of the week occurs within that range, so that I can calculate a value where the factor varies by day.  So I wrote the following code which returns an array of integers, position 0 will tell you how many Sundays there are, position 6 will tell you how many Saturdays, and so on. int [] CountDays(DateTime firstDate, DateTime lastDate) { var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1 ; var weeks = ( int )Math.Floor(totalDays / 7 ); var result = Enumerable.Repeat < int > (weeks, 7 ).ToArray(); if (totalDays % 7 != 0 ) { int firstDayOfWeek = ( int )firstDate.DayOfWeek; int lastDayOfWeek = ( int )lastDate.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek) lastDayOfWeek += 7 ; for ( int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek ++ ) result[dayOfWeek % 7 ] ++ ; } return result; }

ASP MVC Silverlight control not appearing–404 not found

Just a quick tip in case you have deployed your ASP MVC app with silverlight controls in it which are not appearing.  I experienced this recently when deploying to IIS6.  The solution is Open the IIS manager app (Start->Admin->Internet Information Services (IIS) Manager) Expand the local computer node Then expand the Websites nodes Right-click your website and select “Properties” Click the “Http Headers” tab At the bottom of the page click the “MIME Types” button Click the...

Creating a pooled lifetime manager for Unity

My application has a child container per request, this is because there are a lot of services used in each request which depend upon an IUnitOfWork.  I thought it would be nice if I could define a pool of these IUnitOfWork instances so that any cost involved in creating them is reduced, they can just be reused in a round-robin fashion.  Well, more accurately, the object space (EcoSpace) on which they depend can anyway. A pool can now be registered like so… // Must be called once, when the container is created container.AddNewExtension < PooledLifetimeExtension > (); // To register a pooled item container.RegisterType < ISomeItemThatIsExpensiveToCreate, SomeItemThatIsExpensiveToCreate > ( new PooledLifetimeManager()); The number 10 specifies how many instances at once may be stored in the pool.  If an instance requests an item from the pool when it is empty it will get a new instance, it’s only at the point where an instance is returned to the pool that the