PositiveGuy
PositiveGuy

Reputation: 47743

Testing a Singleton

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()

Upvotes: 1

Views: 1444

Answers (5)

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

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.

Upvotes: 0

Ozan
Ozan

Reputation: 4415

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

Upvotes: 2

Dustin Hodges
Dustin Hodges

Reputation: 4195

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.

Upvotes: 3

Sam Holder
Sam Holder

Reputation: 32936

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.

Upvotes: 0

DNNX
DNNX

Reputation: 6255

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

Upvotes: 1

Related Questions