Skip to content

Unable to run .NET Standard 2.0 binaries with embedded mode on .NET Framework 4.8 #1719

Open

Description

Describe the bug
I have an existing library which used built-in support for WinRT. This library is used by both .NET Framework 4.x and .NET Core. In order to add support for .NET 5 and newer consumers of this library, I adopted C#/WinRT's embedded mode with the .NET Standard 2.0 target. However, when I tested the binary on .NET Framework 4.8, I got this exception:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified. File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at WinRT.MarshalString.Pinnable.GetAbi()
at WinRT.WinrtModule.GetActivationFactory[I](String runtimeClassId, Guid iid)
at WinRT.ActivationFactory.Get[I](String typeName, Guid iid)
...

The code in Marshallers.cs is using System.Runtime.CompilerServices.Unsafe. Although this is included in .NET Standard 2.0, it is not in .NET Framework 4.8. I'm guessing this is a bug in what types are in .NET Standard 2.0. C#/WinRT's embedded mode should work with .NET Framework 4.8 when using the .NET Standard 2.0 target framework.

If this is not intended to be supported, the documentation should be updated to state this. In https://github.com/microsoft/CsWinRT/blob/master/docs/embedded.md, it states: "App consumers are able to target any .NET Standard 2.0 compatible TFM". .NET Framework 4.8 is .NET Standard 2.0 compatible according to https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0.

Multi-targetting isn't a good option for me as the existing deployment mechanism expects a single set of binaries to work with all consumers.

To Reproduce
Use C#/WinRT embedded mode with a target framework of netstandard2.0 and try to run the assembly in a .NET Framework 4.8 process.

Expected behavior
WinRT APIs should work.

Version Info
CsWinRT NuGet: 2.0.8
Visual Studio version: 17.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions