将 Windows Phone 8.1 应用商店应用连接到现有的 Azure SQL 数据库
本文关键字:应用 Azure 数据库 SQL Phone Windows 连接 | 更新日期: 2023-09-27 17:55:50
我正在编写一个Windows Phone 8.1商店应用程序来帮助管理我的小镇的公共交通,作为我的大学的一个项目。
教授要求我创建这个实现 Web 服务和 SQL 数据库的应用程序。
经过一番研究,我决定将Windows Azure与SQL数据库一起使用。 数据库仅由一个简单的表组成,该表保存公共汽车站(每个表都有一个ID,名称,纬度和经度)。
如果我执行查询,我对结果没有问题。 但是,问题开始了,然后我尝试将我的项目连接到Web服务和关联的数据库。
我按照Microsoft教程中的建议在项目中创建了che客户端,如下所示:
public static Microsoft.WindowsAzure.MobileServices.MobileServiceClient RapalloBusClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient(
"https://<mywebservicename>.azure-mobile.net/",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
这样做 Visual Studio 为我的解决方案添加了另一个名为 <mywebservicename>MobileService
的项目,该项目在 DataObjects 文件夹中有一个类 Todoitem.cs在 Controllers 文件夹中有一个类.cs在 Controllers 文件夹中有一个 TodoitemController。
然后,我为我的解决方案修改了这些类。
现在为了测试我是否做得很好,我尝试在数据库的所有元素的ListBox
中打印(实际上只有一个:一个 Id=1、名称和坐标的公共汽车站)。
我使用的代码是(fermate=意大利语中的公共汽车站):
MobileServiceInvalidOperationException exception = null;
try
{
fermate = await fermateTable.ToCollectionAsync();
}
catch (MobileServiceInvalidOperationException ex)
{
exception = ex;
}
if (exception != null)
{
await new MessageDialog(exception.Message, "Error loading items").ShowAsync();
}
else
{
lstBox.ItemsSource = fermate;
}
此代码正在生成错误("内部服务器错误"或"未找到"),我无法找出错误所在。
我正在使用以下链接中的教程 https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-windows-universal-dotnet-get-started-data/,并且附上了解决方案,我没有任何问题
编辑:
我使用Visual Studio中<mywebservicename>MobileServiceMobile
项目的设置menù将移动服务连接到数据库(在"Pubblicazione/Creazione pacchetto SQL"部分(意大利语)中,我选中了"从现有数据库中提取数据/架构"的选项,提供来自Azure的数据库URL)。
我删除了断点,让应用程序运行,但是当我执行以下行时,我在异常中运行:
fermate = await fermateTable.ToCollectionAsync();
说明如下:
Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException non è stata gestita dal codice utente
HResult=-2146233079
Message=The request could not be completed. (Internal Server Error)
Source=Microsoft.WindowsAzure.Mobile
StackTrace:
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<ThrowInvalidResponse>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<SendRequestAsync>d__1d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceHttpClient.<RequestAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<ReadAsync>d__f.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceTable.<ReadAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.<Execute>d__8`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.Query.MobileServiceTableQueryProvider.<Execute>d__3`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceCollection`2.<ProcessQueryAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceCollection`2.<LoadMoreItemsAsync>d__c.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.WindowsAzure.MobileServices.MobileServiceCollectionExtensions.<ToCollectionAsync>d__0`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at AppBus.CustomImagePage.<AppBarButton_Click>d__1e.MoveNext()
InnerException:
我怀疑这是服务器上的实体框架问题。如果架构与预期不匹配,服务器将抛出数据操作,等等。您是否修改了与表关联的数据库上下文?
您需要确保服务器具有数据库架构的模型(修改后的 TodoItem.cs),并且实体框架能够适当地解析它。请参阅有关使用现有 SQL 数据库的教程:https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/
为了帮助隔离此问题,您可以做的一件事是尝试在没有客户端的情况下访问终结点。如果你使用 Fiddler/Postman 或类似方法,你可以在/tables/fermate 上尝试 HTTP GET 或 POST,看看 fermate TableController 代码是否适用于你的实际数据库。还可以使用服务根页上的"立即试用"链接来发送测试调用。如果提示输入密码,这通常是您的应用程序密钥(无用户名)。