Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 671
PMARSH Avatar asked Jan 30 '17 18:01

PMARSH


1 Answers

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.

like image 163
Jay Zuo Avatar answered Nov 10 '22 21:11

Jay Zuo