【问题标题】:ASP.NET Core Web Api publishing to IIS producing an Sql Server connection errorASP.NET Core Web Api 发布到 IIS 产生 Sql Server 连接错误
【发布时间】:2019-05-12 08:08:45
【问题描述】:

我在 asp.net core 2.0 中创建了一个简单的 web api,它使用 localdb 作为后备数据存储。它在调试中完全按预期工作 - 我能够从邮递员(或浏览器)发送请求并逐步执行我的代码。这是我的连接字符串:

"Server=(localdb)\\MSSQLLocalDB;Database=UniversityDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"

我现在希望它持续运行,以便我可以从 Xamarin.Forms 项目中使用它,因此我尝试使用 Visual Studio 向导发布 Web 应用程序。

1.

2.

UniversityApi 然后出现在默认网站下的 IIS 中,但不是作为应用程序。我选择“转换为应用程序”,这会显示这个对话框:

3.

NetCoreApps 应用程序池不使用托管代码。

错误:

{
"error": [
    "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.\r\n)"
]}

该错误似乎描述了连接到我的 Sql Server 实例的问题,这很奇怪,因为它在 VS 中调试时运行良好。有什么想法吗?

【问题讨论】:

    标签: sql-server iis asp.net-web-api asp.net-core


    【解决方案1】:

    非常感谢您的回答。我有类似的问题。我的 .net core web api 托管在 AWS 中。当我只是通过 IP 地址(示例https://xx.xx.xxxx/swagger)访问 api 时它可以工作,但是当我将它集成到域(https://xx.sampledomain.com/swagger)时,我遇到了同样的问题。这两个步骤解决了错误。

    1. 1st 我在 IIS 应用程序池中使用了 LocalService 帐户,然后在 MS SQL Server 2016 中添加了 NT Authority/Local Service 帐户。

    enter image description here

    1. 在发布屏幕 > 数据库 > DefaultConnection 上,我勾选了“在运行时使用此连接字符串”复选框。

    enter image description here

    【讨论】:

      【解决方案2】:
      1. 将“LOCAL SERVICE”(Windows 帐户)添加到 SQL Server。
      2. 在(IIS 应用程序池高级设置)中从“ApplicationPoolIdentity”更改为“LocalService”

      注意。添加“本地服务”(Windows 帐户)需要 SQL Server 的权限。

      【讨论】:

        【解决方案3】:

        搜索了一段时间后,我偶然发现了这个问题:

        SQL won't connect after deploying

        其中一个答案对我有用,感谢 Robotron。

        1. IIS 管理器
        2. 应用程序池
        3. 找到您的应用所属的池(对我来说是 .NET v4.5)
        4. 右键->高级设置
        5. 向下滚动到身份
        6. 从任何东西(对我来说是 ApplicationPoolIdentity,和你一样)更改为 LocalSystem。

        【讨论】:

        • 这对我有帮助,但后来出现错误“无法打开登录请求的数据库。登录失败。用户“NT AUTHORITY\SYSTEM”登录失败,并且有一条关于“正在应用现有的ApplicationDbContext 的迁移可能会解决此问题"
        猜你喜欢
        • 2021-01-11
        • 2019-12-28
        • 1970-01-01
        • 2018-10-26
        • 1970-01-01
        • 2021-06-01
        • 2019-02-05
        • 1970-01-01
        • 2016-07-27
        相关资源
        最近更新 更多