UWP,ControlChannelTrigger& RtcRequestFactory类

时间:2016-06-19 15:40:12

标签: c# networking network-programming uwp

在以下针对Windows 10上的UWP应用程序更新的MSDN文章中, https://msdn.microsoft.com/en-us/windows/uwp/networking/network-communications-in-the-background 我们可以读到: "与其他网络传输不同,HttpClient对象不能直接传递给ControlChannelTrigger对象的UsingTransport方法。相反,必须专门构造HttpRequestMessage对象以与HttpClient对象和ControlChannelTrigger一起使用。 HttpRequestMessage对象是使用RtcRequestFactory.Create方法创建的。"

当我在UWP应用程序中使用RtcRequestFactory.Create方法时,我收到以下错误: " System.PlatformNotSupportedException:此平台不支持此操作。    在System.Net.Http.RtcRequestFactory.Create(HttpMethod方法,Uri uri)"

在查看https://msdn.microsoft.com/fr-fr/library/dn600634(v=vs.110).aspx时,提到" .NET Native"中不支持System.Net.Http.RtcRequestFactory类。 我的理解是UWP应用程序必须使用.NET Native。

因此看起来使用RtcRequestFactory不是UWP应用程序的选项。 在后台进行网络通信的替代方案是什么?

1 个答案:

答案 0 :(得分:0)

根据您的说明,您希望将 ControlChannelTrigger HttpClient 一起使用,以在后台实施网络通信。

有两个 HttpClient API,用于在托管的UWP应用中实现HTTP客户端角色:System.Net.Http.HttpClientWindows.Web.Http.HttpClient。它基于this article,表示{®3}}命名空间可能在Windows的未来版本中不可用,以供Windows应用商店应用使用。从Windows 8.1和Windows Server 2012 R2开始,请使用System.Net.Http命名空间中的Windows.Web.Http.HttpClient

要将ControlChannelTrigger与Windows.Web.Http一起使用,还需要一个HttpRequestMessage对象,但可以在不使用 RtcRequestFactory.Create 方法的情况下创建它。

您可以在Windows.Web.Http.HttpClient(项目HttpClientTransportHelper - >类CommunicationModule - >方法SetUpHttpRequestAndSendToHttpServer和SendHttpRequest)中找到实现,其中显示了如何在新的 Windows中将ControlChannelTrigger类与HttpClient一起使用。 Web.Http 命名空间。注意:它是Windows 8.1示例,但可以在UWP中轻松重用。

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