private void SetupResolver(MessagePackHubProtocolOptions options)
        {
            // if counts don't match then we know users customized resolvers so we set up the options
            // with the provided resolvers
            if (options.FormatterResolvers.Count != SignalRResolver.Resolvers.Count)
            {
                var resolver = CompositeResolver.Create(Array.Empty <IMessagePackFormatter>(), (IReadOnlyList <IFormatterResolver>)options.FormatterResolvers);
                _msgPackSerializerOptions = MessagePackSerializerOptions.Standard.WithResolver(resolver);
                return;
            }

            for (var i = 0; i < options.FormatterResolvers.Count; i++)
            {
                // check if the user customized the resolvers
                if (options.FormatterResolvers[i] != SignalRResolver.Resolvers[i])
                {
                    var resolver = CompositeResolver.Create(Array.Empty <IMessagePackFormatter>(), (IReadOnlyList <IFormatterResolver>)options.FormatterResolvers);
                    _msgPackSerializerOptions = MessagePackSerializerOptions.Standard.WithResolver(resolver);
                    return;
                }
            }

            // Use optimized cached resolver if the default is chosen
            _msgPackSerializerOptions = MessagePackSerializerOptions.Standard.WithResolver(SignalRResolver.Instance);
        }
Beispiel #2
0
        public async Task CamelCasedJsonIsPreservedAcrossMassTransitBoundary()
        {
            Harness = new InMemoryTestHarness();

            var messagePackOptions = new MessagePackHubProtocolOptions();

            var jsonOptions = new JsonHubProtocolOptions();

            jsonOptions.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            var backplane1Harness = RegisterBusEndpoint("receiveEndpoint1");
            var backplane2Harness = RegisterBusEndpoint("receiveEndpoint2");

            await Harness.Start();

            try
            {
                backplane1Harness.SetHubLifetimeManager(CreateLifetimeManager(messagePackOptions, jsonOptions));
                backplane2Harness.SetHubLifetimeManager(CreateLifetimeManager());
                using (var client1 = new TestClient())
                    using (var client2 = new TestClient())
                    {
                        var manager1 = backplane1Harness.HubLifetimeManager;
                        var manager2 = backplane2Harness.HubLifetimeManager;

                        var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                        var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                        await manager1.OnConnectedAsync(connection1).OrTimeout();

                        await manager2.OnConnectedAsync(connection2).OrTimeout();

                        await manager1.SendAllAsync("Hello", new object[] { new TestObject {
                                                                                TestProperty = "Foo"
                                                                            } });

                        Assert.IsTrue(backplane2Harness.All.Consumed.Select <All <MyHub> >().Any());

                        var message = await client2.ReadAsync().OrTimeout() as InvocationMessage;

                        Assert.NotNull(message);
                        Assert.AreEqual("Hello", message.Target);
                        CollectionAssert.AllItemsAreInstancesOfType(message.Arguments, typeof(JObject));
                        var jObject = message.Arguments[0] as JObject;
                        Assert.NotNull(jObject);
                        var firstProperty = jObject.Properties().First();
                        Assert.AreEqual("testProperty", firstProperty.Name);
                        Assert.AreEqual("Foo", firstProperty.Value.Value <string>());
                    }
            }
            finally
            {
                await Harness.Stop();
            }
        }
        public void WithMessagePackHubProtocolSetsHubProtocolToMsgPackWithProvidedOptions()
        {
            var expectedOptions = new MessagePackHubProtocolOptions()
            {
                SerializationContext = new SerializationContext()
                {
                    SerializationMethod = SerializationMethod.Array
                }
            };

            Assert.True(new HubConnectionBuilder().WithMessagePackProtocol(expectedOptions).TryGetSetting<IHubProtocol>(HubConnectionBuilderDefaults.HubProtocolKey, out var hubProtocol));
            var actualProtocol = Assert.IsType<MessagePackHubProtocol>(hubProtocol);
            Assert.Equal(SerializationMethod.Array, actualProtocol.SerializationContext.SerializationMethod);
        }
        public async Task CamelCasedJsonIsPreservedAcrossRedisBoundary()
        {
            var server = new TestRedisServer();

            var messagePackOptions = new MessagePackHubProtocolOptions();

            messagePackOptions.SerializationContext.DictionarySerlaizationOptions.KeyTransformer = DictionaryKeyTransformers.LowerCamel;

            var jsonOptions = new JsonHubProtocolOptions();

            jsonOptions.PayloadSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            using (var client1 = new TestClient())
                using (var client2 = new TestClient())
                {
                    // The sending manager has serializer settings
                    var manager1 = CreateLifetimeManager(server, messagePackOptions, jsonOptions);

                    // The receiving one doesn't matter because of how we serialize!
                    var manager2 = CreateLifetimeManager(server);

                    var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                    await manager1.OnConnectedAsync(connection1).OrTimeout();

                    await manager2.OnConnectedAsync(connection2).OrTimeout();

                    await manager1.SendAllAsync("Hello", new object[] { new TestObject {
                                                                            TestProperty = "Foo"
                                                                        } });

                    var message = Assert.IsType <InvocationMessage>(await client2.ReadAsync().OrTimeout());
                    Assert.Equal("Hello", message.Target);
                    Assert.Collection(
                        message.Arguments,
                        arg0 =>
                    {
                        var dict = Assert.IsType <JObject>(arg0);
                        Assert.Collection(dict.Properties(),
                                          prop =>
                        {
                            Assert.Equal("testProperty", prop.Name);
                            Assert.Equal("Foo", prop.Value.Value <string>());
                        });
                    });
                }
        }
        public HubLifetimeManager <THub> CreateLifetimeManager(MessagePackHubProtocolOptions messagePackOptions = null, JsonHubProtocolOptions jsonOptions = null)
        {
            messagePackOptions = messagePackOptions ?? new MessagePackHubProtocolOptions();
            jsonOptions        = jsonOptions ?? new JsonHubProtocolOptions();

            var manager = new MassTransitHubLifetimeManager <THub>(
                Harness.Bus,
                Harness.Bus.CreateClientFactory(TimeSpan.FromSeconds(5)),
                new DefaultHubProtocolResolver(new IHubProtocol[]
            {
                new JsonHubProtocol(Options.Create(jsonOptions)),
                new MessagePackHubProtocol(Options.Create(messagePackOptions)),
            }, NullLogger <DefaultHubProtocolResolver> .Instance));

            return(manager);
        }
        protected MassTransitHubLifetimeManager <THub> CreateLifetimeManager(MessagePackHubProtocolOptions messagePackOptions = null,
                                                                             JsonHubProtocolOptions jsonOptions = null)
        {
            messagePackOptions ??= new MessagePackHubProtocolOptions();
            jsonOptions ??= new JsonHubProtocolOptions();

            var manager = new MassTransitHubLifetimeManager <THub>(
                new HubLifetimeManagerOptions <THub> {
                ServerName = $"{_prefix}_{Guid.NewGuid():N}"
            },
                new BusHubLifetimeScopeProvider(Harness.Bus),
                new DefaultHubProtocolResolver(
                    new IHubProtocol[] { new JsonHubProtocol(Options.Create(jsonOptions)), new MessagePackHubProtocol(Options.Create(messagePackOptions)) },
                    NullLogger <DefaultHubProtocolResolver> .Instance)
                );

            return(manager);
        }
