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?
PersistentConnectionthroughNotifier.NotifyAll()outside of the Controllers. For some reason it then uses the default JSonParser. I tried to set theTypeNameHandlinginside theoverrid Initializeof thePersistentConnection, but that does not have any effect.