c# - Web-api和Kestrel访问本地MySql服务器时得到空连接

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

在 Centos 盒子上将 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;
        });

    }

AddDbContext 行的值不能为 null

这是我的 appsettings.json

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

我尝试修复格式希望你能阅读。

无论如何,该服务在不使用数据库时运行良好,但一旦我尝试连接到数据库,我就会得到“值不能为空”

请帮忙

最佳答案

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

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

和中提琴我们有赢家。我以为连接字符串错误,但只是 kestrel 找不到配置文件..

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

我计划稍后控制数据库访问的凭据

关于c# - Web-api和Kestrel访问本地MySql服务器时得到空连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072264/

相关文章:

c# - 将 log4net 包含到使用 .net 4.0 构建的 .net Web 应用程序中

c# - 带列的 TreeView

mysql - 无法在 MySQL 中创建/写入文件

mysql order by 子句不适用于我的查询

c# - 如何从 Type 的名称中获取 Type 对象?

c# - Unity C#在运行时添加歌曲,然后播放该歌曲

php - 运行查询后,操作成功的消息

c# - NLog 连接字符串的 appsettings.json 中的引用变量

angular - 将 IFormFile 附加到邮件而不保存文件

c# - 带有 .Net Core 2.1 的 HttpClient 挂起