【问题标题】:Strange error in Asp.Net Core 5.0 on Linux "Value cannot be null. (Parameter 'connectionString')"Linux 上 Asp.Net Core 5.0 中的奇怪错误“值不能为空。(参数'connectionString')”
【发布时间】:2021-11-18 10:45:37
【问题描述】:

我因该错误一周后被阻止。

“应用程序抛出了一个未处理的异常。 System.ArgumentNullException:值不能为空。 (参数'connectionString')"

我在 Linux 上的 Apache 中的 Asp.Net Core 中部署了一个应用程序。

我可以访问这些页面,但是当它需要调用数据库时,它给了我这个错误。

当我在 Windows 上本地使用它时,一切正常,我可以使用相同的 connectionString 访问数据库。

我不明白为什么在我构建项目并部署它时它不起作用。

谁能帮帮我。

这是我的连接字符串:

{
"ConnectionStrings": {
    "DefaultConnection": "Data Source=IP SERVER;Initial Catalog=Database;User ID=SA;Password=Password"
},   

我已经在 Azure 上部署了该应用程序,并在 Azure 上使用了 SQL 数据库,也许是因为这个原因?

【问题讨论】:

    标签: asp.net linux database apache deployment


    【解决方案1】:

    由于您使用的是 net core,连接字符串保存在 appsettings.json 文件中。默认的 JsonConfigurationProvider 按以下顺序加载配置:

    1. appsettings.json

    2. appsettings.Environment.json 。例如,appsettings.Production.json 和 appsettings.Development.json 文件。

    appsettings.Environment.json 值覆盖 appsettings.json 中的键:

    在开发中,appsettings.Development.json 配置会覆盖 appsettings.json 中的值。

    在生产中,appsettings.Production.json 配置会覆盖 appsettings.json 中的值。例如,将应用程序部署到 Azure 或任何其他服务器时。

    所以你必须检查 appsettings.Production.json 文件。也许它不包含连接字符串,并且由于覆盖了您的开发文件,您可能会遇到错误。

    更新

    如果您使用 MS SQl 服务器,您的连接字符串应该如下所示,但我认为这不是问题。问题可能是如果您使用不同的名称,而不是“DefaultConnection”

    "DefaultConnection": "Data Source=xx.182.xx.xxx;Initial catalog=XXXX;Integrated Security=False;User ID=user;Password=passwod;"
    

    【讨论】:

    • 感谢您的回答!我检查了你告诉我的文件,我没有找到“appsetings.Production.json”我发布后仍然只有一个appsettings.Development.json和appsetings.json
    • @Edenn 检查 appsettings 并找到连接字符串。如果没问题,您可以复制一份appsetiings并将其重命名为appsetings.Production.json”。之后您必须重新启动网站。
    • 我做到了,但它仍然给我同样的错误......我真的不明白为什么,但谢谢你的帮助!
    • @Eq 你在那里找到连接字符串了吗?它与您的应用程序中的名称相同吗?
    • 是的,我在本地运行时具有相同配置的完全相同的文件...我在想是不是因为我已经在 Azure 上部署了该应用程序,并使用了 Azure 的数据库?也许它仍在等待来自 azure 的 connectionString...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2020-11-19
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多