Reputation: 525
I have a class I am trying to send from WebAPI server w/ signalR to Blazor WASM. I used the new Blazor template with the .net core hosted option checked. It works fine sending a string or integer to the NewUser method, but when using a custom object like User seen below, I get nothing. I think there is a problem serializing/deserializing but I can't find any options. Am I missing something in the configuration?
public class User
{
public string Id { get; set; }
[Required(ErrorMessage ="You must specify a username")]
[StringLength(20,MinimumLength=1,ErrorMessage="Please enter a username no longer than 20 characters")]
public string Username { get; set; }
public string ConnectionId { get; set; }
}
Hub
public async Task Register(AppState state)
{
await _roomRepository.RegisterUser(state);
await Groups.AddToGroupAsync(state.user.ConnectionId,state.matchroom.Id);
await Clients.Group(state.matchroom.Id).SendAsync("NewUser", $"{state.user}");
}
Startup.cs (WebAPI Server) lots omitted
services.AddSignalR();
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
Blazor Page
protected override async Task OnInitializedAsync()
{
hubConnection = new HubConnectionBuilder()
.WithUrl(NavigationManager.ToAbsoluteUri("/matchhub"))
.Build();
hubConnection.On<User>("NewUser", (user) =>
{
AppState.matchroom.Users.Add(user);
StateHasChanged();
});
await hubConnection.StartAsync();
AppState.user.ConnectionId = hubConnection.ConnectionId;
await hubConnection.SendAsync("register", AppState)
}
Upvotes: 1
Views: 831
Reputation: 1931
It looks like you're writing state.user
as a string and sending that, so the client side can't match the User
type to the string
type it receives.
Upvotes: 1