OneClutteredMind
OneClutteredMind

Reputation: 459

Redis Cache - A blocking operation was interrupted by a call to WSACancelBlockingCall

I have an ASP.NET Web Forms Page with the following code:

public partial class MyAspNetWebFormPage : System.Web.UI.Page{
   protected void Page_Load(object sender, EventArgs e){
      ClearRedisCache("RedisCacheConnectionString_01");
      ClearRedisCache("RedisCacheConnectionString_02");
   }

   private void ClearRedisCache(string redisConnectionString){
      var serverName = redisConnectionString.Split(',')[0];
      using (var redis = ConnectionMultiplexer.Connect(redisConnectionString)){
         var server = redis.GetServer(serverName);
         server.FlushAllDatabases(CommandFlags.None);
         redis.Close(true); // this was added as an effort to fix the problem but doesn't work
         
         // Another effort to make it work
         // Added after receiving the error
         do {
            Thread.Sleep(1000);
         } while (redis.IsConnected);
      }
   }
}

When I run this on my local box it seems to work perfectly fine. However, when I run it in our Azure Environment it throws an exception with the following message:

A blocking operation was interrupted by a call to WSACancelBlockingCall

Other examples that I've seen are caching the connection and keeping it open. Is that how I'm suppose to implement redis cache?

This code is executed from an Admin Dashboard that is completely disconnected from the Consumer Site. The Admin Dashboard doesn't use Redis Cache other than to clear it.

Any ideas are greatly appreciated. Thank you in advance.

Upvotes: 1

Views: 1035

Answers (1)

OneClutteredMind
OneClutteredMind

Reputation: 459

Looking into this further and playing with the settings here's what worked for me:

public partial class MyAspNetWebFormPage : System.Web.UI.Page{
   protected void Page_Load(object sender, EventArgs e){
      ClearRedisCache("RedisCacheConnectionString_01");
      ClearRedisCache("RedisCacheConnectionString_02");
   }

   private void ClearRedisCache(string redisConnectionString){
      string securityProtocol = (SecurityProtocol)Enum.Parse(typeof(SecurityProtocol), securityProtocol);

      var options = ConfigurationOptions.Parse(redisConnectionString);
      options.SslProtocols = System.Security.authentication.SslProtocols.Tls12;
      options.Ssl = true;
      options.AllowAdmin = true;
      options.AbortOnConnectFail = false;

      var serverName = redisConnectionString.Split(',')[0];
      using (var redis = ConnectionMultiplexer.Connect(options)){
         var server = redis.GetServer(serverName);
         server.FlushAllDatabases(CommandFlags.None);
      }
   }
}

Upvotes: 1

Related Questions