2016-06-19 22 views
0

在下面的MSDN文章更新的UWP應用程序在Windows 10, https://msdn.microsoft.com/en-us/windows/uwp/networking/network-communications-in-the-background 我們可以看到: 「不像其他的網絡傳輸,HttpClient的對象不能直接傳遞到的的UsingTransport方法ControlChannelTrigger對象,而是HttpRequestMessage對象必須專門構造用於HttpClient對象和ControlChannelTrigger。HttpRequestMessage對象是使用RtcRequestFactory.Create方法創建的。UWP,ControlChannelTrigger與該RtcRequestFactory類

當我使用RtcRequestFactory.Create方法在UWP應用程序,我得到下面的錯誤: 「System.PlatformNotSupportedException:操作不支持此平臺上 在System.Net.Http.RtcRequestFactory.Create(列舉HTTPMethod方法,Uri uri)「

當看到https://msdn.microsoft.com/fr-fr/library/dn600634(v=vs.110).aspx時,提到」System.Net.Http.RtcRequestFactory類在.NET Native中不受支持「。 我的理解是,UWP應用程序必須使用.NET Native。

所以它看起來像使用RtcRequestFactory不是一個UWP應用程序的選項。 在後臺進行網絡通信有什麼選擇?

回答

0

根據你的描述,你要使用ControlChannelTriggerHttpClient的實現在後臺的網絡通信。

有兩種HttpClient的的API實現在託管UWP應用程序中的HTTP客戶端角色:System.Net.Http.HttpClientWindows.Web.Http.HttpClient。根據this article,它表示System.Net.Http命名空間可能在將來的Windows版本中不可用,供Windows應用商店應用使用。從Windows 8.1和Windows Server 2012 R2開始,請在Windows.Web.Http名稱空間中使用Windows.Web.Http.HttpClient

要使用Windows.Web.Http.HttpClient使用ControlChannelTrigger,還需要一個HttpRequestMessage對象,但它可以在不RtcRequestFactory.Create方法來創建。

您可以找到​​實施(項目HttpClientTransportHelper - >類CommunicationModule - >方法SetUpHttpRequestAndSendToHttpServer和SendHttpRequest),它展示瞭如何在新的Windows.Web.Http命名空間中的ControlChannelTrigger類HttpClient的。注意:這是Windows 8.1示例,但它可以輕鬆地在UWP中重用。

HttpRequestMessage httpRequest= new HttpRequestMessage(HttpMethod.Get, serverUri); 
channel.UsingTransport(httpRequest);