.net core 2.0 webapi和mysql db-dotnet publish-运行.dll时连接字符串为空

kxkpmulp  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(382)

寻找对这个问题的一些见解。对我来说,看起来所有的配置都符合预期,但是每当我尝试运行 dotnet publish TestAPI.dll 并试图击中一个端点,我看到以下内容:
argumentnullexception:值不能为null。参数名称:connectionstring microsoft.entityframeworkcore.utilities.check.NoteEmpty(字符串值,字符串参数名称)microsoft.entityframeworkcore.infrastructure.relationaloptionsextension.withconnectionstring(字符串连接字符串)microsoft.entityframeworkcore.mysqldbcontextoptionsextensions.usemysql(dbcontextoptionsbuilder选项生成器,字符串连接字符串,action mysqloptionsaction)testapi.startup.b_uu4_0(dbcontextoptionsbuilder options)在startup.cs+options.usemysql(configuration.getconnectionstring(“defaultconnection”));microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions+<>c\u displayclass0\u 0.b\u 0(iserviceprovider p,dbcontextoptionsbuilder b)microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions.dbcontextoptionsfactory(iserviceprovider applicationserviceprovider,操作选项action)microsoft.extensions.dependencyinjection.entityframeworkservicecollectionextensions+<>c\u displayclass5\u 0.b\u 0(iserviceprovider p)microsoft.extensions.dependencyinjection.servicelookup.callsiteruntimeresolver.visitfactory(factorycallsite factorycallsite,serviceprovider)microsoft.extensions.dependencyinjection.servicelookup.callsitevisitor.visitcallsite(iservicecallsite callsite,Target参数)
当我从ide(visualstudioformac)运行应用程序时,我可以确认它是否按预期工作。以下是我的相关配置:
appsettings.json文件

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;userid=root;pwd=root;port=3306;database=Expenses;sslmode=none;"
  },
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    //"Console": {
    //  "LogLevel": {
    //    "Default": "Warning"
    //  }
    //}
  }
}

appsettings.development.json文件

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;userid=root;pwd=root;port=3306;database=Expenses;sslmode=none;"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

启动.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TestAPI.Data;
using TestAPI.Data.Models;
using System;
using Microsoft.Extensions.Logging;

namespace TestAPI
{
    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.AddCors();

            services.AddMvc();

            services.AddDbContext<ExpensesDbContext>(options =>
                                                    options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
            services.AddTransient<IBaseDa<Accounts>, AccountsDataAccess>();
            services.AddTransient<IExpensesDa, ExpensesDa>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            env.EnvironmentName = "Development";

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors(builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());

            app.UseMvc();
        }
    }
}

程序.cs

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace TestAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //var builder = new ContainerBuilder();

            //// register types here for DI
            //builder.RegisterType<AccountsDataAccess>().As<IBaseDa<Accounts>>();

            //_container = builder.Build();

            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }
}

甚至尝试在相应的发布文件夹(例如bin/release/netcoreapp2.0/publish)中编辑appsettings.development.json和appsettings.json
非常感谢您的帮助!谢谢大家

t30tvxxf

t30tvxxf1#

试试这个,如果不起作用让我知道。
启动.cs

namespace TestAPI
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
                Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        .
        .
        .
        .
        .

    }
}

相关问题