IAsyncOperation await in Windows Service: "Type is defined in an assembly that is not referenced..."
Asked Answered
T

4

7

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)

Targe answered 30/1, 2017 at 18:48 Comment(1)
You're running as a service. There may be no users logged on when you're running. There may be multiple users logged on when you're running. You can't launch an app - services aren't allowed to interact with desktops, even if one does exist,Gagne
P
6

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.

Pleuropneumonia answered 1/2, 2017 at 11:24 Comment(1)
What if the folder .../10/UnionMetadata does not contain Windows.winmd? It contains subfolders for the different SDKs; in my case, /10.0.19041.0/Windows.winmd. Does this still work?Lutyens
I
0

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.

Interdigitate answered 19/10, 2018 at 19:0 Comment(0)
W
0

I have to add Windows 10 SDK to the Visual Studio installation.

Warwickshire answered 5/6, 2020 at 12:26 Comment(0)
L
0

Rather than adding more dependencies, it's better to try to remove them, to stop this cancer spreading. The core problem is the Nuget package of System.Runtime.WindowsRuntime which tries to load these, and whenever you e.g. use reflection, the dependencies are also loaded.

It's some old WinRT things, but it seems that packages like System.Net.Http has it still in dependencies. But it's only needed in ".NET Core 5.0" which is a deprecated alias for UAP10.0. So if you can restrict your dependencies targetframeworks to exclude that, it should fix the problem.

Lavinialavinie answered 21/12, 2022 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.