社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Redis

如何将ASP.NET核心应用程序和Redis对接在一起?

Melianessa • 5 年前 • 731 次点击  

我正在和一个Docker合作,尝试连接Redis和Web应用程序。这是我的Docker撰写文件:

version: '3.7'

services:  
  web:
    image: redistest:latest
    depends_on:
      - "redis_image"
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "9901:80"

  redis_image:
    image: redis
    container_name: cache
    ports:
      - "6379:6379"

这是我的连接字符串:

"ConnectionStrings": {
    "redis": "localhost:6379,abortConnect=False"
  },

Dockerfile如下:

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app
EXPOSE 80

COPY *.csproj ./
RUN dotnet restore RedisTest.csproj

COPY . ./
RUN dotnet publish RedisTest.csproj -c Release -o out

FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "RedisTest.dll"]

在startup.cs中连接到redis:

services.AddDistributedRedisCache(option =>
{
    option.Configuration = Configuration.GetConnectionString("redis");
}); 

要连接到Docker,我在命令中写入了如下命令:

docker-compose build
docker-compose up

这个命令工作得很好,但是在我转到“localhost:9901”之后,我在控制台中收到下一个错误

web_1          | fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
web_1          |       An unhandled exception has occurred while executing the request.
web_1          | StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: EVAL; SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago ---> StackExchange.Redis.RedisConnectionException: SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago
web_1          |    --- End of inner exception stack trace ---

web_1          | fail: Microsoft.AspNetCore.Server.Kestrel[13]
web_1          |       Connection id "0HLKGR00VUQLM", Request id "0HLKGR00VUQLM:00000001": An unhandled exception was thrown by the application.
web_1          | StackExchange.Redis.RedisConnectionException: No connection is available to service this operation: EVAL; SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago ---> StackExchange.Redis.RedisConnectionException: SocketFailure on localhost:6379/Subscription, origin: Error, input-buffer: 0, outstanding: 0, last-read: 0s ago, last-write: 0s ago, unanswered-write: 1291s ago, keep-alive: 60s, pending: 0, state: Connecting, last-heartbeat: never, last-mbeat: -1s ago, global: 0s ago
web_1          |    --- End of inner exception stack trace ---

我如何解决这个问题?我不知道如何强制ASP.NET核心应用程序参见Docker中的Redis。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30825
 
731 次点击  
文章 [ 1 ]  |  最新文章 5 年前