我有一个 UWP WRC 库,我需要从命令行应用程序对其进行测试。当我尝试开始下载时挂起。
我将这个简单的 UWP C# 控制台应用放在一起,它是我使用 Pieter Nijs“在 C# 中创建 UWP 控制台应用”教程创建的。它演示了当我尝试使用 BackgroundDownloader 开始下载时收到的挂起。如果提供超时,它将超时而不是挂起。我正在使用 Visual Studio 2022。我正在 Windows 10(本地计算机)上进行调试。
using System;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Networking.BackgroundTransfer;
using Windows.Foundation;
namespace TestRunnerRt
{
internal class Program
{
static async Task Main(string[] args)
{
try
{
Uri source = new Uri("https://some url"); // insert valid url
string destination = "downloaded.htm";
StorageFile destinationFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destination, CreationCollisionOption.ReplaceExisting);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
Console.WriteLine($"Starting download from {source}");
var task = download.StartAsync().AsTask();
// *** The next line hangs ***
task.Wait();
}
catch (Exception ex)
{
Console.WriteLine("Download Error: ", ex);
}
// wait for user to press a key before exiting (and closing the console)
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
我尝试了几种开始下载的方法。我也试过在 Task.Run() 中启动它,以及简单地等待 DownloadOperation::StartAsync() 调用:
DownloadOperation r = null;
var task = Task.Run(async () => r = await download.StartAsync());
它总是挂起(或超时)。 “downloaded.htm”文件始终为零字节。
我启用了“互联网(客户端和服务器)”、“互联网(客户端)”和“专用网络(客户端和服务器)”功能。
这是我的清单的样子:
<?xml version="1.0" encoding="utf-8"?>
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"
xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
IgnorableNamespaces="uap mp iot uap4 rescap uap3">
<Identity
Name="TestRunnerRt"
Publisher="CN=py122c"
Version="1.0.0.0" />
<mp:PhoneIdentity PhoneProductId="7c65166f-96f5-4385-b3e7-799a3040b21d" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>
<Properties>
<DisplayName>TestRunnerRt</DisplayName>
<PublisherDisplayName>py122c</PublisherDisplayName>
<Logo>Assets\StoreLogo.png</Logo>
</Properties>
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
</Dependencies>
<Resources>
<Resource Language="x-generate"/>
</Resources>
<Applications>
<Application Id="App"
Executable="$targetnametoken$.exe"
EntryPoint="TestRunnerRt.App"
desktop4:Subsystem="console"
desktop4:SupportsMultipleInstances="true"> <!-- this app will run in the console (and can run in multiple instances) -->
<uap:VisualElements
DisplayName="TestRunnerRt"
Square150x150Logo="Assets\Square150x150Logo.png"
Square44x44Logo="Assets\Square44x44Logo.png"
Description="TestRunnerRt"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
<uap:SplashScreen Image="Assets\SplashScreen.png" />
</uap:VisualElements>
<!-- Allow app to start from the console -->
<Extensions>
<uap5:Extension
Category="windows.appExecutionAlias"
Executable="$targetnametoken$.exe"
EntryPoint="$targetnametoken$.Program">
<uap5:AppExecutionAlias desktop4:Subsystem="console">
<uap5:ExecutionAlias Alias="$targetnametoken$.exe" />
</uap5:AppExecutionAlias>
</uap5:Extension>
</Extensions>
</Application>
</Applications>
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
<Capability Name="internetClientServer"/>
<Capability Name="privateNetworkClientServer"/>
<uap:Capability Name="sharedUserCertificates"/>
<uap4:Capability Name="userDataTasks"/>
<uap3:Capability Name="remoteSystem"/>
<DeviceCapability Name="location"/>
</Capabilities>
</Package>