简体   繁体   English

EF 上下文在 docker 中没有响应

[英]EF context not responding in docker

I just upgraded my Web API application to .NET core 3.0, all works fine when running in debug mode in IIS Express, but context does not respond when running in docker container either on server or in VS debug. I just upgraded my Web API application to .NET core 3.0, all works fine when running in debug mode in IIS Express, but context does not respond when running in docker container either on server or in VS debug. No error thrown, just never responds.没有抛出错误,只是从不响应。

I tried deploying an updated image to server which is where I first noticed the issue.我尝试将更新的图像部署到服务器,这是我第一次注意到问题的地方。 I then tried running as docker in vs to debug.然后我尝试在 vs 中以 docker 的身份运行以进行调试。 I have updated all NuGet packages and set frameworks to .NET Core 3.0 or .NET Standard 2.1.我已更新所有 NuGet 软件包并将框架设置为 .NET Core 3.0 或 .NET Standard 2.1。 I have inspected the context connection string in debug and it appears to be correct.我在调试中检查了上下文连接字符串,它似乎是正确的。 I rolled back to an earlier image using .NET core 2.2 and all worked as expected using the same startup parameters.我使用 .NET 核心 2.2 回滚到较早的映像,并且使用相同的启动参数全部按预期工作。 I created a test method that does not use context and it returns correct values on server and in VS docker debug.我创建了一个不使用上下文的测试方法,它在服务器和 VS docker 调试中返回正确的值。 I tried changing the method to use synchronous call to context, but no change in behavior.我尝试更改方法以使用对上下文的同步调用,但行为没有改变。 The test database is very small, only 3 records in the table being queried.测试数据库很小,被查询的表中只有3条记录。

    public async Task<List<SendingSystemInfoResponse>> getSendingSystemInfoList()
    {
        try
        {
            return await _context.EmailSendingSystem.Where(m => !m.Deleted && m.Active == true).Select(m => new SendingSystemInfoResponse
            {
                SystemId = m.EmailSendingSystemId,
                SystemName = m.Title,
                SystemDescription = m.Description

            }).ToListAsync();
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, string.Format("EmailDataAccess: Exception retrieving EmailSendingSystem List"));
            throw;
        }
    }

If there were an error connecting to the SQL Server, or if the SQL request timed out, I would expect to hit the catch block code, but this never happens.如果连接到 SQL 服务器时出错,或者如果 SQL 请求超时,我预计会遇到 catch 块代码,但这永远不会发生。

Here is the content of the docker file, I'm not sure if my targets for base and build images are correct.这是 docker 文件的内容,我不确定我的基本映像和构建映像的目标是否正确。 They seem to work, but might be the cause of my issue.他们似乎工作,但可能是我的问题的原因。

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["EmailAutomation.API/EmailAutomation.API.csproj", "EmailAutomation.API/"]
COPY ["EmailAutomation.API/NuGet.Config", "/src"]
RUN dotnet restore "EmailAutomation.API/EmailAutomation.API.csproj"
COPY . .
WORKDIR "/src/EmailAutomation.API"
RUN dotnet build "EmailAutomation.API.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "EmailAutomation.API.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "EmailAutomation.API.dll"]

------Update----- So, I've been working on this off and on today, and have a little more info that might help someone point me in the right direction. ------更新----- 所以,我今天断断续续地处理这个问题,并且有更多的信息可以帮助有人指出我正确的方向。 I created a simple test Web API project with an EF connection to SQL Server running on my local PC using .NET core 3.0 and the latest NuGet packages, and enabled TCP/IP connections to SQL Server running locally. I created a simple test Web API project with an EF connection to SQL Server running on my local PC using .NET core 3.0 and the latest NuGet packages, and enabled TCP/IP connections to SQL Server running locally. I was able to get it to connect to the database and return values.我能够让它连接到数据库并返回值。

Next, I created a copy of the test database on my local SQL Server.接下来,我在本地 SQL 服务器上创建了测试数据库的副本。 This also worked, the Web API in the original question running in Docker connected and returned data.这也有效,Docker 中运行的原始问题中的 Web API 连接并返回数据。 I then changed the connection string to point back to the test SQL Server and the process hangs in the same spot with no error.然后我将连接字符串更改为指向测试 SQL 服务器,并且该过程挂在同一位置,没有错误。 Next, I tested it with the connection still pointed to the test SQL Server but running in IIS Express rather than Docker.接下来,我测试了它,连接仍然指向测试 SQL 服务器,但运行在 IIS Express 而不是 Docker 中。 Again, everything worked as expected.同样,一切都按预期工作。

I then tried running the previous release docker image which uses .NET Core 2.2, and it also returned data from the test SQL Server.然后我尝试运行使用 .NET Core 2.2 的先前版本 docker 映像,它还返回了来自测试 SQL 服务器的数据。

What could be the reason that I can not connect via IP to the test SQL Server using .NET Core 3.0 in Docker when all other combinations work just fine. What could be the reason that I can not connect via IP to the test SQL Server using .NET Core 3.0 in Docker when all other combinations work just fine.

