CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Referencing a Windows Metadata(.winmd) component in .NET 6

Open techyrajeev opened this issue 3 years ago • 12 comments

Hi Team,

I am using below api in .Net Framework based project ( Centennial app ).

     Windows.UI.Core.Preview.Communications.PreviewTeamDeviceCredentials

have added a .winmd reference for this. This api mainly used in Windows Teams OS ( specially meant for Surface Hub devices).

While migrating to .NET 6.0 I started facing below issue -

Windows.UI.Core.Preview.Communications.PreviewCommunicationsContract.winmd
dotnet\sdk\7.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1027,5): error NETSDK1130: 
Windows.UI.Core.Preview.Communications.PreviewCommunicationsContract.winmd cannot be referenced. 
Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. 
For more information, see https://aka.ms/netsdk1130

This api(Windows.UI.Core.Preview.Communications.PreviewTeamDeviceCredentials) is available as

Device family Windows Team Extension SDK (introduced in 10.0.19041.0)
API contract Windows.UI.Core.Preview.Communications.PreviewCommunicationsContract (introduced in v1.0)

In new project .NET 6.0 TFM is set to <TargetFramework>net6.0-windows10.0.19041</TargetFramework>

I tried to follow the link https://aka.ms/netsdk1130 but couldn't figure out what needs to be done and seems to be stuck.

can someone please help here?

Is there any sample available which I can refer?

techyrajeev avatar Nov 18 '22 16:11 techyrajeev

Right, you can't directly reference the winmd. I think you need to project that type (PreviewTeamDeviceCredentials) to use it, like this

j0shuams avatar Nov 21 '22 15:11 j0shuams

I am able remove errors with below changes, is this the correct way? lib is folder where I have stored winmd file within project root.

<PropertyGroup>
    <CsWinRTIncludes>Windows.UI.Core.Preview.Communications</CsWinRTIncludes>
</PropertyGroup>
  <ItemGroup>
    <!--Explicitly reference WinAppSDK winmds from TFM uap10.0-->
    <CsWinRTInputs Include="lib\*.winmd" />
  </ItemGroup>

Please review @j0shuams. Thanks

techyrajeev avatar Nov 24 '22 15:11 techyrajeev

Did you install C#/WinRT?

dongle-the-gadget avatar Nov 25 '22 08:11 dongle-the-gadget

yes @dongle-the-gadget, I did

techyrajeev avatar Nov 25 '22 08:11 techyrajeev

Debug is running fine but during release build getting this error @j0shuams, @dongle-the-gadget -

The type or namespace name 'Communications' does not exist in the namespace 'Windows.UI.Core.Preview' (are you missing an assembly reference?)

techyrajeev avatar Nov 25 '22 10:11 techyrajeev

@techyrajeev I'm surprised this is not working on release. Check your VS build configuration and for each permutation (Debug|x64, Release|x64, Debug|x86, etc...) make sure things are matching up.

If that's not it, feel free to attach a binlog.

j0shuams avatar Nov 29 '22 19:11 j0shuams

@j0shuams do I need to create a separate project with winmd file and generate its projection and reference the nuget in my current project? Currently I directly added

<PropertyGroup> <CsWinRTIncludes>Windows.UI.Core.Preview.Communications</CsWinRTIncludes> </PropertyGroup> <ItemGroup> <!--Explicitly reference WinAppSDK winmds from TFM uap10.0--> <CsWinRTInputs Include="lib\*.winmd" /> </ItemGroup>

in main csproj file.

Please let me know if this is incorrect way to use cswinrt.

techyrajeev avatar Dec 05 '22 17:12 techyrajeev

That is the correct way to generate a projection, you don't have to make a separate projection.

j0shuams avatar Dec 05 '22 17:12 j0shuams

so, the issue comes only during publishing (when generating appxbundle via visual studio) in release mode. When I run the program directly form Visual Studio it works fine both in release mode and debug mode.

techyrajeev avatar Dec 05 '22 18:12 techyrajeev

That is strange, could you attach a binlog?

j0shuams avatar Dec 07 '22 14:12 j0shuams

I'm also trying to reference Windows.winmd in .Net 6, but can't get it to work. Any suggestions? I've installed CsWinRT.

ulfemsoy avatar Feb 18 '23 16:02 ulfemsoy

C#/WinRT excludes Windows by default.

dongle-the-gadget avatar Feb 19 '23 03:02 dongle-the-gadget