我在.NET Universal应用程序中使用Parse API。我已将Parse库添加到PCL,然后将.NET WinRT库与实际的Windows RT项目一起包含在内。以下是我的服务电话。
public async Task<IEnumerable<BasicTask>> GetTasksAsync()
{
ParseQuery<ParseObject> query = ParseObject.GetQuery(typeof(BasicTask).Name);
IEnumerable<ParseObject> parseObjects = await query.FindAsync();
// Convert the parse objects returned from the cloud into our local model representation.
// We do this in order order to maintain support for the model across multiple cloud services
// instead of restricting ourselves to the entire app using ParseCloud only.
return parseObjects.Select(po => this.ConvertParseObjectToDutyTask(po));
}
此方法适用于以下单元测试:
[TestMethod]
public async Task TaskService_GetTasks_HasValue()
{
var service = new ParseCloudTaskService();
var tasks = await service.GetTasksAsync();
Debug.WriteLine(string.Format("{0} tasks found.", tasks.Count()));
}
我从Parse收回所有记录(仅1个大气压)。
当我在Windows App Store应用程序中使用相同的方法时,出现The remote name could not be resolved: 'api.parse.com'
异常。目前,我在我的Page load事件中调用它。事件处理程序在我的视图模型中调用Intialize方法,该方法向存储库请求所有任务。存储库从服务中获取任务并将其返回。
网页
private async void pageRoot_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
if (!(this.DataContext is MainPageViewModel))
{
return;
}
var vm = this.DataContext as MainPageViewModel;
await vm.Initialize();
this.TasksList.ItemsSource = vm.Tasks;
}
查看模型
public async Task Initialize()
{
try
{
IEnumerable<BasicTask> tasks = await this.repository.GetTasksAsync();
this.tasks = new ObservableCollection<BasicTask>(tasks);
}
catch (Exception)
{
throw;
}
}
存储库
public async Task<IEnumerable<BasicTask>> GetTasksAsync()
{
IEnumerable<BasicTask> tasks = await this.service.GetTasksAsync();
return tasks;
}
当事件执行时,当serivce调用await query.FindAsnyc()
时会抛出异常。
以下是完整的例外情况。
System.Net.Http.HttpRequestException was unhandled by user code
HResult=-2146233088
Message=An error occurred while sending the request.
Source=mscorlib
StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Parse.PlatformHooks.<RequestAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Parse.Internal.InternalExtensions.<>c__DisplayClass7`1.<OnSuccess>b__6(Task t)
at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Parse.Internal.InternalExtensions.<>c__DisplayClass7`1.<OnSuccess>b__6(Task t)
at System.Threading.Tasks.ContinuationResultTaskFromTask`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- 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 Duty.Services.ParseCloud.ParseCloudTaskService.<GetTasksAsync>d__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 Duty.Repositories.Tasks.BasicTaskRepository.<GetTasksAsync>d__0.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 Duty.WindowsAppStore.ViewModels.MainPageViewModel.<Initialize>d__0.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 Duty.WindowsAppStore.Views.MainPage.<pageRoot_Loaded>d__0.MoveNext()
InnerException: System.Net.WebException
HResult=-2146233079
Message=The remote name could not be resolved: 'api.parse.com'
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
InnerException:
有没有人知道为什么它在单元测试中工作正常,但未能在应用程序内点击服务?我使用异步模式错了吗?我不确定这是否会影响它,因为在调用Parse服务时发生异常。
修改
为了澄清,我有一个WinRT项目引用了Universal PCL存储库和通用PCL服务库。一旦调用await query.FindAsync();
,它就会失败。我的单元测试是一个标准的Windows单元测试项目,并引用完全相同的两个PCL库。两者之间的唯一区别是一个是WinRT,一个是标准的Windows单元测试。两者都使用相同的库,并使用异步进行调用。这两个库都针对Windows 8,Windows Phone 8和.NET 4.5
我是否有特殊权限允许我的应用程序允许在WinRT设备上进行网络访问?
任何帮助都会很棒!
答案 0 :(得分:2)
我终于解决了这个问题。您必须修改Package.appxmanifest文件以告知设备该应用程序需要出站Internet权限。这解释了为什么单元测试工作而应用没有。
只要我在应用上启用了互联网权限,网络呼叫就成功了,我从Parse返回了数据。我希望框架会抛出InternetCapabilityNotSet
异常或其他东西。当没有设置正确的功能时,还有其他任何东西。