title: SignalR How to receive a custom data type?
body: <p>I am working on a server using SignalR and Asp.Net Core 2.2.</p>
  
  <p>I have a function on the server side that can receive 3 parameters, 2 of these parameters are optional. This is not supported in SignalR. But to counter that I created a class to act as a custom datatype.</p>
  
  <p><a href="https://github.com/aspnet/AspNetCore/issues/8853" rel="nofollow noreferrer">https://github.com/aspnet/AspNetCore/issues/8853</a></p>
  
  <p>But when i invoke the function via the client I get this error:</p>
  
  <pre><code>Failed to invoke hub method 'CreateRoom'.
  System.IO.InvalidDataException: Error binding arguments. Make sure that the types of the provided values match the types of the hub method being invoked. ---&gt; Newtonsoft.Json.JsonSerializationException: Error converting value 2 to type 'GameServer.Classes.CreateParams'. Path '[0]', line 1, position 15. ---&gt; System.ArgumentException: Could not cast or convert from System.Int64 to GameServer.Classes.CreateParams.
     at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) in /_/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:line 616
     at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) in /_/Src/Newtonsoft.Json/Utilities/ConvertUtils.cs:line 587
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 982
     --- End of inner exception stack trace ---
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 989
     at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) in /_/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs:line 167
     at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in /_/Src/Newtonsoft.Json/JsonSerializer.cs:line 907
     at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer) in /_/Src/Newtonsoft.Json/Linq/JToken.cs:line 2070
     at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.BindArguments(JArray args, IReadOnlyList`1 paramTypes)
     --- End of inner exception stack trace ---
     at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.BindArguments(JArray args, IReadOnlyList`1 paramTypes)
     at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.ParseMessage(Utf8BufferTextReader textReader, IInvocationBinder binder)
  </code></pre>
  
  <p>Here is my code.</p>
  
  <p>Beginning of the server side function.</p>
  
  <p><strong>GameHub.cs</strong></p>
  
  <pre><code>public async Task CreateRoom(CreateParams data)
          {
              Log.Information("CreateRoom is called with Game: {0}, RoomName: {1}", data.Game, data.RoomName);
  
              try
              {
                  int _MaxUsers = 2;
                  if (data.MaxUsers != null)
                  {
                      if (data.MaxUsers &lt;= 2)
                      {
                          _MaxUsers = 2;
                      }
                      else
                      {
                          _MaxUsers = (int)data.MaxUsers;
                      }
                  }
  </code></pre>
  
  <p>Custom class</p>
  
  <p><strong>CreateParams</strong></p>
  
  <pre><code>public class CreateParams
      {
          public string Game { get; set; }
          public string RoomName { get; set; }
          public int? MaxUsers { get; set; }
      }
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <pre><code>connection.invoke("CreateRoom", (gameType, lobbyName, maxUsers));
  </code></pre>
  
  <p>I don't know why this is happening.
  I hope you guys can help me.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried what you gave me but i get this error:</p>
  
  <pre><code>Source: System.Collections.Concurrent, Method: ThrowKeyNullException, Error: Value cannot be null.
  Parameter name: key, Stack Trace:    at System.Collections.Concurrent.ConcurrentDictionary`2.ThrowKeyNullException()
     at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue&amp; value)
     at System.Collections.Concurrent.ConcurrentDictionary`2.get_Item(TKey key)
     at GameServer.Hubs.GameHub.CreateRoom(CreateParams data) in projectlocation\GameServer\GameServer\Hubs\GameHub.cs:line 149
  2019-04-15 14:34:50.504 +02:00 [ERR] Failed to invoke hub method 'CreateRoom'.
  System.ArgumentNullException: Value cannot be null.
  Parameter name: key
     at System.Collections.Concurrent.ConcurrentDictionary`2.ThrowKeyNullException()
     at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue&amp; value)
     at System.Collections.Concurrent.ConcurrentDictionary`2.get_Item(TKey key)
     at GameServer.Hubs.GameHub.CreateRoom(CreateParams data) in projectlocation\GameHub.cs:line 149
     at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.ExecuteHubMethod(ObjectMethodExecutor methodExecutor, THub hub, Object[] arguments)
     at Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher`1.Invoke(HubMethodDescriptor descriptor, HubConnectionContext connection, HubMethodInvocationMessage hubMethodInvocationMessage, Boolean isStreamedInvocation)
  
  </code></pre>
  
  <p>I logged the response and all parameters are null</p>