How to use a singleton for storing the RedisConnection with Booksleeve?

Go To


I'm using Booksleeve (the latest nuget package).

I want so use a single connection for my whole Web Application so I'm storing it in a singleton:

public static RedisConnection Conn = RedisConfig.GetUnsecuredConnection(waitForOpen: true);

The RedisConfig.GetUnsecuredConnection method is the same as used in BookSleeve tests.

When I try to do an operation I get an InvalidOperationException: The queue is closed exception:

[InvalidOperationException: The queue is closed] BookSleeve.MessageQueue.Enqueue(RedisMessage item, Boolean highPri) in C:\Dev\BookSleeve\BookSleeve\MessageQueue.cs:73 BookSleeve.RedisConnectionBase.ExecuteVoid(RedisMessage message, Boolean queueJump) in C:\Dev\BookSleeve\BookSleeve\RedisConnectionBase.cs:794 ASP.welisten_booksleevetests_aspx.SaveDictionaryToRedis(Dictionary`2 dictionary) +173 ASP.welisten_booksleevetests_aspx.Page_Load(Object sender, EventArgs e) +67 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25 System.Web.UI.Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

I tried this with the waitForOpen parameter set to true and false

Here is the code I'm trying to execute:

 private void SaveDictionaryToRedis(Dictionary<string, string> dictionary)
        using (Mp.Step("Saving Data to Redis"))
            using (RedisConfig.Conn)
                RedisConfig.Conn.Strings.Set(DB, dictionary);

2012-04-04 00:13
by Carlos Mendes


Depending on what was copied, it could be that there is a missing call to:


which will open the connection and perform various handshakes. Thin the case of a singleton, this would be reasonable to do during the initializer.

However! Perhaps the problem here is that your second example is simply wrong. If Conn is, as described, a singleton - then it does not belong to that code, and you should not be using using. That would mean it is only usable once, and all subsequent access would fail. Just access the connection; no using here.

2012-04-05 20:47
by Marc Gravell