c# - SignalR 和类型化对象

标签 c# asp.net-web-api signalr

我有一个非常基本的自托管网络服务,使用 SignalR 2.x具有以下配置:

服务器:

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();
    }
}

发送消息的服务器代码:

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;
    }
}

客户:

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

Web 服务很好地返回类型化 JSON,但 SignalR 发送的更新是纯 JSON。我在这里遗漏了什么吗?

最佳答案

虽然整个设置相当深奥,但对于那些感兴趣的人来说,这里是解决方案。

我认为问题是由使 GlobalHost.ConnectionManager.GetConnectionContext 静态化引起的。通过这样做,我认为我在正确设置 WebService DependencyResolver 之前创建了一个 PersistentConnection (尽管我不确定这是为什么)。通过为每个事件再次获取 ConnectionContext 来解决该问题:

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;
    }
}

而不是:

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;
    }
}

关于c# - SignalR 和类型化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616876/

相关文章:

javascript - 尝试使用 Knockout ViewModel 实现 SignalR

c# - 具有多个参数的 SignalR 客户端

c# - 根据 MSDN,为什么 .NET 类 System.String 现在在三个不同的程序集中?

c# - 无法理解如何决定何时使用 Hidden() 以及何时使用 HiddenFor()

asp.net-mvc - 多个 PUT 操作,asp.net mvc 4 web api

c# - 如何清理 web api 过滤器中字符串参数的值?

c# - SignalR 和 c# 导致版本错误

c# - 单击更新按钮时 Datagridview 未更新

c# - 为 Nullable<T> 序列化生成 IL?

javascript - 无法通过webapi在浏览器angularjs中显示文件