【问题标题】:Asp .NET MVC Core application not running in Windows Docker containerAsp .NET MVC Core 应用程序未在 Windows Docker 容器中运行
【发布时间】:2020-03-29 07:09:06
【问题描述】:

我正在尝试在 docker 容器中部署一个 asp.net core mvc 应用程序,当我运行该应用程序时,我在 docker 日志中收到以下错误

失败:Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware1 执行请求时发生未处理的异常。 System.ArgumentNullException:值不能为空。 参数名称:connectionString

我已经在我的 appsettings.json 文件中提到了连接字符串,但它仍然无法正确获取它。

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "<<< AZURE-SQL CONNECTION STRING >>>"
  }
}

Startup.cs

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.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

            services.AddScoped<IBusinessLogic, BusinessLogic>();
            services.AddScoped<IDataRepository, DataRepository>();

var connection = Configuration.GetConnectionString("DefaultConnection");
            services.AddDbContext<DataContext>(options => options.UseSqlServer(connection));
            services.AddScoped<DataContext>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Mvc}/{action=Design}/{id?}");
            });
        }

已发布文件夹

Dockerfile

FROM microsoft/dotnet:2.1-aspnetcore-runtime
EXPOSE 80

COPY  /publish/ /app

ENTRYPOINT ["dotnet", "MVC_Module_Designer.dll"]

用于发布的 Docker 命令

docker build -t webapp .

docker run -p 8080:80 webapp

我是否错过了在 docker 容器中部署 asp.net mvc 核心应用程序的任何内容?

【问题讨论】:

  • 我相信您的问题是services.AddScoped&lt;DataContext&gt;();。这是不必要的,因为AddDbContext&lt;DataContext&gt; 已经将服务注册为作用域。您实际上是在使用尚未配置的上下文实例覆盖该注册。
  • 在docker之外运行应用是否也会出现同样的错误?

标签: c# docker asp.net-core asp.net-core-mvc


【解决方案1】:

我发现了我所做的问题,它在 Docker 文件中,在将已发布的文件夹复制到 app 目录后,并没有将其更改为那个。因此,它导致了该错误。

正确的 docker 文件

FROM microsoft/dotnet:2.1-aspnetcore-runtime
EXPOSE 80

WORKDIR /app

COPY  /publish .

ENTRYPOINT ["dotnet", "MVC_Module_Designer.dll"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多