开发者

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


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.


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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