Skip to content

Does Microsoft.AspNetCore.SignalR.Client support AOT? #59133

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

Closed
JusterZhu opened this issue Nov 25, 2024 · 4 comments
Closed

Does Microsoft.AspNetCore.SignalR.Client support AOT? #59133

JusterZhu opened this issue Nov 25, 2024 · 4 comments
Labels
area-signalr Includes: SignalR clients and servers NativeAOT Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue

Comments

@JusterZhu
Copy link

Expected behavior
Can Microsoft.AspNetCore.SignalR.Client be used for AOT publishing in a console application or WPF

Actual behavior
When I publish the console application with AOT, it cannot receive server push messages. However, if I cancel the AOT publishing, it can send and receive messages normally. This issue occurs in .NET 9 and the latest version of Microsoft.AspNetCore.SignalR.Client 9.0.0.

Steps to reproduce
The code to reproduce the issue is very simple. It's just a standard example of a SignalR server and client. Once the client is compiled with AOT, it can no longer receive messages.

@ghost ghost added the area-signalr Includes: SignalR clients and servers label Nov 25, 2024
@martincostello
Copy link
Member

Looks like not all parts of SignalR support native AoT yet: #41704

@BrennanConroy BrennanConroy added the Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue label Nov 25, 2024
Copy link
Contributor

Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.

@dotnet-policy-service dotnet-policy-service bot added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Nov 25, 2024
@eerhardt
Copy link
Member

eerhardt commented Nov 26, 2024

@JusterZhu - native AOT is supported on both the client and server of SignalR. However, you need to use the JSON protocol. And you need to hook up the System.Text.Json source generator on both the client and server. See https://learn.microsoft.com/aspnet/core/release-notes/aspnetcore-9.0?view=aspnetcore-9.0#signalr-supports-trimming-and-native-aot

For the Client, this means configuring the JsonHubProtocolOptions object and adding a JsonSerializerContext to the PayloadSerializerOptions. For example:

[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(int))]
// add all the types you need to serialize
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}

...

var hubConnectionBuilder = new HubConnectionBuilder()
    .WithUrl(server.Url + "/hub");

hubConnectionBuilder.Services.Configure<JsonHubProtocolOptions>(o =>
{
    o.PayloadSerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});

var connection = hubConnectionBuilder.Build();

Closing as answsered. If this doesn't work for you, please reopen with a repro application that exhibits the problem.

@JusterZhu
Copy link
Author

It can now be used normally under AOT compilation according to your description. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers NativeAOT Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. Needs: Repro Indicates that the team needs a repro project to continue the investigation on this issue
Projects
None yet
Development

No branches or pull requests

4 participants