2

I have a very basic self-hosted webservice using SignalR 2.x with the following configuration:

Server:

internal class WebService
{
    public void Configuration( IAppBuilder app )
    {
        var config = new HttpConfiguration { DependencyResolver = new ControllerResolver() };
        config.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
        config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
        config.Routes.MapHttpRoute( "Default", "{controller}/{id}", new { id = RouteParameter.Optional } );
        app.UseWebApi( config );
        app.MapConnection<EventDispatcher>( "", new ConnectionConfiguration { EnableCrossDomain = true } );

        GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => JsonSerializer.Create(new JsonSerializerSettings
                                            {
                                                TypeNameHandling = TypeNameHandling.All
                                            }));

        app.MapHubs();
    }
}

Server code to send a message:

public class Notifier
{
    static readonly IPersistentConnectionContext Context = GlobalHost.ConnectionManager.GetConnectionContext<EventDispatcher>();

    public static void NotifyAll( NotificationType type, object obj )
    {
        Context.Connection.Broadcast( ConstructEvent( type, obj ) );
    }

    public static object ConstructEvent( NotificationType type, object obj )
    {
        var notevent =  new { Event = type.ToString(), Data = obj };
        return notevent;
    }
}

Client:

void connect()
{
    var _eventHandler = new Connection(Address);
    _eventHandler.JsonSerializer.TypeNameHandling = TypeNameHandling.Objects;
    _eventHandler.Received += eventHandler_Received;
    _eventHandler.Start().Wait();
}

The web service nicely returns typed JSON, but the updates send by SignalR are plain JSON. Am I missing something here?

2
  • 2
    I tried your sample client and it sends Json Typed data=%7B%22%24type%22%3A%22%3C%3Ef__AnonymousType0%602%5B%5BSystem.Int32%2C+mscorlib%5D%2C%5BSystem.String%2C+mscorlib%5D%5D%2C+Microsoft.AspNet.SignalR.Client.Samples%22%2C%22type%22%3A1%2C%22value%22%3A%22first+message%22%7D Commented Jul 12, 2013 at 16:08
  • It turns out the problem is that I call the PersistentConnection through Notifier.NotifyAll() outside of the Controllers. For some reason it then uses the default JSonParser. I tried to set the TypeNameHandling inside the overrid Initializeof the PersistentConnection , but that does not have any effect. Commented Jul 16, 2013 at 16:07

1 Answer 1

2

Although the whole setup is rather esoteric, here's the solution for those interested.

I think the problem was caused by making the GlobalHost.ConnectionManager.GetConnectionContext static. By doing this I think I created a PersistentConnection before the WebService DependencyResolver was properly set (although I am not sure why this is). The problem is solved by grabbing the ConnectionContext again for each event:

public class Notifier
{
    public static void NotifyAll(NotificationType type, object obj)
    {
        var context = GlobalHost.ConnectionManager.GetConnectionContext<EventDispatcher>();
        var output = ConstructEvent(type, obj);
        context.Connection.Broadcast(output);
    }

    protected static object ConstructEvent(NotificationType type, object obj)
    {
        var notevent = new { Event = type.ToString(), Data = obj };
        return notevent;
    }
}

Rather than:

public class Notifier
{
    static readonly IPersistentConnectionContext Context = GlobalHost.ConnectionManager.GetConnectionContext<EventDispatcher>();

    public static void NotifyAll(NotificationType type, object obj)
    {
        var output = ConstructEvent(type, obj);
        Context.Connection.Broadcast(output);
    }

    protected static object ConstructEvent(NotificationType type, object obj)
    {
        var notevent = new { Event = type.ToString(), Data = obj };
        return notevent;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.