ASP.NET Web API Cannot Connect to Azure Redis Cache

Asked 1 years ago, Updated 1 years ago, 122 views

I'd like to access Azure Redis Cache from ASP.NET Web API.
I implemented the controller by referring to this official document and blog, but I can't connect.
How to use Azure Redis Cache
Using Azure Redis Cache with ASP.NET Web API

public class TestController:ApiController
{
    private static connectionMultiplexer_connection;
    public static connectionMultiplexer Connection
    {
        get
        {
            if(_connection==null||!_connection.IsConnected)
            {
                _connection=ConnectionMultiplexer.Connect("myredis.redis.cache.windows.net, abortConnect=false, ssl=true, password=<password>";
            }
            return_connection;
        }
    }

    public IHttpActionResult Post()
    {
        try
        {
            IDatabase cache=Connection.GetDatabase();
            cache.StringSet("foo", "bar");
            string foo=cache.StringGet("foo");

            return Ok (new {foo=foo});
        }
        catch (Exception e)
        {
            return InternalServerError(e);
        }            
    }    
}

Based on the above code,
IDatabase cache=Connection.GetDatabase();
Breakpoints do not stop after
return Ok (new {foo=foo});
It flew to and there was no exception.
However, if you run it with the same code in the console application, you will be able to connect.

class program
{
    private static connectionMultiplexer_connection;
    public static connectionMultiplexer Connection
    {
        get
        {
            if(_connection==null||!_connection.IsConnected)
            {
                _connection=ConnectionMultiplexer.Connect("myredis.redis.cache.windows.net, abortConnect=false, ssl=true, password=<password>";
            }
            return_connection;
        }
    }
    static void Main (string[]args)
    {            
        IDatabase cache=Connection.GetDatabase();
        cache.StringSet("foo", "bar");

        string foo=cache.StringGet("foo");

        Console.WriteLine(foo);
        Console.ReadLine();
    }
}

Is there anything I need to consider when calling from the controller of the Web API?
Thank you for your cooperation.

Environment
Windows 8.1 Pro Visual Studio Professional 2015 Version 14.0.2470.00 Update 1
.NET Framework 4.6.1

c# visual-studio asp.net azure redis

2022-09-30 18:16

1 Answers

Based on the above code, IDatabase cache=Connection.GetDatabase();
The breakpoint does not stop after that and returns Ok(new{foo=foo});
There is no exception as it flew to the .

Are you debugging Debug builds instead of Relase builds?

Also, I think you have confirmed it, but are there any slight errors in the connection string?


2022-09-30 18:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.