Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

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

$
0
0

Hi everybody,

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

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"));
        }
</div> <div>

The problem is:

</div> <div>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:  </div> <div>'System.Data.SqlClient.SqlException: 'Failed to update database "databaseName" because the database is read-only.''</div><div>See exception in the image below.</div> <div></div> <div>My questions are:</div>
  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?
<div>I'd appreciate any help, thank you very much!</div> <div> <div></div> <div></div> </div> <div>Failed to update</div><div> <div></div> <div></div> </div> <div> </div>




Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>