I have a Ninject IoC container which has many bindings. I'm binding in my linqpad script.
However, this fails when I try to run more than once, because there are then multiple bindings for the same types on the second run, so when I try to get from the IoC container it fails.
Is there an application start or init function I use from Ninject so the binding only happens once?
In an MVC program for example, I would use Application_Start()
Example: In this program each time the program is run the output count is increased. Is there an application start event I can hook into to reset the count?
My actual needs are to prevent double binding of ninject kernels, so please no "solutions" that fix the singleton, I know it's not thread safe etc!
void Main()
{
{
var x = Singleton.Instance;
Console.WriteLine(x.count++);
}
}
// Define other methods and classes here
public class Singleton
{
private static Singleton instance;
private Singleton() { }
public int count;
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
instance.count = 0;
}
return instance;
}
}
}
I didn't realise this happened with statics in LinqPad. Good find.
Have you tried wrap the call to Application_Start()
with a flag in your Singleton
class in a similar way to your use of if(instance == null)
.
Something like...
private static bool _initialised;
public static void Initialise()
{
if(_initialised)
return;
_initialise = true;
something.Application_Start();
}