Skip to content

Commit

Permalink
Update authoring support for project references (#919)
Browse files Browse the repository at this point in the history
* update targets
  • Loading branch information
j0shuams authored Sep 15, 2021
1 parent 837b1a8 commit 8ec0bed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
19 changes: 13 additions & 6 deletions docs/authoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Overview

**Note: Authoring Support is still in preview**
**Note**: Authoring support is still in preview.
**Note**: C#/WinRT authoring and hosting project reference support is available with [.NET 6 RC1](https://dotnet.microsoft.com/download/dotnet/6.0) and later. This means the following scenario is **not supported in .NET 5**: C# app with a project reference to a C#/WinRT authored component.

C#/WinRT provides support for authoring Windows Runtime components. You can write a library in C#, and specify that it is a `CsWinRTComponent` for C#/WinRT to produce a WinMD that any WinRT compatible language can use. For example, a library written in C# can be used by a C++ program, via C#/WinRT and C++/WinRT.
Managed apps only need a project or package reference to the authored component, and native apps will need some extra steps that we cover in this documentation.
Expand All @@ -25,15 +26,19 @@ To author your component, first create a project using the C# **Class Library (.
- **net5.0-windows10.0.17763.0**
- **net5.0-windows10.0.18362.0**
- **net5.0-windows10.0.19041.0**
- **net5.0-windows10.0.20348.0**
- **net6.0-windows10.0.17763.0**
- **net6.0-windows10.0.18362.0**
- **net6.0-windows10.0.19041.0**
- **net6.0-windows10.0.20348.0**

2. Install the latest version of the [Microsoft.Windows.CsWinRT](https://www.nuget.org/packages/Microsoft.Windows.CsWinRT) NuGet package.

3. Add the following C#/WinRT specific properties to the project file. The `CsWinRTComponent` property specifies that your project is a Windows Runtime component, so that a WinMD file is generated for the component. The `CsWinRTWindowsMetadata` property provides a source for Windows Metadata and is required as of the latest C#/WinRT version.
3. Add the following C#/WinRT specific properties to the project file. The `CsWinRTComponent` property specifies that your project is a Windows Runtime component, so that a WinMD file is generated for the component.

```xml
<PropertyGroup>
<CsWinRTComponent>true</CsWinRTComponent>
<CsWinRTWindowsMetadata>10.0.19041.0</CsWinRTWindowsMetadata>
</PropertyGroup>
```

Expand Down Expand Up @@ -67,7 +72,7 @@ Similarly, any other dependencies, e.g. `Microsoft.WinUI`, will need to be inclu
<file src="$(TargetDir)Microsoft.Windows.SDK.NET.dll" target="lib\$(TargetFramework)\Microsoft.Windows.SDK.NET.dll" />

<!-- Note: you must rename the CsWinRT.Authoring.Targets as follows -->
<file src="C:\Path\To\CsWinRT\NugetDir\buildTransitive\Microsoft.Windows.CsWinRT.Authoring.targets"
<file src="C:\Path\To\CsWinRT\NugetDir\build\Microsoft.Windows.CsWinRT.Authoring.targets"
target="buildTransitive\MyAuthoredComponent.targets" />

<!-- buildTransitive is for consumers using packagereference, build is for consumers using packages.config -->
Expand Down Expand Up @@ -107,7 +112,7 @@ Consuming a C#/WinRT component from a C++/WinRT desktop application is supported

- For package references, simply right-click on the native project node and click **Manage NuGet packages** to find and install the component package.

- For project references, you also currently need a reference to the component's generated WinMD. The WinMD can be found in the output directory of the authored component's project. To add both the project and WinMD references, right-click on the native project node, and click **Add** -> **Reference**. Select the C#/WinRT component project under the **Projects** node and the generated WinMD file from the **Browse** node.
- For project references, if your authored component is built with C#/WinRT version 1.3.3 or earlier, you also need a WinMD reference along with the typical project reference. The WinMD can be found in the output directory of the authored component's project. To add both the project and WinMD references, right-click on the native project node, and click **Add** -> **Reference**. Select the C#/WinRT component project under the **Projects** node and the generated WinMD file from the **Browse** node.

For native consumption of C#/WinRT components, you also need to create a manifest file named `YourNativeApp.exe.manifest`. If your app is packaged with MSIX, then you don't need to include the manifest file. In the case that you do make a manifest, you need to add activatable class registrations for the public types in your component. We provide an [authoring sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/AuthoringDemo/CppConsoleApp) with an example manifest file. To create the manifest file:

Expand All @@ -119,7 +124,9 @@ For native consumption of C#/WinRT components, you also need to create a manifes

### Consuming from C# applications

Consuming a C#/WinRT component from C#/.NET 5 is supported by both package reference or project reference. This scenario is equivalent to consuming any ordinary C# class library and does not involve WinRT activation in most cases.
**Note** Starting with C#/WinRT 1.3.5, project references require .NET 6.

Consuming a C#/WinRT component from C#/.NET apps is supported by both package reference or project reference. This scenario is equivalent to consuming any ordinary C# class library and does not involve WinRT activation in most cases.

### Consuming an out of process component

Expand Down
56 changes: 46 additions & 10 deletions nuget/Microsoft.Windows.CsWinRT.Authoring.targets
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<CsWinRTDetectDependentAuthoringWinMDs Condition="'$(CsWinRTDetectDependentAuthoringWinMDs)'==''">true</CsWinRTDetectDependentAuthoringWinMDs>
</PropertyGroup>

<PropertyGroup>
<CsWinRTAuthoring_Platform Condition="'$(Platform)'=='AnyCPU'">x86</CsWinRTAuthoring_Platform>
<CsWinRTAuthoring_Platform Condition="'$(Platform)'!='AnyCPU'">$(Platform)</CsWinRTAuthoring_Platform>
</PropertyGroup>

<ItemGroup>
<CompilerVisibleProperty Include="AssemblyName" />
<CompilerVisibleProperty Include="AssemblyVersion" />
Expand Down Expand Up @@ -67,13 +72,13 @@ Copyright (C) Microsoft Corporation. All rights reserved.
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

<None Condition="Exists('$(CsWinRTPath)runtimes\win-$(Platform)\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-$(Platform)\native\WinRT.Host.dll">
<None Condition="Exists('$(CsWinRTPath)runtimes\win-$(CsWinRTAuthoring_Platform)\native\WinRT.Host.dll')"
Include="$(CsWinRTPath)runtimes\win-$(CsWinRTAuthoring_Platform)\native\WinRT.Host.dll">
<TargetPath>WinRT.Host.dll</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</ItemGroup>

<!-- Add the RuntimeConfig to output of project reference consumers -->
<Target Name="CsWinRTAuthoring_AddRuntimeDependenciesToContent"
Expand All @@ -96,16 +101,46 @@ Copyright (C) Microsoft Corporation. All rights reserved.

</Target>

<Target Name="GetTargetPath" Returns="@(TargetPathWithTargetPlatformMoniker)">
<!-- Because we remove this .dll during GetTargetPath, we have to make sure it gets placed in the output directory -->
<Target Name="CsWinRTAuthoring_OutputManagedDll" DependsOnTargets="GetTargetPathWithTargetPlatformMoniker" BeforeTargets="GetTargetPath">

<ItemGroup>
<AllItemsFullPathWithTargetPath Include="@(TargetPathWithTargetPlatformMoniker)">
<TargetPath>%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AllItemsFullPathWithTargetPath>

<AllPublishItemsFullPathWithTargetPath Include="@(TargetPathWithTargetPlatformMoniker)">
<TargetPath>%(Filename)%(Extension)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AllPublishItemsFullPathWithTargetPath>
</ItemGroup>

</Target>

<!-- Update the project to only output the assembly's .winmd -->
<Target Name="GetTargetPath" DependsOnTargets="CsWinRTAuthoring_OutputManagedDll" Returns="@(TargetPathWithTargetPlatformMoniker)">
<ItemGroup>

<CsWinRTComponent_ManagedImplementation Include="@(TargetPathWithTargetPlatformMoniker)">
<TargetPath>%(FullPath)</TargetPath>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</CsWinRTComponent_ManagedImplementation>
</ItemGroup>

<ItemGroup>
<!-- Clear the .dll -->
<TargetPathWithTargetPlatformMoniker Remove="@(TargetPathWithTargetPlatformMoniker)" />
<!-- Add the .winmd -->
<TargetPathWithTargetPlatformMoniker Include="$(TargetDir)$(AssemblyName).winmd">
<BuildReference>true</BuildReference>
<WinMDFile>true</WinMDFile>
<!-- implementation is NOT $(AssemblyName).dll -->
<Implementation>WinRT.Host.dll</Implementation>
<ResolveableAssembly>false</ResolveableAssembly>
<!-- show to c++ compiler (native case) and dotnet sdk (managed case) -->
<ResolveableAssembly>true</ResolveableAssembly>
<!-- Used by the dotnet sdk -->
<ManagedImplementation>%(CsWinRTComponent_ManagedImplementation.TargetPath)</ManagedImplementation>
<FileType>winmd</FileType>
<WinMDFile>true</WinMDFile>
<BuildReference>true</BuildReference>
<Primary>true</Primary>
</TargetPathWithTargetPlatformMoniker>

Expand Down Expand Up @@ -138,7 +173,8 @@ Copyright (C) Microsoft Corporation. All rights reserved.

<!-- Local item group to store the SDK.NET.dll -->
<CsWinRTSDKRefDll Include="@(RuntimePackAsset)" Condition="'%(RuntimePackAsset.DestinationSubPath)' == 'Microsoft.Windows.SDK.NET.dll'" />
<!-- Use the below item group to package up managed DLLs from one source -->
<!-- the target "CsWinRTIncludeHostDlls" uses the item group CsWinRTAuthoringDependencyDlls
as a source for all managed DLLS that need to be packaged -->
<CsWinRTAuthoringDependencyDlls Include="@(CsWinRTSDKRefDll)" />
<!-- Make sure it is copied out -->
<_ThisProjectItemstoCopyToOutputDirectory Include="@(CsWinRTSDKRefDll)">
Expand Down

0 comments on commit 8ec0bed

Please sign in to comment.