none
IDistributedCache.Get() throws exception 'Failed to update database' RRS feed

  • Question

  • Hi everybody,

    I am using the ASP.NET Core IDistributed Cache Interface with SQL Server 2016, as explained in the official documentation.

    I have implemented an ASP.NET Core Web API, in which Startup.cs file I have this configuration for the DistributedSqlServerCache service, note the ApplicationIntent=ReadOnly attribute in the connection string.  I have added this attribute because the company DBA implemented the cache database in an Always On Availability Group in order to have our cache database in a sort of load balancing:

    public void ConfigureServices(IServiceCollection services)
            {
                // Add framework services.
                services.AddMvc();
                services.AddDistributedSqlServerCache(options =>
                {
                    options.ConnectionString = @"Data Source=ipAddress,1433;Initial Catalog=DatabaseName;User Id=userId;Password=userPwd;ApplicationIntent=ReadOnly;";
                    options.SchemaName = "dbo";
                    options.TableName = "results";
                });
            }

    In the Web API controller, I have a GET endpoint in which I read a value stored in the distributed cache table, using the IDistributedCache Get method:

    namespace SQLServerDistributedCache.Controllers
    {
        [Produces("application/json")]
        [Route("api/Resultados")]
        public class ResultadosController : Controller
        {
            IDistributedCache _memoryCache;
            public ResultadosController(IDistributedCache distributedCache)
            {
                _memoryCache = distributedCache;
            }
            // GET: api/Resultados
            [HttpGet]
            public string Get()
            {
                return Encoding.UTF8.GetString(_memoryCache.Get("myStoredValue"));
            }

    The problem is:

    If I remove the ApplicationIntent=ReadOnly from the connection string, the endpoint works fine, and I get the value retrieved from the table.  But, when I place again the ApplicationIntent=ReadOnly attribute in the connection string, I start receiving this exception in the IDistributedCache.Get() method call:  
    'System.Data.SqlClient.SqlException: 'Failed to update database "databaseName" because the database is read-only.''
    See exception in the image below.
    My questions are:
    1. Why is it saying 'Failed to update database' if the method I am calling is Get() (a read operation)?
    2. What is then the connection string I should use in order to execute a read operation against a Distributed Cache running on a SQL Server 2016 Always On Availability Group setup?
    I'd appreciate any help, thank you very much!
    Failed to update
     




    Friday, September 22, 2017 5:12 PM

All replies

  • Hi Carolina Cordero,

    Thank you for posting here.

    According to your question is more related to ASP.NET Core, you could post a new thread in ASP.NET Core forum for suitable support.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework, including Managed Extensibility Framework (MEF), Charting Controls, CardSpace, Windows Identity Foundation (WIF), Point of Sale (POS), Transactions. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 25, 2017 6:15 AM