IAsyncOperation await in Windows Service: “Type is defined in an assembly that is not referenced…”

久未见 提交于 2019-11-28 05:21:23

问题


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

And I am trying to run an IAsyncOperation:

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

However, I get the following error from the await:

  • 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'.

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.

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)


回答1:


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.

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

  • 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.

  • 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.

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.

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

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.




回答2:


Download the Windows 10 SDK at https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk. Then add Windows.winmd and System.Runtime.WindowsRuntime.dll as your references. This worked for me.




回答3:


You could use PowerShell. e.g.:

using (PowerShell shell =  PowerShell.Create( ))
{
    shell.AddScript("explorer.exe Kodi://");
    shell.Invoke();
}


来源:https://stackoverflow.com/questions/41943297/iasyncoperation-await-in-windows-service-type-is-defined-in-an-assembly-that-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!