Skip to content

About

A sample demonstrates in-proc/out-of-proc WinRT server with C# + CsWinRT

Resources

Code of conduct

Stars

25 stars

Watchers

3 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

WinRT Server with C# + CsWinRT

A sample demonstrates both in-process and out-of-process WinRT server with C# + CsWinRT.

What is an in-proc WinRT Server

The Windows Runtime (WinRT) supports the concept of In Process Servers, which allows for using objects that are in a different dlls with super-fast performance and easy-to-use ABI.

What is an out-of-proc WinRT Server

The Windows Runtime (WinRT) supports the concept of Out Of Process (OOP) Servers, which allows for using objects that are in a different process (or even a different machine) as though they were in the local process.

Why?

Cross language

It uses WinRT for communication so that any language that can use WinRT is able to be a WinRT server or serve as a client of a WinRT server.

Easy to use with async support

With built-in support for async primitives such as IAsyncAction, IAsyncOperation<T> and etc., it is easy to work with async code.

Complex types

Most RPC/ICP systems are just sending messages between the two processes. At most they can serialize and deserialize an object.

WinRT allows for more complicated objects, where the returned types can have methods, events, and properties. If you could do it with a local object, you can do it with a remote object.

Usage

Build and run

Use Visual Studio 2026 with the .NET desktop development and Windows application development workloads, the .NET 10 SDK, and the Windows 11 SDK (10.0.26100.0).

WinRTServerTest owns the MSIX manifest and package assets and includes the WinRT server in its package. Set WinRTServerTest as the startup project, select x86, x64, or ARM64, and run the WinRTServerTest launch profile to deploy and launch the packaged client.

To create an unsigned MSIX package from the command line:

dotnet build WinRTServer.sln -c Release -p:Platform=x64 -p:GenerateAppxPackageOnBuild=true

Packages are written under WinRTServerTest\AppPackages. Sign the package with a trusted certificate before installing it outside Visual Studio.

NativeAOT publishing

Install the Desktop development with C++ workload in Visual Studio to provide the native compiler and linker. Add the ARM64 C++ build tools when targeting ARM64.

To compile both the client and server with NativeAOT and create an unsigned MSIX package:

dotnet publish WinRTServerTest\WinRTServerTest.csproj -c Release -p:Platform=x64 -p:PublishAot=true -p:PublishAppxPackage=true

Use x86 or ARM64 for the corresponding platform. NativeAOT packages include the native executables and do not require an installed .NET runtime.

Server project

The server project needs to provide the implementation of types, and it uses CsWinRT to automatically generate interop code and winmd file, where the winmd file is served as a contract to be used between server and clients.

Structs, classes and delegates are supported, to add a type in the WinRT server:

  1. If you are adding a class, it must be sealed, and make sure you also add the type as an ActivatableClass in Package.appxmanifest under OutOfProcessServer (or InProcessServer if you are using the in-proc server), and if you are using the out-of-proc WinRT server, you also need to register an activation factory for it in the RoRegisterActivationFactories call.
  2. Methods, properties and events for both static and non-static are supported, but the type must have a projection (primitive types, types that have a .NET/WinRT mapping, WinRT types and types defined in the server project) before it can be used in the signature.

Client project

The client project only needs to consume the winmd file generated by the server project, all the server activation, type instantiation and marshaling things will be automatically done by CsWinRT.

Switch between in-process and out-of-process model

To switch the out-of-process WinRT server to in-process WinRT server:

  1. Change the OutputType of the server project to Library.
  2. Open WinRTServerTest\Package.appxmanifest, uncomment the in-process server manifest, and comment the out-of-process server manifest.

For NativeAOT publishing, set the in-process server's manifest path to WinRTServer\WinRTServer.dll. For managed publishing, use WinRTServer\WinRT.Host.dll.

About

A sample demonstrates in-proc/out-of-proc WinRT server with C# + CsWinRT

Resources

Code of conduct

Stars

25 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages