【问题标题】:Web-api and Kestrel get a null connection when accessing local MySql serverWeb-api 和 Kestrel 在访问本地 MySql 服务器时获得空连接
【发布时间】:2019-03-08 23:10:54
【问题描述】:

在 Centos box 上运行 DotNet-Core 2.2 Web-Api 作为服务

我的配置服务方法中的值不能为空

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<nameOfContextContext>(options => options.UseMySQL(Configuration.GetConnectionString("DevDatabase")));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.Configure<ForwardedHeadersOptions>(options =>
        {
            options.ForwardedHeaders =
                ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        });

    }

值不能为 null 在 AddDbContext 行

这是我的 appsettings.json

{  "ConnectionStrings": {
"DevDatabase": "Server=127.0.0.1;Port=3306;Database=nameOfDatabse;Uid=svcUser;Pwd=Password;"}, "Logging": {
"LogLevel": {
  "Default": "Warning"}   },"AllowedHosts": "*"}

我试图修正格式希望你能阅读。

不管怎样,服务在不使用数据库时运行良好,但当我尝试连接到数据库时,我得到 Value cannot be null

请帮忙

【问题讨论】:

  • "Null 对象是 AddDbContext 行",什么?您可以重写它以使其成为正确的陈述吗? “空对象”是矛盾的,要么是空,要么是对象
  • 对不起,我会再次运行它并得到确切的错误
  • 值不能为空

标签: c# mysql .net-core centos asp.net-core-webapi


【解决方案1】:

我的 appsettings.json 是空的。我相信这是一个路径错误,我正在运行哪个 dll,但事实是。

        services.AddDbContext<nameOfContextContext>(options => options.UseMySQL(add database connection string here));

还有中提琴,我们有一个赢家。我在想连接字符串是错误的,但它只是红隼找不到配置文件..

请记住,这可能对生产不安全。

我打算稍后控制数据库访问的凭据

【讨论】:

    猜你喜欢
    • 2011-04-22
    • 2013-10-08
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2014-09-03
    • 2015-11-13
    • 2016-11-15
    相关资源
    最近更新 更多