Beispiel #7
0
        private void SetupResolver(MessagePackHubProtocolOptions options)
        {
            // if counts don't match then we know users customized resolvers so we set up the options
            // with the provided resolvers
            if (options.FormatterResolvers.Count != SignalRResolver.Resolvers.Count)
            {
                _resolver = new CombinedResolvers(options.FormatterResolvers);
                return;
            }

            for (var i = 0; i < options.FormatterResolvers.Count; i++)
            {
                // check if the user customized the resolvers
                if (options.FormatterResolvers[i] != SignalRResolver.Resolvers[i])
                {
                    _resolver = new CombinedResolvers(options.FormatterResolvers);
                    return;
                }
            }

            // Use optimized cached resolver if the default is chosen
            _resolver = SignalRResolver.Instance;
        }
    private RedisHubLifetimeManager <Hub> CreateLifetimeManager(TestRedisServer server, MessagePackHubProtocolOptions messagePackOptions = null, NewtonsoftJsonHubProtocolOptions jsonOptions = null)
    {
        var options = new RedisOptions()
        {
            ConnectionFactory = async(t) => await Task.FromResult(new TestConnectionMultiplexer(server))
        };

        messagePackOptions = messagePackOptions ?? new MessagePackHubProtocolOptions();
        jsonOptions        = jsonOptions ?? new NewtonsoftJsonHubProtocolOptions();
        return(new RedisHubLifetimeManager <Hub>(
                   NullLogger <RedisHubLifetimeManager <Hub> > .Instance,
                   Options.Create(options),
                   new DefaultHubProtocolResolver(new IHubProtocol[]
        {
            new NewtonsoftJsonHubProtocol(Options.Create(jsonOptions)),
            new MessagePackHubProtocol(Options.Create(messagePackOptions)),
        }, NullLogger <DefaultHubProtocolResolver> .Instance)));
    }
 public static IHubConnectionBuilder WithMessagePackProtocol(this IHubConnectionBuilder builder, MessagePackHubProtocolOptions options)
 {
     return(builder.WithHubProtocol(new MessagePackHubProtocol(Options.Create(options))));
 }
        private RedisHubLifetimeManager <MyHub> CreateLifetimeManager(TestRedisServer server, MessagePackHubProtocolOptions messagePackOptions = null, JsonHubProtocolOptions jsonOptions = null)
        {
            var options = new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer(server)
            };

            messagePackOptions = messagePackOptions ?? new MessagePackHubProtocolOptions();
            jsonOptions        = jsonOptions ?? new JsonHubProtocolOptions();

            return(new RedisHubLifetimeManager <MyHub>(
                       NullLogger <RedisHubLifetimeManager <MyHub> > .Instance,
                       Options.Create(options),
                       new DefaultHubProtocolResolver(new IHubProtocol[]
            {
                new JsonHubProtocol(Options.Create(jsonOptions)),
                new MessagePackHubProtocol(Options.Create(messagePackOptions)),
            }, NullLogger <DefaultHubProtocolResolver> .Instance)));
        }