3

让这个工作有问题。

我希望我的集线器处理参数中的泛型。所以参数类型是一个抽象类,它将由一个具体的泛型类型实现——因为我不可能创建一个泛型方法。像这样:

         public void Process(MyAbstractClass arg)

但是,当我告诉客户端序列化类型信息时,注册失败。

这是客户端 (SignalR WinRT) 序列化配置。

        _hubConnecton.JsonSerializer = new JsonSerializer()
                                       {
                                           PreserveReferencesHandling = PreserveReferencesHandling.Objects,
                                           TypeNameHandling = TypeNameHandling.Objects,
                                           TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple
                                       };

这是我从提琴手跟踪得到的错误:

[JsonSerializationException]: Could not load assembly 'Microsoft.AspNet.SignalR.Client'.
   at Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key)
   at Newtonsoft.Json.Utilities.ThreadSafeStore`2.Get(TKey key)
   at Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id)
[JsonSerializationException]: Error resolving type specified in JSON 'Microsoft.AspNet.SignalR.Client.Hubs.HubRegistrationData, Microsoft.AspNet.SignalR.Client'. Path '[0].$type', line 1, position 111.
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection wrappedList, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Deserialize(TextReader reader, Type objectType)
   at Microsoft.AspNet.SignalR.Json.JsonNetSerializer.Parse(TextReader reader, Type targetType)
   at Microsoft.AspNet.SignalR.Json.JsonSerializerExtensions.Parse[T](IJsonSerializer serializer, String json)
   at Microsoft.AspNet.SignalR.Hubs.HubDispatcher.AuthorizeRequest(IRequest request)
   at Microsoft.AspNet.SignalR.PersistentConnection.Authorize(IRequest request)
   at Microsoft.AspNet.SignalR.Owin.CallHandler.Invoke(IDictionary`2 environment)
   at Microsoft.AspNet.SignalR.Owin.Handlers.HubDispatcherHandler.Invoke(IDictionary`2 environment)
   at Microsoft.Owin.Host.SystemWeb.OwinCallContext.Execute()
   at Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object extraData)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow()
   at Microsoft.Owin.Host.SystemWeb.CallContextAsyncResult.End(IAsyncResult result)
   at Microsoft.Owin.Host.SystemWeb.OwinHttpHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

显然,它在注册期间发送类型信息,导致上述错误被抛出

GET http://127.0.0.1:81/signalr/connect?transport=serverSentEvents&connectionToken=JKbyIAOOvt5BYGu_Ly2Yk9dNYVR7B180TobrrJpc5BYN5-DxdSwXs6i71pF0nJrLC3C7kaB-4VwD8Lu76vgVbIoWLE5Ux42GhJOJ_REslxuvo0bcCkbvf3rfki3Rk6TJ0&connectionData=[%7B%22$id%22:%221%22,%22$type%22:%22Microsoft. AspNet.SignalR.Client.Hubs.HubRegistrationData,%20Microsoft.AspNet.SignalR.Client%22,%22Name%22:%22BusGatewayHub%22%7D] HTTP/1.1

如果我将以下行更改TypeNameHandling = TypeNameHandling.ObjectsTypeNameHandling = TypeNameHandling.Auto,则会收到一个错误,抱怨MyAbstractClass无法实例化,因为它是抽象类型。

似乎我需要手动处理序列化,但如果可以的话,我宁愿避免这样做。

想法?

4

5 回答 5

3

这可以做到但并不容易——SignalR 团队中的某个人一定一直在努力使扩展解析例程几乎不可能。

我看到了一堆 JSonSerializer 实例化,而不是提供已经在 GlobalConfig 中注册的那些。

无论如何,这里是如何做到的:

在客户端,实现 IHttpClient。这种实施将从消息信封中去除类型信息。我们不需要在信封上保留类型信息,就我们而言,一个信封与另一个相同。重要的是内容类型。加上 WinRT 的包络引用了与标准框架不兼容的 WinRT 框架。

public class PolymorphicHttpClient : IHttpClient
{
    private readonly IHttpClient _innerClient;

    private Regex _invalidTypeDeclaration = new Regex(@"""?\$type.*?:.*?"".*?SignalR\.Client.*?"",?");

    public PolymorphicHttpClient(IHttpClient innerClient)
    {
        _innerClient = innerClient;
    }

    public Task<IResponse> Get(string url, Action<IRequest> prepareRequest)
    {
        url = _invalidTypeDeclaration.Replace(url, "");
        return _innerClient.Get(url, prepareRequest);
    }

    public Task<IResponse> Post(string url, Action<IRequest> prepareRequest, IDictionary<string, string> postData)
    {
        if (postData != null)
        {
            var postedDataDebug = postData;
                //TODO: check out what the data looks like and strip out irrelevant type information.
            var revisedData = postData.ToDictionary(_ => _.Key,
                                                    _ =>
                                                    _.Value != null
                                                        ? _invalidTypeDeclaration.Replace(_.Value, "")
                                                        : null);
            return _innerClient.Post(url, prepareRequest, revisedData);
        }

        return _innerClient.Post(url, prepareRequest, null);
    }
}

您想在客户端开始这样的连接(在我的情况下是一个应用商店应用程序)

 _hubConnecton.Start(new AutoTransport(new PolymorphicHttpClient(new DefaultHttpClient())))

我希望这已经足够了,但是在服务器端,内置的解析器是一团糟,所以我也不得不一起“破解”它。

你想实现 IJsonValue。原始实现创建了一个不尊重您的配置的 JSonSerializer 新实例。

 public class SerializerRespectingJRaw : IJsonValue
{
    private readonly IJsonSerializer _jsonSerializer;
    private readonly JRaw _rawJson;

    public SerializerRespectingJRaw(IJsonSerializer jsonSerializer, JRaw rawJson)
    {
        _jsonSerializer = jsonSerializer;
        _rawJson = rawJson;
    }

    public object ConvertTo(Type type)
    {
        return _jsonSerializer.Parse<object>(_rawJson.ToString());            
    }

    public bool CanConvertTo(Type type)
    {
        return true;
    }
}

然后你想创建你自己的解析器。请注意反射黑客,您可能希望将类型更改为任何版本。SignalR 你有。这也是为什么我说写这个部分的人一定非常讨厌 OO,因为所有的模块都是内部的——这使得它们很难扩展。

public class PolymorphicHubRequestParser : IHubRequestParser
{
    private readonly IJsonSerializer _jsonSerializer;
    private JsonConverter _converter;

    public PolymorphicHubRequestParser(IJsonSerializer jsonSerializer)
    {
        _converter =
            (JsonConverter) Type.GetType(
                "Microsoft.AspNet.SignalR.Json.SipHashBasedDictionaryConverter, Microsoft.AspNet.SignalR.Core, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
                .GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
                .Single(_ => !_.GetParameters().Any())
                .Invoke(null);

        _jsonSerializer = jsonSerializer;
    }

    private IDictionary<string, object> GetState(HubInvocation deserializedData)
    {
        if (deserializedData.State == null)
            return (IDictionary<string, object>)new Dictionary<string, object>();
        string json = ((object)deserializedData.State).ToString();
        if (json.Length > 4096)
            throw new InvalidOperationException("Maximum length exceeded.");
        JsonSerializerSettings settings = new JsonSerializerSettings();
        settings.Converters.Add(_converter);
        return JsonSerializerExtensions.Parse<IDictionary<string, object>>((IJsonSerializer)new JsonNetSerializer(settings), json);
    }

    public HubRequest Parse(string data)
    {
        var deserializedInvocation = new JsonNetSerializer().Parse<HubInvocation>(data);
        var secondPass = new HubRequest()
                             {
                                 Hub = deserializedInvocation.Hub,
                                 Id = deserializedInvocation.Id,
                                 Method = deserializedInvocation.Method,
                                 State = GetState(deserializedInvocation),
                                 ParameterValues =
                                     deserializedInvocation.Args.Select(
                                         _ => new SerializerRespectingJRaw(_jsonSerializer, _))
                                                           .Cast<IJsonValue>()
                                                           .ToArray()
                             };
        return secondPass;
    }

    private class HubInvocation
    {
        [JsonProperty("H")]
        public string Hub { get; set; }

        [JsonProperty("M")]
        public string Method { get; set; }

        [JsonProperty("I")]
        public string Id { get; set; }

        [JsonProperty("S")]
        public JRaw State { get; set; }

        [JsonProperty("A")]
        public JRaw[] Args { get; set; }
    }
}

现在一切就绪,您想使用以下覆盖启动您的 SignalR 服务。容器是您向主机注册的任何 DI。在我的情况下,容器是IUnityContainer.

        //Override the defauult json serializer behavior to follow our default settings instead.
        container.RegisterInstance<IJsonSerializer>(
            new JsonNetSerializer(Serialization.DefaultJsonSerializerSettings));
        container.RegisterType<IHubRequestParser, PolymorphicHubRequestParser>();
于 2013-10-06T21:11:41.633 回答
1

我认为您最好为 JSON 序列化创建一个中间类型,其中包含手动重新生成具体类型所需的所有数据。

另一种选择是为实现的每个具体类型创建具有不同名称(未重载MyAbstractClass)的 N 个方法。然后每个方法可以简单地将其参数传递给您Process接受抽象类型的方法。您必须小心以正确的类型调用正确的集线器方法,但它可能会起作用。

于 2013-10-03T23:28:55.880 回答
0

或者...您可以改用连接Received事件,返回一个字符串。然后,使用 JObject 解析它,确定它的类型并相应地反序列化它。

于 2014-11-22T04:24:48.047 回答
0
        RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(data);

    A msgBase = rootObject.A[0];

    if (msgBase.CommandName == "RequestInfo")
    {
        RootObjectRequestInfo rootObjectRequestInfo = JsonConvert.DeserializeObject<RootObjectRequestInfo>(data);
        RequestInfo requestInfo = rootObjectRequestInfo.RequestInfo[0];

        ConnectionID = requestInfo.ConnectionID;
    }
    else if (msgBase.CommandName == "TalkWord")
    {
        RootObjectTalkWord rootObjectTalkWord = JsonConvert.DeserializeObject<RootObjectTalkWord>(data);
        TalkWord talkWord = rootObjectTalkWord.TalkWord[0];

        textBoxAll.Text += talkWord.Word + "\r\n";
    }
于 2017-05-19T07:28:35.630 回答
0

有一个简单的解决方案,只需几行代码,但它需要一个丑陋的反射黑客。好处是该解决方案适用于序列化和反序列化,即向客户端发送数据并从中接收数据。

TypeNameHandling.Auto在客户端使用允许发送和接收的完全多态性。

var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto };
GlobalHost.DependencyResolver.Register(typeof(JsonSerializer), () => JsonSerializer.CreateDefault(settings));
GlobalHost.DependencyResolver.Register(typeof(IParameterResolver), () => new CustomResolver());

class CustomResolver : DefaultParameterResolver
{
    public override object ResolveParameter(ParameterDescriptor descriptor, Microsoft.AspNet.SignalR.Json.IJsonValue value)
    {
        var _value = (string)value.GetType().GetField("_value", System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(value);
        var obj = JsonConvert.DeserializeObject(_value, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto });
        return obj;
    }
}
于 2018-04-05T14:47:43.007 回答