Skip to content

Commit

Permalink
move projection steps to usage doc
Browse files Browse the repository at this point in the history
  • Loading branch information
angelazhangmsft authored Apr 8, 2021
1 parent 2a0ba3f commit 7e371b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ However, C#/WinRT is a general effort and is intended to support other scenarios

## What's New

See our [release notes](https://github.com/microsoft/CsWinRT/releases) for the most recent C#/WinRT releases and corresponding .NET SDK versions. C#/WinRT runtime and Windows SDK projection updates typically become available in an upcoming .NET SDK update, which follows a monthly release cadence. We also make updates to the C#/WinRT tool itself, which are shipped through the C#/WInRT NuGet package. Details on breaking changes and specific issues can be found in the releases notes.
See our [release notes](https://github.com/microsoft/CsWinRT/releases) for the latest C#/WinRT releases and corresponding .NET SDK versions. C#/WinRT runtime and Windows SDK projection updates typically become available in a future .NET SDK update, which follows a monthly release cadence. We also make updates to the C#/WinRT tool itself, which are shipped through the C#/WinRT NuGet package. Details on breaking changes and specific issues can be found in the releases notes.

[C#/WinRT version 1.1.4](https://github.com/microsoft/CsWinRT/releases/tag/1.1.4.210316.1) is aligned with the [.NET April 2021 Update](https://github.com/dotnet/core/blob/main/release-notes/5.0/5.0.5/5.0.5.md) which includes bugfixes to the runtime and Windows SDK projections, as well as improvements to [C#/WinRT authoring support](https://github.com/microsoft/CsWinRT/blob/master/docs/authoring.md) which is currently in preview.

Expand All @@ -26,7 +26,7 @@ Download the C#/WinRT NuGet package here: https://www.nuget.org/packages/Microso

### Documentation

- [Usage guide](docs/usage.md) - information on how to use the C#/WinRT package to generate a projection
- [Usage guide](docs/usage.md) - usage guide for developers
- [C#/WinRT NuGet properties](nuget/readme.md) - documentation on customizing C#/WinRT NuGet package properties
- [Repository structure](docs/structure.md) - detailed breakdown of this repository
- [COM Interop guide](docs/interop.md) - for recommendations on migrating from System.Runtime.InteropServices
Expand All @@ -37,22 +37,6 @@ For additional documentation and walkthroughs, visit <http://aka.ms/cswinrt>.

The C#/WinRT runtime assembly, `WinRT.Runtime.dll`, is required by all C#/WinRT assemblies. It provides an abstraction layer over the .NET runtime, supporting .NET 5+. The runtime assembly implements several features for all projected C#/WinRT types, including WinRT activation, marshaling logic, and [COM wrapper](https://docs.microsoft.com/dotnet/standard/native-interop/com-wrappers) lifetime management.

### Create and distribute an interop assembly

Component authors need to build a C#/WinRT interop assembly for .NET 5+ consumers. In the diagram below, the **cswinrt.exe** tool processes Windows Metadata (`*.winmd`) files in the "Contoso" namespace to create projection source files. These source files are then compiled into an interop projection assembly named `Contoso.projection.dll`. The interop assembly must be distributed along with the implementation assemblies (`Contoso.*.dll`) as a NuGet package. See [this walkthrough](https://docs.microsoft.com/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component) for details on how to generate a .NET 5+ projection using C#/WinRT.

<img alt="Creating projection"
src="docs/images/Diagram_CreateProjection.jpg"
width="70%" height="50%">

### Reference an interop assembly

Application developers on .NET 5+ can reference C#/WinRT interop assemblies by adding a reference to the interop NuGet package. This replaces any `*.winmd` references. The .NET 5+ application includes `WinRT.Runtime.dll` which handles WinRT type activation logic.

<img alt = "Adding projection"
src="docs/images/Diagram_AddProjection.jpg"
width="70%" height="50%">

## Building and running C#/WinRT

C#/WinRT currently requires the following packages, or newer, to build:
Expand Down
47 changes: 35 additions & 12 deletions docs/usage.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,53 @@
# Usage
# Usage Guide

The [C#/WinRT NuGet package](https://www.nuget.org/packages/Microsoft.Windows.CsWinRT/) provides tooling for the following scenarios:

- [Generate and distribute a projection interop assembly](#generate-and-distribute-a-projection-interop-assembly)
- [Author and consume a C#/WinRT component](#author-and-consume-a-cwinrt-component)
- [Generate and distribute an interop assembly](#generate-and-distribute-an-interop-assembly)
- [Author and consume a C#/WinRT component](#author-and-consume-a-cwinrt-component) (in preview)

For more information on using the NuGet package, refer to the [NuGet documentation](../nuget/readme.md).
For more information on using the NuGet package, refer to the [NuGet documentation](../nuget/readme.md). Command line options can be displayed by running `cswinrt -?`.

## Generate and distribute a projection interop assembly
## Generate and distribute an interop assembly

A projection project adds a NuGet reference to C#/WinRT to invoke cswinrt.exe at build time, generate projection sources, and compile these into a projection interop assembly. For an example of this, see the [Projection Sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/Net5ProjectionSample) and associated [walkthrough](https://docs.microsoft.com/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component). Command line options can be displayed by running `cswinrt -?`. The interop assembly is then typically distributed as a NuGet package itself for application projects to use.
Component authors need to build a C#/WinRT interop assembly for .NET 5+ consumers. The C#/WinRT NuGet package (Microsoft.Windows.CsWinRT) includes the C#/WinRT compiler, **cswinrt.exe**, which you can use to process .winmd files and generate projection source files. These source files are compiled into an interop projection assembly, and then distributed along with the C#/WinRT runtime assembly for .NET 5+ applications to reference.

To invoke cswinrt to generate projection sources for types in the **Contoso** namespace, you need to add the following property to your C# library project file. In this project you would also need to reference the C#/WinRT NuGet package and the project-specific `.winmd` files you want to project, whether through a NuGet package, project reference or direct reference. By default, the **Windows** and **Microsoft** namespaces are not projected.
For an example of generating and distributing a projection interop assembly as a NuGet package, see the [projection sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/Net5ProjectionSample) and [associated walkthrough](https://docs.microsoft.com/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component).

```
### Generating an interop assembly

Component authors create a projection project, which is a C# library project that references the C#/WinRT NuGet package and the project-specific `.winmd` files you want to project, whether through a NuGet package reference, project reference, or direct file reference.

The following project fragment demonstrates a simple invocation of C#/WinRT to generate projection sources for types in the "Contoso" namespace. These sources are then included in the project build. By default, the **Windows** and **Microsoft** namespaces are not projected.

```xml
<PropertyGroup>
<CsWinRTIncludes>Contoso</CsWinRTIncludes>
</PropertyGroup>
```

To further customize C#/WinRT behavior, refer to the [C#/WinRT NuGet documentation](../nuget/readme.md).
For a full list of C#/WinRT NuGet project properties, refer to the [NuGet documentation](../nuget/readme.md).

In the example diagram below, the projection project invokes **cswinrt.exe** at build time, which processes `.winmd` files in the "Contoso" namespace to generate projection source files and compiles these into an interop projection assembly named `Contoso.projection.dll`. The interop assembly is typically distributed along with the implementation assemblies (`Contoso.*.dll`) as a NuGet package.

<img alt="Creating projection"
src="images/Diagram_CreateProjection.jpg"
width="70%" height="50%">

### Distributing the interop assembly

An interop assembly is typically distributed along with the implementation assemblies as a NuGet package for application projects to reference. This package will require a dependency on C#/WinRT to include `WinRT.Runtime.dll` for .NET 5+ targets. If the interop assembly is not distributed as a NuGet package, an application project adds references to both the component interop assembly produced above and to C#/WinRT to include the `WinRT.Runtime.dll` assembly. If a third party WinRT component is distributed without an official interop assembly, an application project may add a reference to C#/WinRT to generate its own private component interop assembly. There are versioning concerns related to this scenario, so the preferred solution is for the third party to publish an interop assembly directly.

See the [projection sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/Net5ProjectionSample) for an example of how to create and reference the interop NuGet package.

### Application projects

Application developers on .NET 5+ can reference C#/WinRT interop assemblies by adding a reference to the interop NuGet package, as shown in the diagram below. This replaces any `*.winmd` references. The .NET 5+ application includes `WinRT.Runtime.dll` which handles WinRT type activation logic.

### Application Project
<img alt = "Adding projection"
src="images/Diagram_AddProjection.jpg"
width="70%" height="50%">

An interop assembly is typically distributed as a NuGet package for application projects to reference. This package will require a dependency on C#/WinRT to include `WinRT.Runtime.dll` for .NET 5 targets. If the interop assembly is not distributed as a NuGet package, an application project adds references to both the component interop assembly produced above and to C#/WinRT to include the `WinRT.Runtime.dll` assembly. If a third party WinRT component is distributed without an official interop assembly, an application project may add a reference to C#/WinRT to generate its own private component interop assembly. There are versioning concerns related to this scenario, so the preferred solution is for the third party to publish an interop assembly directly.

## Author and consume a C#/WinRT component

C#/WinRT provides authoring and hosting support for .NET 5 component authors. For more details, refer to this [walkthrough](https://docs.microsoft.com/en-us/windows/uwp/csharp-winrt/create-windows-runtime-component-cswinrt) and these [authoring docs](https://github.com/microsoft/CsWinRT/blob/master/docs/authoring.md).
C#/WinRT provides authoring and hosting support for .NET 5 component authors. For more details, refer to this [walkthrough](https://docs.microsoft.com/en-us/windows/uwp/csharp-winrt/create-windows-runtime-component-cswinrt) and these [authoring docs](https://github.com/microsoft/CsWinRT/blob/master/docs/authoring.md).

0 comments on commit 7e371b2

Please sign in to comment.