Repository navigation
How to author an out-of-process WinRT exe server with CsWinRT? #1064
|
I found this sample: https://github.com/microsoft/DesktopBridgeToUWP-Samples/tree/master/Samples/WinformsOutOfProcessWinRTComponent I'm trying to replace the C++ WinRT server with C# using CsWinRT: WinUIApp1.Server: using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Streams;
namespace WinUIApp1.Server;
class Program
{
static async Task Main()
{
while (true)
{
await Task.Delay(1000);
}
}
}
public sealed class TestClass
{
public IAsyncOperation<IInputStream> GetFileStreamAsync(string fileName)
{
Console.WriteLine("Received IPC Call");
return AsyncInfo.Run(async _ =>
{
var file = await StorageFile.GetFileFromPathAsync(fileName);
return await file.OpenSequentialReadAsync();
});
}
}and the csproj is: <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CsWinRTComponent>true</CsWinRTComponent>
<Platforms>x86;x64;arm64</Platforms>
<UseWindowsForms>true</UseWindowsForms>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="1.4.1" />
</ItemGroup>
</Project>Then in my client app I referenced the generated var server = new TestClass();And finally I created a package and registered the <Extension Category="windows.activatableClass.outOfProcessServer">
<OutOfProcessServer ServerName="WinUIApp1.Server" uap5:IdentityType="activateAsPackage" uap5:RunFullTrust="true">
<Path>WinUIApp1.Server\WinUIApp1.Server.exe</Path>
<Instancing>singleInstance</Instancing>
<ActivatableClass ActivatableClassId="WinUIApp1.Server.TestClass"></ActivatableClass>
</OutOfProcessServer>
</Extension>But it doesn't work. While calling the A minimal repro: |
Replies: 3 comments 8 replies
|
Steve (@hez2010), we currently don't have a sample for OOP WinRT, but there is a sample on OOP COM: https://github.com/dotnet/samples/tree/main/core/extensions/OutOfProcCOM For WinRT, you would use the same model as the OutofProcCOM sample but replace CoRegisterClassObject with RoRegisterActivationFactories, and use RoRevokeActivationFactories on shutdown to unregister the activation factories. |
|
is there any update? Any sample available now? |
|
Dev Home is using oop to create extension by cs/winrt, and winget oop is written in cpp/winrt. I make a Loopback Exemption Manager in UWP by oop. But I don't know how to use wrl to manage server. So I use the destructor to watching disposing... |
I made a sample for this: https://github.com/hez2010/WinRTServer
Note here we need two workarounds:
_AppxWinmdFilesToHarvest, see the targetRemoveOutOfProcWinMDin the wapproj. Otherwise an incorrectinProcessServerentry will be added to the manifest despite there's noinProcessServerat all, which will make the build fail.ItemGroup, otherwise the server implementation (WinRTServer.dll) will be removed from the package layout if you publish it to appx package, which can lead to failure while starting the server.