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>- Why is it saying 'Failed to update database' if the method I am calling is Get() (a read operation)?
- 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?