首页 > 解决方案 > 从 Web 浏览器调用时 Azure 应用服务无法连接到 Azure Redis

问题描述

我有一个使用 Asp.net 核心并托管在 Azure App Service 上的 REST API 服务应用程序。我将 Azure Redis 添加为缓存机制。

我使用本地 redis 在本地机器上测试了该应用程序,它运行良好。我将 Web 服务部署到 Azure App Service,并对其进行了测试。

当我尝试使用 Postman 测试服务时,它工作正常,它正在填充缓存并从缓存中读取。

但是当我运行前端应用程序时,它是一个调用后端服务的 JavaScript 单页应用程序。

我不是从前端查询 Redis。但来自后端.net 应用程序。

对服务的调用失败并出现此错误

Timeout performing EVAL, inst: 1, 
clientName: <.....>, serverEndpoint: Unspecified/my-redis.redis.cache.windows.net:6380, 
keyHashSlot: 15126 (Please take a look at this article for some common client-side issues that can cause timeouts: http://stackexchange.github.io/StackExchange.Redis/Timeouts)

正如我所说,我正在使用相同的参数从 Postman 调用相同的 EndPoint(这是我的 App 服务),并且它正在工作。但从浏览器,它不工作

这是我的配置和代码:

在 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  ...
   services.AddDistributedRedisCache(options => {
                options.Configuration = Configuration.GetConnectionString("AzureCache");
                options.InstanceName = "master";
                });
}

并在控制器(ProductController)中

using Microsoft.Extensions.Caching.Distributed;
public class ProductController: ControllerBase
{
   IDistributedCache _cashe;
   IDataRepository _repo;
   public ProductController (IDistributedCache cache, IDataRepository repo)
   {
       _cache = cache;
       _repo = repo;
   }

   [HttpGet]
   public Async Task<IActionResult> GetProductions([FormBody] DataRequest request)
   {
      string data = _cache.GetString(request.AsKey());
      if (data == null)
      {
           data = _repo.getData(request);
           _cache.SetString(request.AsKey());
      }
      return Ok(data);
   }
}

}

客户端代码如下:

const request = axios.post('https://mydata.myserver.azure.com', reqBody, headers);
request.then(res => {
    .... process the data
});

PS:错误提到了网上的一篇文章。我读了这篇文章,没有跳出来。我所有的服务的大小都小于 5k,除了一个在 250k 到 300k 之间,而且所有呼叫对我来说都失败了。

标签: azureasp.net-coreredisazure-web-app-serviceazure-redis-cache

解决方案


错误本身描述“请查看本文以了解一些常见的客户端问题”。你能分享你打电话的客户端代码吗?


推荐阅读