我有一个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"文件的结果总是0字节。
我已经启用了"Internet (Client)"服务器)";互联网(客户端)"one_answers"私人网络(客户端)";服务器),功能。
清单如下:
<?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>AssetsStoreLogo.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="AssetsSquare150x150Logo.png"
Square44x44Logo="AssetsSquare44x44Logo.png"
Description="TestRunnerRt"
BackgroundColor="transparent">
<uap:DefaultTile Wide310x150Logo="AssetsWide310x150Logo.png"/>
<uap:SplashScreen Image="AssetsSplashScreen.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>
后台传输与呼叫应用程序分开运行,主要用于视频,音乐和大图像等资源的长期传输操作。
UWP控制台应用程序的其他注意事项
UWP控制台应用程序不能使用后台任务,也不能作为后台任务。
所以,你不能在UWP控制台应用程序中使用BackgroundDownloader
。建议您在普通的UWP应用程序中使用BackgroundDownloader
。