【问题标题】:Azure App Service cannot connect to Azure Redis when calling from Web Browser从 Web 浏览器调用时 Azure 应用服务无法连接到 Azure Redis
【发布时间】:2019-12-27 21:05:18
【问题描述】:

我有一个使用 Asp.net 核心并托管在 Azure 应用服务上的 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(这是我的应用服务),并且它正在工作。 但在浏览器中,它不起作用

这是我的配置和代码:

在 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
});

P.S:错误提到了网上的一篇文章。 我读了这篇文章,没有跳出来。我所有的服务都小于 5k,除了一个在 250k 到 300k 之间,而且所有的调用都失败了。

【问题讨论】:

    标签: azure asp.net-core redis azure-web-app-service azure-redis-cache


    【解决方案1】:

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

    【讨论】:

    • 我添加了客户端脚本。我阅读了错误中提到的那篇文章,但没有找到与我相关的任何内容。对于所有服务,我的 json 返回小于 5k,除了 250k 到 300k 的服务。
    猜你喜欢
    • 2021-04-15
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    相关资源
    最近更新 更多