gpt4 book ai didi

c# - System.ArgumentNullException:值不能为null。 (参数 'connectionString')asp.net Core Docker-compose

转载 作者:行者123 更新时间:2023-12-02 19:18:29 27 4
gpt4 key购买 nike

我有一个dockerized asp.net Core应用程序试图连接到mySql数据库。两者都在docker-compose内部运行。当我在没有Docker的情况下测试到本地数据库的连接时,我的代码工作正常,但是当我将其部署在docker-compose内的Vm上并且调用了我的一个 Controller 时,出现此错误:System.ArgumentNullException:值无法为空。 (参数'connectionString')。
这是我的docker-compose:

version: '3'
services:
dbgil:
container_name: dbgil
image: mysql
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: root
volumes:
- dbdata:/var/lib/mysql
webserver:
depends_on:
- dbgil
image: lionelquirynen/gillesquirynensys:latest
ports:
- "8021:80"
links:
- dbgil
volumes:
dbdata:

这是我在asp.net核心应用程序中的启动:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GillesQuirynenSys.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace GillesQuirynenSys
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<MySqlDbContext>(options =>
options.UseMySql("server=localhost;port=3306;database=test;user=root;password=root;convert zero datetime=True"));
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MySqlDbContext context)
{
context.Database.EnsureCreated();
var databaseCreator = context.GetService<IRelationalDatabaseCreator>();
databaseCreator.CreateTables();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}



我首先在我的appsettings.json文件中有连接字符串,但是却遇到了同样的错误,因此我尝试对其进行硬编码,但没有任何改变。有任何想法吗?看来我的配置工作正常(至少,它在本地没有Docker)。

PS:这是我的asp.net Core应用程序的DockerFile,然后将其推送到DockerHub:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80

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

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

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

最佳答案

我很高兴我的回答有所帮助。
因此,基本上在docker容器中,MySQL db的服务器名称与docker-compose.yml文件中声明的服务名称相同。

请注意您的代码:
代替这个:

        public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<MySqlDbContext>(options =>
options.UseMySql("server=localhost;port=3306;database=test;user=root;password=root;convert zero datetime=True"));
}

->您在哪里硬编码您的连接字符串

试试这个:
      public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<MySqlDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));
}

并调整 appsettings.json文件-对于生产(docker容器)构建,您将具有:
{
"ConnectionStrings": {
"DefaultConnection": "server=dbgil;port=3306;database=test;user=root;password=root;convert zero datetime=True"
}
...
}


appsettings.Development.json中,您将有一个用于 Development模式的连接字符串(您的本地主机):
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=3306;database=test;user=root;password=root;convert zero datetime=True"
}
...
}

关于c# - System.ArgumentNullException:值不能为null。 (参数 'connectionString')asp.net Core Docker-compose,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61833736/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com