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

Bug: Cannot cast some of the UWP activation arguments using dotnet as operator #1860

Open
lhak opened this issue Nov 3, 2024 · 6 comments · May be fixed by #1873
Open

Bug: Cannot cast some of the UWP activation arguments using dotnet as operator #1860

lhak opened this issue Nov 3, 2024 · 6 comments · May be fixed by #1873

Comments

@lhak
Copy link

lhak commented Nov 3, 2024

Description

This is related to microsoft/microsoft-ui-xaml#9983 (comment)

When publishing a UWP project using the preview .net 9 support with AOT, I noticed a crash when an appservice from a win32 helper tries to connect. It turns out that casting the argument (args.TaskInstance.TriggerDetails) in the BackgroundActivated(BackgroundActivatedEventArgs args) activation method to AppServiceTriggerDetails fails using the dotnet as operator. However, it works using args.TaskInstance.TriggerDetails.As<AppServiceTriggerDetails>().

Steps To Reproduce

A project that uses background activation through an appservice with a win32 helper is needed. The project also has to be published with AOT in release mode by creating an msix package (it sometimes worked in Debug configuration).

Expected Behavior

Casting using dotnet operator should work

Version Info

cswinrt 2.1.16
dotnet 9 rc2

Additional Context

No response

@dongle-the-gadget
Copy link
Contributor

Does (AppServiceTriggerDetails)args.TaskInstance.TriggerDetails work?

@lhak
Copy link
Author

lhak commented Nov 4, 2024

No, I get an InvalidCastException in this case.

@dongle-the-gadget
Copy link
Contributor

Can you try to check if the type of TriggerDetails is WinRT.IInspectable?

@lhak
Copy link
Author

lhak commented Nov 5, 2024

Using GetType().FullName indeed returns WinRT.IInspectable when publishing the msix with AOT and AppServiceTriggerDetails otherwise.

@dongle-the-gadget
Copy link
Contributor

I've found the issue with the as operator. Since C#/WinRT could not reason what the actual type is in AOT mode (since it doesn't support dynamic code), it fallbacks to the IDynamicInterfaceCastable implementation. That implementation then checks the helper type of AppServiceTriggerDetails, which it expects an interface. However, the helper type of AppServiceTriggerDetails is a struct, not an interface, hence it fails.

I haven't managed to see why As<TInterface>() works though, since from my initial look it seems to also be going through IDynamicInterfaceCastable?

@manodasanW
Copy link
Member

Given this is a class type with just an WinRT exclusive interface, there isn't any IDynamicInterfaceCastable interface today. I assume As<> works because it is making sure the type doesn't get trimmed whereas with just a cast, .NET is deciding it can trim it as it only seen it used with the cast especially given the API that it is returning this says it returns an Object.

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.

3 participants