signalR and CustomTypeConverter

前端 未结 1 1620
别跟我提以往
别跟我提以往 2021-01-01 08:18

I\'m want to serialize derived type as explained here but for signalR I want to implement my custom converter derived from JsonConverter

I added it to signalR as f

相关标签:
1条回答
  • 2021-01-01 08:51

    This has been driving me nuts for hours. Thanks to halter73's comment I was able to come up with a workaround for the problem in my special situation: I need the pipeline to create interface instances for me which does not work out of the box. So the solution was the following:

    class CustomResolver : Microsoft.AspNet.SignalR.Hubs.DefaultParameterResolver
    {
        public override object ResolveParameter( Microsoft.AspNet.SignalR.Hubs.ParameterDescriptor descriptor, Microsoft.AspNet.SignalR.Json.IJsonValue value )
        {
            if( descriptor.ParameterType.IsInterface )
            {
                object TargetObject = <Create the object instance here>
                return value.ConvertTo( TargetObject.GetType() );
            }
            else
            {
                return value.ConvertTo( descriptor.ParameterType );
            }
        }
    }
    

    Then before you call

    WebApp.Start()
    

    Register this new parameter resolver with:

    GlobalHost.DependencyResolver.Register( typeof( Microsoft.AspNet.SignalR.Hubs.IParameterResolver ), () => new CustomResolver() );
    
    0 讨论(0)
提交回复
热议问题