I am trying to implement a serviced component as a singleton. Currently, my code is like this:
[assembly: ApplicationName("SingletonServicedComponent")]
[assembly: ApplicationActivation(ActivationOption.Server)]
[assembly: ApplicationAccessControl(false)]
[ComVisible(true)]
[JustInTimeActivation(true)]
[ComponentAccessControl(false)]
[ProgId("Singleton.ServicedComponent")]
[ObjectPooling(Enabled = true, MaxPoolSize = 1, MinPoolSize = 1, CreationTimeout = 5000)]
public sealed class SingletonServicedComponent : System.EnterpriseServices.ServicedComponent
{
private int value = 0;
protected override bool CanBePooled()
{
return true;
}
public int Increment()
{
return this.value++;
}
}
I am following the pattern of having it pooled, with a minimum and maximum instance count of 1. I signed my assembly and registered it with regasm and regsvcs. It shows up in Component Services console, and appears to be OK. However, when I instantiate it in different applications, I don't seem to be getting the same instance. Any thoughts?
Got it! I was getting an activation exception because I was not releasing (.Dispose(), = null) the instance I had, so other processes could not acquire a reference to it.