microsoft / CsWinRT

C# language projection for the Windows Runtime
MIT License
524 stars 102 forks source link

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

Open techyrajeev opened 1 year ago

techyrajeev commented 1 year ago

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?

j0shuams commented 1 year ago

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

techyrajeev commented 1 year ago

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

dongle-the-gadget commented 1 year ago

Did you install C#/WinRT?

techyrajeev commented 1 year ago

yes @dongle-the-gadget, I did

techyrajeev commented 1 year ago

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?)

j0shuams commented 1 year ago

@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.

techyrajeev commented 1 year ago

@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

`

Windows.UI.Core.Preview.Communications

` in main `csproj` file. Please let me know if this is incorrect way to use cswinrt.
j0shuams commented 1 year ago

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

techyrajeev commented 1 year ago

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.

j0shuams commented 1 year ago

That is strange, could you attach a binlog?

ulfemsoy commented 1 year ago

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

dongle-the-gadget commented 1 year ago

C#/WinRT excludes Windows by default.