------Update 2----- I created the necessary database for my new simple test Web API project on the Test SQL Server, and changed the simple Web API project connection string. ------Update 2----- I created the necessary database for my new simple test Web API project on the Test SQL Server, and changed the simple Web API project connection string. This new, clean, simple, .NET Core 3 project also did not connect to the Test SQL Server when running as Docker, but worked fine when running on IIS Express. This new, clean, simple, .NET Core 3 project also did not connect to the Test SQL Server when running as Docker, but worked fine when running on IIS Express. It also worked fine when running in Docker but connecting to my local DB by IP.在 Docker 中运行但通过 IP 连接到我的本地数据库时,它也可以正常工作。

Something has changed with .NET Core 3 in Docker that is stopping it from connecting to the external database server. Docker 中的 .NET Core 3 发生了一些变化,阻止了它连接到外部数据库服务器。 Anybody have any ideas on what I need to do to resolve this?有人对我需要做些什么来解决这个问题有任何想法吗?

--UPDATE 3 ----- Thanks to MATT, After reading Matt's response, I wasn't able to get the RUN commands to work within the docker file. --UPDATE 3 ----- 感谢 MATT,在阅读了 Matt 的回复后,我无法让 RUN 命令在 docker 文件中工作。 but changing my base image to bionic did work.但是将我的基本图像更改为仿生确实有效。 I have also been working with Microsoft support who also pointed me to the link that Matt provided, Maybe I just didn't place the RUN commands in the right location, so if anyone can provide a sample docker file using the RUN commands to resolve this issue.我也一直在与 Microsoft 支持人员合作,他们还向我指出了 Matt 提供的链接,也许我只是没有将 RUN 命令放在正确的位置,所以如果有人可以使用 RUN 命令提供示例 docker 文件来解决这个问题问题。 I would be grateful: Here is an updated docker file from a simple test project:我将不胜感激:这是来自一个简单测试项目的更新 docker 文件:

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-bionic AS base
    WORKDIR /app
    EXPOSE 80

    FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
    WORKDIR /src
    COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
    RUN dotnet restore "WebApplication1/WebApplication1.csproj"
    COPY . .
    WORKDIR "/src/WebApplication1"
    RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

    FROM build AS publish
    RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish

    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "WebApplication1.dll"]

---Final Update ----- ---最终更新-----

I tried the RUN commands in the docker file again, but got it right this time.我再次尝试了 docker 文件中的 RUN 命令,但这次做对了。 Here is that version of the docker file.这是 docker 文件的那个版本。

    FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
    WORKDIR /app
    RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf
    RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /usr/lib/ssl/openssl.cnf
    EXPOSE 80

    FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
    WORKDIR /src
    COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
    RUN dotnet restore "WebApplication1/WebApplication1.csproj"
    COPY . .
    WORKDIR "/src/WebApplication1"
    RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

    FROM build AS publish
    RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish

    FROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "WebApplication1.dll"]

I believe the issue you're running into is documented here: https://github.com/dotnet/SqlClient/issues/222 .我相信您遇到的问题记录在这里: https://github.com/dotnet/SqlClient/issues/222 As you said, something did change with .NET Core 3 because those Docker images are based on Debian Buster by default.正如您所说,.NET Core 3 确实发生了一些变化,因为这些 Docker 图像默认基于 Debian Buster。 Buster is configured to use 1.2 as the minimum TLS protocol, a change from the previous version (see https://www.debian.org/releases/stable/amd64/release-notes/ch-information.en.html#openssl-defaults ). Buster 配置为使用 1.2 作为最低 TLS 协议,与之前的版本有所不同(参见https://www.debian.org/releases/stable/amd64/release-notes/ch-information.en.html#openssl-默认值)。

This can be temporarily fixed by adding the following to the Dockerfile:这可以通过将以下内容添加到 Dockerfile 来临时修复:

RUN sed -i 's/MinProtocol = TLSv1.2/MinProtocol = TLSv1/g' /etc/ssl/openssl.cnf
RUN sed -i 's/MinProtocol = TLSv1.2/MinProtocol = TLSv1/g' /usr/lib/ssl/openssl.cnf

This is not a great solution because it is essentially downgrading the version of TLS.这不是一个很好的解决方案,因为它本质上是在降级 TLS 的版本。 The better long-term solution is to enable TLS 1.2 on the SQL Server (see https://support.microsoft.com/en-us/help/3135244/tls-1-2-support-for-microsoft-sql-server ).更好的长期解决方案是在 SQL 服务器上启用 TLS 1.2(请参阅https://support.microsoft.com/en-us/help/3135244/tls-1-2-support-for-microsoft-sql-server )。

David Violett did you get this to work as I am having the same issue.大卫维奥莱特你有没有让这个工作,因为我有同样的问题。 Sorry I don't have enough reps to comment.抱歉,我没有足够的代表发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM