Skip to content

Commit

Permalink
Update the projection sample to .NET 6 (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelazhangmsft authored Nov 23, 2021
1 parent 9ad6e66 commit b216a38
Show file tree
Hide file tree
Showing 24 changed files with 131 additions and 179 deletions.
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For more information on using the NuGet package, refer to the [NuGet documentati

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.

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).
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/NetProjectionSample) and [associated walkthrough](https://docs.microsoft.com/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component).

### Generating an interop assembly

Expand All @@ -37,7 +37,7 @@ In the example diagram below, the projection project invokes **cswinrt.exe** at

An interop assembly is typically distributed along with the implementation assemblies as a NuGet package for applications 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 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 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.
See the [projection sample](https://github.com/microsoft/CsWinRT/tree/master/src/Samples/NetProjectionSample) for an example of how to create and reference the interop NuGet package.

### Applications

Expand All @@ -50,4 +50,4 @@ Application developers on .NET 5+ can reference C#/WinRT interop assemblies by a

## 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).
14 changes: 0 additions & 14 deletions src/Samples/Net5ProjectionSample/ConsoleAppSample/Program.cs

This file was deleted.

32 changes: 0 additions & 32 deletions src/Samples/Net5ProjectionSample/README.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<Platforms>x64</Platforms>
<Platforms>x64;x86;ARM64</Platforms>
</PropertyGroup>

<PropertyGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/Samples/NetProjectionSample/ConsoleAppSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;

var x = new SimpleMathComponent.SimpleMath();
Console.WriteLine("Adding 5.5 + 6.5 ...");
Console.WriteLine(x.add(5.5, 6.5).ToString());
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30404.54
# Visual Studio Version 17
VisualStudioVersion = 17.1.31911.260
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimpleMathComponent", "SimpleMathComponent\SimpleMathComponent.vcxproj", "{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleMathProjection", "SimpleMathProjection\SimpleMathProjection.csproj", "{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimpleMathComponent", "SimpleMathComponent\SimpleMathComponent.vcxproj", "{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{47FC5375-776F-4922-81DE-74E1A345F35F}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Expand All @@ -15,26 +15,36 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|ARM64.ActiveCfg = Debug|ARM64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|ARM64.Build.0 = Debug|ARM64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x64.ActiveCfg = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x64.Build.0 = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x86.ActiveCfg = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|ARM64.ActiveCfg = Release|ARM64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|ARM64.Build.0 = Release|ARM64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x64.ActiveCfg = Release|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x64.Build.0 = Release|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x86.ActiveCfg = Release|x64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|ARM64.ActiveCfg = Debug|ARM64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|ARM64.Build.0 = Debug|ARM64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|x64.ActiveCfg = Debug|x64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|x64.Build.0 = Debug|x64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|x86.ActiveCfg = Debug|Win32
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Debug|x86.Build.0 = Debug|Win32
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|ARM64.ActiveCfg = Release|ARM64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|ARM64.Build.0 = Release|ARM64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|x64.ActiveCfg = Release|x64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|x64.Build.0 = Release|x64
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|x86.ActiveCfg = Release|Win32
{D6B0C16D-858A-4C1B-99CF-D6F4CF5BCD5F}.Release|x86.Build.0 = Release|Win32
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x64.ActiveCfg = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x64.Build.0 = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Debug|x86.ActiveCfg = Debug|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x64.ActiveCfg = Release|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x64.Build.0 = Release|x64
{3D562B38-115F-481C-ADE2-5A60EFBA1ED0}.Release|x86.ActiveCfg = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
42 changes: 42 additions & 0 deletions src/Samples/NetProjectionSample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# C#/WinRT Projection Sample

This sample demonstrates how to do the following:

- Use the C#/WinRT package to generate a C# .NET projection interop assembly from a C++/WinRT component
- Distribute the component along with the interop assembly as a NuGet package
- Consume the component from a .NET 6 C# console application

## Requirements

* [Visual Studio 2022](https://visualstudio.microsoft.com/downloads/) with the Universal Windows Platform development workload installed. In **Installation Details** > **Universal Windows Platform development**, check the **C++ (v14x) Universal Windows Platform tools** option.
* [.NET 6 SDK](https://dotnet.microsoft.com/download/dotnet/6.0)
* nuget.exe 5.8.0-preview.2 or later (for command line MSBuild)

**Note**: This sample uses .NET 6 and therefore requires Visual Studio 2022 to build and run. If you prefer, you can use Visual Studio 2019 and modify the sample to target [.NET 5](https://dotnet.microsoft.com/download/dotnet/5.0). To do this, you will need to modify the `TargetFramework` and the *nuspec* file in the `SimpleMathProjection` project to target `net5.0-windows10.0.19041.0`.

## Build and run the sample

For building in Visual Studio:

1. Open *CppWinRTComponentProjectionSample.sln* in Visual Studio. Ensure that *SimpleMathProjection* is set as the startup project, and set the Platform and Configuration to x64 and Release. Right click on the solution and build. This will do the following:
- Build *SimpleMathComponent*: this will generate *SimpleMathComponent.winmd* and *SimpleMathComponent.dll*
- Generate the projection interop assembly for the component using C#/WinRT, *SimpleMathProjection.dll*
- Generate a NuGet package for the component. To ensure the solution has built successfully, navigate to the *SimpleMathProjection/nuget* folder in your file explorer. You should see the generated NuGet package (*SimpleMathComponent.0.1.0-prerelease.nupkg*). which can be referenced by C# .NET app consumers.

2. Open *ConsoleAppSample.sln* in Visual Studio. Build and run the solution which references and restores the SimpleMathComponent NuGet package to consume the projection.

- If you run into errors restoring NuGet packages, look at the docs on [NuGet restore options](https://docs.microsoft.com/nuget/consume-packages/package-restore). You may need to configure your NuGet package manager settings to allow for package restores on build. You may also need to run `nuget.exe restore ConsoleAppSample.sln` from a command prompt.

For building with the command line, execute the following:

```cmd
nuget restore CppWinRTComponentProjectionSample.sln
msbuild /p:platform=x64;configuration=release CppWinRTComponentProjectionSample.sln
nuget restore ConsoleAppSample.sln
msbuild /p:platform=x64;configuration=release ConsoleAppSample.sln
```

## Resources

- [Walkthrough documentation](https://docs.microsoft.com/windows/uwp/csharp-winrt/net-projection-from-cppwinrt-component) for this sample
- [C#/WinRT NuGet properties](../../../nuget/README.md)
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#include "pch.h"
#include "SimpleMath.h"
#include "SimpleMath.g.cpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#pragma once

#include "SimpleMath.g.h"
Expand Down
15 changes: 15 additions & 0 deletions src/Samples/NetProjectionSample/SimpleMathComponent/SimpleMath.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace SimpleMathComponent
{
[default_interface]
runtimeclass SimpleMath
{
SimpleMath();
Double add(Double firstNumber, Double secondNumber);
Double subtract(Double firstNumber, Double secondNumber);
Double multiply(Double firstNumber, Double secondNumber);
Double divide(Double firstNumber, Double secondNumber);
}
}
Loading

0 comments on commit b216a38

Please sign in to comment.