在Windows Service中等待IAsyncOperation:“类型在未引用的程序集中定义...”。 [英] IAsyncOperation await in Windows Service: "Type is defined in an assembly that is not referenced..."

查看:467
本文介绍了在Windows Service中等待IAsyncOperation:“类型在未引用的程序集中定义...”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务(使用此教程)。

I have a Windows Service (created using this tutorial).

我正在尝试运行 IAsyncOperation

var uri= new Uri(@"uri/to/second/app");
var options = new LauncherOptions
{
   TargetApplicationPackageFamilyName = "second-app-guid"
};
var results = await Launcher.LaunchUriForResultsAsync(uri, options);

但是,我从 await


  • 类型'IAsyncOperationWithProgress<,>'在未引用的程序集中定义。您必须添加对程序集'Windows,Version = 255.255.255.255,Culture = neutral,PublicKeyToken = null,ContentType = WindowsRuntime'的引用。

  • 类型 IAsyncOperation<>是在未引用的程序集中定义的。您必须添加对程序集'Windows,Version = 255.255.255.255,Culture = neutral,PublicKeyToken = null,ContentType = WindowsRuntime'的引用。

  • 在未引用的程序集中定义了类型 IAsyncActionWithProgress<>。您必须添加对程序集'Windows,Version = 255.255.255.255,Culture = neutral,PublicKeyToken = null,ContentType = WindowsRuntime'的引用。

  • 类型 IAsyncAction在未引用的程序集中定义。您必须添加对程序集'Windows,Version = 255.255.255.255,Culture = neutral,PublicKeyToken = null,ContentType = WindowsRuntime'的引用。

  • The type 'IAsyncOperationWithProgress<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
  • The type 'IAsyncOperation<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
  • The type 'IAsyncActionWithProgress<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.
  • The type 'IAsyncAction' is defined in an assembly that is not referenced. You must add a reference to assembly 'Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime'.

我已经查过此错误,很多人说这是由于VS安装错误引起的,但是执行全新安装(仅检查ISO的校验和)未能解决问题。 。

I have looked this error up and a lot of people said that it is due to a VS installation error but, performing a clean install (whilst checking the checksums of the ISOs) failed to solve the problem.

此外,我在一个空白的Universal Windows App中尝试了此操作,但未出现此错误。因此,我想知道Windows服务中是否还可以做到这一点? (.NET Framework版本4.6.1)

Also, I tried this in a blank Universal Windows App and this error didn't appear. So I wonder if this is even possible in a Windows Service? (.NET Framework version 4.6.1)

推荐答案

根据您的代码,您似乎想在Windows中使用UWP API服务应用程序,并且从您的错误中,我想这里的问题是因为您在项目中缺少参考

According to your code, it seems you want to use UWP APIs in a Windows Service application and from your error, I'd suppose the problem here is because you are missing References in your project.

调用UWP API从经典的.NET Framework应用程序中,我们应该添加以下两个引用:

To call UWP APIs from a classic .NET Framework application, we should add the following two references:


  • 首先,我们需要添加对包含所有受支持API的定义的元数据文件:它称为 Windows.md ,我们可以在计算机的以下路径中找到它: C:\Program Files(x86) \Windows套件\10\UnionMetadata

  • Firstly, we need to add a reference to the metadata file that contains the definition of all the supported APIs: it’s called Windows.md and we can find it in the following path on our computer: C:\Program Files (x86)\Windows Kits\10\UnionMetadata.

第二个是 System.Runtime.WindowsRuntime.dll 并且可以在以下路径中找到它: C:\Program Files(x86)\参考程序集\Microsoft\Framework\.NETCore\v4.5 。没有这个DLL,我们就不能在WinRT类型上使用 await ,这个DLL提供了.NET和WinRT之间的互操作。

The second is System.Runtime.WindowsRuntime.dll and we can find it in the following path: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5. Without this DLL, we can't use use await on WinRT types, this and other interop between .NET and WinRT are provided by this DLL.

此外,我们还可以使用 UWPDesktop NuGet软件包,而不是上面的两个引用。有关更多信息,请参见调用Windows

Besides, we can also using UWPDesktop NuGet package instead of above two references. For more info, please see Calling Windows 10 APIs From a Desktop Application.


所以我想知道Windows服务中是否有可能? (.NET Framework版本4.6.1)

So I wonder if this is even possible in a Windows Service? (.NET Framework version 4.6.1)

但是,即使您修复了这些编译器错误,您的代码仍然无法使用。这是因为 Launcher.LaunchUriForResultsAsync 不支持标准桌面应用程序&中的API。 Windows服务。因此,不允许在 Windows服务中使用 Launcher.LaunchUriForResultsAsync 方法,甚至不允许使用 Launcher.LaunchUriAsync 方法在Windows Service中不起作用。

However, even you fixed these compiler error, your code still can't work. This is because Launcher.LaunchUriForResultsAsync is not an API supported in standard desktop applications & Windows Service. So it is not allowed to use Launcher.LaunchUriForResultsAsync method in Windows Service, even Launcher.LaunchUriAsync method is not workable in Windows Service.

这篇关于在Windows Service中等待IAsyncOperation:“类型在未引用的程序集中定义...”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