Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

Case issues #73

Closed
rhegner opened this issue Dec 14, 2016 · 6 comments
Closed

Case issues #73

rhegner opened this issue Dec 14, 2016 · 6 comments

Comments

@rhegner
Copy link

rhegner commented Dec 14, 2016

Let's say we have a model like this on the server side:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

A Asp.Net Core Controller serializes this to a JSON with 'firstName' and 'lastName'. So all my client-side classes use this convention.

I realized that SignalR serializes this to 'FirstName' and 'LastName'. This means I can't re-use my client-side classes.

  • Is there an easy way to configure this behaviour in SignalR?
  • If not, what's the recommended workaround?
  • It would be nice if the default behaviour of SignalR was the same as for Controllers.
@rhegner
Copy link
Author

rhegner commented Dec 14, 2016

The workaround described here does not seem to work.

@rhegner
Copy link
Author

rhegner commented Dec 14, 2016

I found a very ugly workaround. In my hub before returning any object I call the following function on it:

private object ToCamelCaseObject(object obj)
{
	var json = JsonConvert.SerializeObject(obj, Formatting.None, SerializerSettings);
	return JsonConvert.DeserializeObject(json);
}
private readonly static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings() { ContractResolver = new Serialization.CamelCasePropertyNamesContractResolver() };

PS (not related to this issue but FYI): issue number 71 seems to be one of the biggest show-stoppers at the moment. Would be nice if arbitrary large objects can be transferred soon!

@radu-matei
Copy link

radu-matei commented Dec 14, 2016

@rhegner What version of SignalR are you using? The article you pointed to was based on the old SignalR repo you can find here - https://github.com/signalr/signalr

Still, could you provide something I can take a look at?

Thanks!

@moozzyk
Copy link
Contributor

moozzyk commented Dec 14, 2016

@rhegner - the problem is that at the moment we just new up JSonSerializer/JSonTextWriter without allowing passing any options (or maybe resolving these from the DI). We are discussing the best way to resolve #71.
Everything here is pretty much work in progress.

@muratg muratg added this to the 1.0.0 milestone Feb 3, 2017
@cruikshj
Copy link

cruikshj commented Mar 29, 2017

I worked around this issue by using my own IInvocationAdapter. It is simply a copy of JsonNetInvocationAdapter (from the repo) but it uses constructor injection to resolve the JsonSerializer rather than newing one up.

public class JsonInvocationAdapter : IInvocationAdapter
{
        public JsonInvocationAdapter(JsonSerializer serializer)
        {
            _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
        }

then registering as follows..

var jsonSerializerSettings = new JsonSerializerSettings();
jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonSerializerSettings.Formatting = Formatting.Indented;
jsonSerializerSettings.Converters.Add(new StringEnumConverter());
services.AddSingleton(JsonSerializer.Create(jsonSerializerSettings));

services.AddSingleton<JsonInvocationAdapter>();
services.AddSignalR(options =>
{
     options.RegisterInvocationAdapter<JsonInvocationAdapter>("json");
});

@davidfowl
Copy link
Member

Tracked by #261

@muratg muratg modified the milestones: 2.1.0, 2.1.0-preview1 Dec 11, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants