This is what made me want to answer:
// Set the license... (Moderately expensive - accesses the file system)
You do not want any constructor, static or not, to do work like this. A constructor that accesses the file system is screaming "I CAN BLOW UP ANYTIME!", and can throw file system -related exceptions. You don't want that in a constructor, even less a
A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced.
Making your field
static ensures that the type (not the instance) has the field value, and your logic ensures it's set only once in the lifetime of your application. If your application isn't starting any process (requirement is once per process, right?), it can probably work.
I don't like that you're assuming that your application will only ever call this method in the same process though - it's a fair assumption to make, but since the requirements say once per process, I think your code should account for
System.Diagnostics.Process.GetCurrentProcess().Id, and call the
SetAsposePdfLicense() method if the method wasn't called in the process your code is running in.
I'm thinking of a factory living in a Singleton scope (or just as a plain Singleton), that would maintain an
HashSet<int> would be ideal for this) where each item is a process ID for which
SetAsposePdfLicense was called.
public class PdfInspectorFactory
private readonly ICollection<int> _processes = new HashSet<int>();
private void SetLicenseForCurrentProcess()
var currentProcessId = System.Diagnostics.Process.GetCurrentProcess().Id;
// set the license...
public PdfInspector Create()
var inspector = new PdfInspector();
// do whatever you need to do with your inspector before returning it
Then your client code can use this
PdfInspectorFactory and call its
Create method to get a
PdfInspector without having to care about whether or not the license is set.
The client code will not call the factory method in its constructor, rather only when it needs it.
public class SomeClass
private readonly PdfInspectorFactory _factory;
public SomeClass(PdfInspectorFactory factory)
_factory = factory;
public void DoSomething()
var inspector = _factory.Create();
// here you go, now do what you need to do with your inspector...
// todo: log or otherwise report exception
The factory instance should only exist as a Singleton instance - if you're using an IoC container that is very easy to accomplish. Otherwise you need to implement the factory class itself, as a Singleton.