默认 Azure 移动应用后端返回 500

Default Azure Mobile App backend returning 500

我正在尝试使用 Azure 移动应用程序和 Azure 存储为我的 Xamarin.Forms 应用程序构建后端。我在 Azure 中创建了所有内容(移动服务、存储帐户、ConnectionString 等)并下载了后端(因为我选择 C# 作为语言)和 Xamarin.Forms 应用程序的默认项目。

然后我在本地调试了后台的项目,可以运行,所以发布了。当我使用默认 Xamarin.Forms 应用程序测试 Web API 时,服务器返回了一个异常(500 内部服务器异常)和消息 "An error has occured."。之后我尝试启用服务器日志、远程调试和 xml 日志文件,但我没有找到太多。

远程调试显示"visible"后端代码没有问题(调试时没有抛出异常)。 xml 日志文件在 OWIN 方法中显示警告,但没有错误。 服务器日志显示 GET 请求中有两个错误: azure-log.txt
观察。我删除了我的应用服务名称并替换为“{servicename}”。

我已经尝试删除并重新创建所有 Azure 服务,已经尝试将 http 更改为 https 并尝试将 connectionString 更改为其他值。但是我只是在尝试调试默认项目但没有成功后才进行这些更改。

我已经尝试解决这个错误一段时间了,但我不知道还能做什么。
感谢您的帮助!

Exception=System.Data.SqlClient.SqlException (0x80131904): 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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.) ---> System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Data.ProviderBase.DbConnectionPool.TryGetConnection

根据您提供的错误,我假设您在 Azure 移动后端使用本地数据库。您可以利用 KUDU or Azure App Service Editor 检查 web.config 文件下的 MS_TableConnectionString 连接字符串。连接字符串如下所示:

<add name="MS_TableConnectionString" connectionString="Server=tcp:{your_dbservername}.database.windows.net,1433;Initial Catalog={your_dbname};Persist Security Info=False;User ID={your_username};Password={your_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>

当通过 Visual Studio 将您的移动应用程序部署到 Azure 时,您需要将连接字符串指向您的 Azure 数据库,如下所示:

有关创建移动应用并将其部署到 Azure 应用服务的更多详细信息,您可以参考此 document. Also, you could leverage "All Settings > Application settings" under your web app blade to configure your connectionstring, which could override your existing connectionstring in your web.config file at runtime. For more details, you could follow this official tutorial

此外,您可以关注 Adrian Hall's book here 以更好地理解和快速开始使用 Azure 移动应用程序。

发生错误是因为 Azure 为 back-end 创建的默认项目旨在与 SQL 一起使用。由于我将 Azure 存储用作数据库,因此我不得不修改此项目。

Adrian Hall 的书的第 3 章 (NoSQL Storage with the StorageDomainManager) 详细解释了如何做到这一点。