2013-07-12 106 views
2

我使用SignalR 2.x具有以下配置一個非常基本的自託管的Web服務:SignalR和類型的對象

服務器:

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代碼來發送消息:

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。我在這裏錯過了什麼嗎?

+2

我想你的樣本客戶機,並將其發送的Json類型化 數據=%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 22%22%3A1%2C 22%22%3A%22首先+消息%22%7D –

+0

事實證明,問題是我通過'Notifier.NotifyAll()'調用'PersistentConnection'控制器。出於某種原因,它然後使用默認的JSonParser。我嘗試在'PersistentConnection'的'overrid Initialize'內設置'TypeNameHandling',但這沒有任何作用。 – dsfgsho

回答

2

雖然整個設置相當深奧,但對於那些有興趣的人來說,這是一個解決方案。

我認爲問題是由於GlobalHost.ConnectionManager.GetConnectionContext靜態造成的。通過這樣做,我想我在創建了一個PersistentConnection,然後WebService DependencyResolver被正確設置了(儘管我不確定這是爲什麼)。

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

不是:問題是由再次抓住ConnectionContext每個事件解決

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