2014-06-29 138 views
0

我在.NET通用應用程序中使用瞭解析API。我已經將Parse庫添加到PCL中,然後將.NET WinRT庫與實際的Windows RT項目一起使用。以下是我的服務電話。遠程名稱無法解析:'api.parse.com'

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())); 
} 

我找回所有的從解析記錄(只有1個大氣壓)。

當我在Windows App Store應用程序中使用相同的方法時,我得到一個The remote name could not be resolved: 'api.parse.com'異常。目前,我在頁面加載事件中調用它。事件處理程序在我的視圖模型中調用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服務時發生異常。

編輯

只是爲了澄清,我有引用通用PCL庫庫和通用PCL服務庫中的一WinRT的項目。立即調用await query.FindAsync();失敗。我的單元測試是一個標準的Windows單元測試項目,並引用完全相同的兩個PCL庫。兩者之間的唯一區別是一個是WinRT,另一個是標準的Windows單元測試。兩者都使用相同的庫,並使用異步進行調用。這兩個庫都針對Windows 8,Windows Phone 8和.NET 4.5

是否有特殊的權限,我必須讓我的應用程序允許在WinRT設備上訪問網絡?

任何幫助將是真棒!

+0

你是否有任何機會背後的代理? –

+0

這很明顯,您的應用程序無法解析api.parse.com名稱。正如@YuvalItzchakov所說,這可能是一個代理問題。您可能需要使用Fiddler來查看您的請求/響應。 –

+0

我沒有通過代理。單元測試通過我提到的,但應用程序失敗。兩者都在同一臺機器上,在同一個解決方案中。所以我不確定爲什麼應用程序無法解決它。 –

回答

2

我終於可以解決這個問題了。您必須修改Package.appxmanifest文件以告訴設備應用程序需要出站Internet權限。這解釋了爲什麼單元測試工作,應用程序沒有。

只要我在應用程序上啓用了互聯網權限,網絡調用就成功了,並且我從Parse中返回了數據。我希望框架會拋出一個InternetCapabilityNotSet異常或其他東西。除了沒有設置正確的功能之外,還有其他什麼。

相關問題