PMARSH
PMARSH

Reputation: 167

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:

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)

Upvotes: 7

Views: 8159

Answers (4)

Tuomas Hietanen
Tuomas Hietanen

Reputation: 5323

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.

Upvotes: 0

Suplanus
Suplanus

Reputation: 1601

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

Upvotes: 0

Taylor
Taylor

Reputation: 316

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.

Upvotes: 0

Jay Zuo
Jay Zuo

Reputation: 15758

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.

Upvotes: 6

Related Questions