2

I'm trying to pass an object from client to hub: On client:

connection.invoke('MyMethod', {
                i: 1,
                a: 25
            });

On hub:

        public async Task MyMethod(TestModel model)
        {
            // logic
        }

Model:

public class TestModel
{
    [JsonProperty("i")]
    public double Min {get;set;}
    [JsonProperty("a")]
    public double Max {get;set;}
}

In the MyMethod the model is not null, but the values are always 0.

What I'm doing wrong?

2
  • 1
    The JsonProperty attribute is used when you're serializing the TestModel not binding it with json according to Newtonsoft docs. Did you try naming the TestModel fields i and a as a test? Commented Sep 17, 2020 at 12:14
  • @HMZ you're right, renamed them and it works. How do I use simplified names then? I want to reduce the data amount per request Commented Sep 17, 2020 at 12:36

1 Answer 1

7

According to your description, you should use Newtonsoft.Json in an ASP.NET Core 3.0 SignalR project, since the asp.net core doesn't use Newtonsoft.Json by default.

You should install the Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson NuGet package and then modify the startp.cs ConfigureServices method as below:

services.AddSignalR()
    .AddNewtonsoftJsonProtocol();

More details, you could refer to this article.

Sign up to request clarification or add additional context in comments.

2 Comments

what to do with client side signalR.HubConnectionBuilder?
Use this option .withHubProtocol(new signalR.JsonHubProtocol()). Works with newtonsoft json protocol as well. Not sure why this isn´t in their documentation at all as far as I can tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.