Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specified cast is not valid while using as WinRT server on net8.0 #19

Closed
hez2010 opened this issue Dec 6, 2023 · 18 comments · Fixed by #20
Closed

Specified cast is not valid while using as WinRT server on net8.0 #19

hez2010 opened this issue Dec 6, 2023 · 18 comments · Fixed by #20
Assignees

Comments

@hez2010
Copy link

hez2010 commented Dec 6, 2023

Hello, thanks for the project.

I'm trying to use it to create a WinRT server with CsWinRT to run on net8.0, and here is the project I'm using for testing: https://github.com/hez2010/WinRTServerTest

I copy-pasted your library code because in net8.0 the IActivationFactory interface type will fail to load, so I removed the : IActivationFactory from BaseActivationFactory.

The ActivationFactory can be created successfully, but when activating the class TestClass, it failed to cast the managed object (an instance of TestClass) to IInspectable at unkwnPtr.CopyTo(instance) in BaseActivationFactoryProxy.ActivateInstance with Specified cast is not valid..

image
@shmuelie
Copy link
Owner

shmuelie commented Dec 8, 2023

Yeah, the server components highly rely on the built in COM/WinRT marshaling. I've started working on support for "modern" .NET code but it's not working yet.

@shmuelie shmuelie self-assigned this Dec 17, 2023
@shmuelie
Copy link
Owner

I got the COM Server working on modern .NET. Working on WinRT server now

@shmuelie
Copy link
Owner

Ok, @hez2010 try using https://github.com/shmuelie/Shmuelie.WinRTServer/pkgs/nuget/Shmuelie.WinRTServer/158852442 and if that works, I'll prepare a full release for nuget.org

@hez2010
Copy link
Author

hez2010 commented Dec 17, 2023

Hello, thanks for the investment into modern .net support for WinRT.

I tested it with a simple test project https://github.com/hez2010/WinRTServerTest but unfortunately it still failed with Interface was not supported.
However, this time the instance on the server (WinRTServer) can be activated successfully, but soon it failed at VftblIUnknown.QueryInterface(ThisPtr, &iid, &thisPtr); in WinRT.IObjectReference.TryAs on the client side (WinRTServerTest) with HRESULT 0x80004002.

Is there anything I'm missing in the test project?

@hez2010
Copy link
Author

hez2010 commented Dec 17, 2023

I suspect this is an issue from CsWinRT as I hit the same exception while using RoRegisterActivationFactories manually.
Opened an issue there: microsoft/CsWinRT#1419

@shmuelie
Copy link
Owner

Ok, I looked at your test project and you're missing the Metadata and interface projects that my sample shows. I admit having them is a pain but I haven't been able to get away from them yet

@hez2010
Copy link
Author

hez2010 commented Dec 18, 2023

I see. I just added the interface and metadata projects but still with no luck.
You may see the simple test project here: https://github.com/hez2010/WinRTServerTest/tree/metadata

@shmuelie
Copy link
Owner

@hez2010
Copy link
Author

hez2010 commented Dec 20, 2023

Still no luck even if with a GuidAttribute added to the TestClass.
And note that if I downgrade the server project to net7.0-windows10.0.22621.0, it will fail at Specified cast is not valid at unkwnPtr.CopyTo(instance) again as the top-most post of this issue.

@shmuelie
Copy link
Owner

The ITestCase needs to use the Windows.Foundation.Metadata.GuidAttribute attribute, not the System.Runtime.InteropServices.GuidAttribute attribute.

@hez2010
Copy link
Author

hez2010 commented Dec 24, 2023

Still not working even with Windows.Foundation.Metadata.GuidAttribute. I'm going to create a new project and see if it works.

@shmuelie
Copy link
Owner

@hez2010 if that doesn't work, could you see if the test projects in the modern-net branch work for you?

@hez2010
Copy link
Author

hez2010 commented Dec 27, 2023

I changed my client from net8.0 to net481 while kept the server project net8.0 and this time it works as expected.
Seems that it's definitely a CsWinRT issue.

@shmuelie
Copy link
Owner

That is very weird as Shmuelie.WinRTServer.Sample.WpfNet6Client worked for me with the WinRT Activation of a .NET 8 server.

@hez2010
Copy link
Author

hez2010 commented Dec 27, 2023

Yeah, it's very weird as your sample also works for me...

Here is the repro: https://github.com/hez2010/WinRTServerRepro
It will only work if you change the TFM of WinRTServer.Client/WinRTServer.Client.csproj from net8.0-windows10.0.22621.0 to net481.

@hez2010
Copy link
Author

hez2010 commented Dec 27, 2023

It turns out to be my fault that I didn't copy the winmd files next to the client exe.
Now it works as expected.

Anyway, a big thanks for your work, and feel free to close this issue and release the new package :)

@shmuelie
Copy link
Owner

Nice. I'll try to make some improvements to the documentation first but awesome!

@shmuelie
Copy link
Owner

@hez2010 version 1.2.0 published to nuget!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants