views:

200

answers:

5

I've created a singleton which in the constructor goes like this:

public static class MyCertificate
{
    private readonly static X509Certificate2 _singletonInstance = new X509Certificate2();

    static MyCertificate()
    {
        if(_singletonInstance == null)
            _singletonInstance = GetMyCertificateFromDatabase();
    }

    public static X509Certificate2 MyX509Certificate
    {
        get { return _singletonInstance; }
    }
...
}

the MyX509Certificate property returns _sigletonInstance.

What I need to do though is debug the methods being called such as GetMyCertificateFromDatabase(). So in an.aspx.cs I have this:

    protected void Page_Load(object sender, EventArgs e)
    {
        InsertCertificate();
    }

    private static void InsertCertificate()
    {
        X509Certificate2 certificate;

        certificate =  MyCerfiticate.MyX509Certificate;

    }

I am not quite sure how to step through so that I can step through the methods being called that help to set that singleton. It just steps to the property then returns when I debug the InsertCertificate()

A: 

I am assuming you are using visual studio. In visual studio go to Tools->Options->Debugging and uncheck the box that says step over properties and operators

Edit: I just noticed that you do the following:

private readonly static X509Certificate2 _singletonInstance = new X509Certificate2();

That'll prevent your _singletonInstance from ever being null when you check it.

Dustin Hodges
didn't make a difference..
CoffeeAddict
It doesn't even hit my constructor even if I put a break point on the if null check
CoffeeAddict
Double check that you didn't accidentally set your project to release instead of debug. You'll never hit any break points if you are building release dlls
Mark Arnott
definitely in debug mode
CoffeeAddict
Either don't set it at all (leave it null) so that the static constructor can detect the condition or have this static field declaration call GetMyCertificateFromDatabase(). Doing both will ensure GetMyCertificateFromDatabase() never gets called at all.
Jesse C. Slicer
+1  A: 

Why don't you try to set up a breakpoint in MysCertificate static constructor? This should help.

DNNX
did that, did not hit it.
CoffeeAddict
A: 

in the module window, does the module that has your singleton in appear in the list? Does it have symbols loaded. If not, manually load symbols for it and then you should be able to debug it.

Sam Holder
+2  A: 

_singletonInstance is initialized before MyCertificate() is called. There, you check if _singletonInstance is null and since it is not, GetMyCertificateFromDatabase is not called.

Ozan
fields are called before constructors..
CoffeeAddict
CoffeeAddict
just set it to null in initialization and in the MyX509Certificate property getter check if it is null, if so create the instance with GetMyCertificateFromDatabase, then return the field.
Ozan
A: 
public static class MyCertificate
{
    private readonly static X509Certificate2 _singletonInstance = GetMyCertificateFromDatabase();

    public static X509Certificate2 MyX509Certificate
    {
        get { return _singletonInstance; }
    }
...
}

set your breakpoint on the field with its initialization.

Jesse C. Slicer
ah since it's static no need to check to see if it's null first before assigning
CoffeeAddict
or is that check for null still valid? You have to have an instance of the singleton somewhere
CoffeeAddict
Unless GetMyCertificate() returns a null on the first call, then no, there is no need for a check for null. It is your one and only instance. If it COULD return a null on the first call, then you'll need to check in your static property and re-call GetMyCertificate().
Jesse C. Slicer