Пример #1
0
        public async Task SendJsonBodyMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var client     = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
                var sender     = client.CreateSender(scope.QueueName);
                var serializer = new JsonObjectSerializer();
                var testBody   = new TestBody
                {
                    A = "text",
                    B = 5,
                    C = false
                };
                var body = BinaryData.FromSerializable(testBody, serializer);
                var msg  = new ServiceBusMessage(body);

                await sender.SendMessageAsync(msg);

                var receiver = client.CreateReceiver(scope.QueueName);
                var received = await receiver.ReceiveMessageAsync();

                var receivedBody = received.Body.Deserialize <TestBody>(serializer);
                Assert.AreEqual(testBody.A, receivedBody.A);
                Assert.AreEqual(testBody.B, receivedBody.B);
                Assert.AreEqual(testBody.C, receivedBody.C);
            }
        }
Пример #2
0
        public void GenericCreateThrowsOnNullSerializer()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };

            Assert.That(
                () => BinaryData.FromSerializable(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
            Assert.That(
                async() => await BinaryData.FromSerializableAsync(payload, null),
                Throws.InstanceOf <ArgumentNullException>());
        }
Пример #3
0
        public void AsThrowsOnNullSerializer()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();
            var data       = BinaryData.FromSerializable(payload, serializer);

            Assert.That(
                () => data.Deserialize <TestModel>(null),
                Throws.InstanceOf <ArgumentNullException>());
            Assert.That(
                async() => await data.DeserializeAsync <TestModel>(null),
                Throws.InstanceOf <ArgumentNullException>());
        }
Пример #4
0
        public async Task CanCreateBinaryDataFromCustomType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();

            await AssertData(BinaryData.FromSerializable(payload, serializer));
            await AssertData(await BinaryData.FromSerializableAsync(payload, serializer));

            async Task AssertData(BinaryData data)
            {
                Assert.AreEqual(payload.A, data.Deserialize <TestModel>(serializer).A);
                Assert.AreEqual(payload.B, data.Deserialize <TestModel>(serializer).B);
                Assert.AreEqual(payload.C, data.Deserialize <TestModel>(serializer).C);
                Assert.AreEqual(payload.A, (await data.DeserializeAsync <TestModel>(serializer)).A);
                Assert.AreEqual(payload.B, (await data.DeserializeAsync <TestModel>(serializer)).B);
                Assert.AreEqual(payload.C, (await data.DeserializeAsync <TestModel>(serializer)).C);
            }
        }
Пример #5
0
        public async Task AsThrowsExceptionOnIncompatibleType()
        {
            var payload = new TestModel {
                A = "value", B = 5, C = true
            };
            var serializer = new JsonObjectSerializer();

            AssertData(BinaryData.FromSerializable(payload, serializer));
            AssertData(await BinaryData.FromSerializableAsync(payload, serializer));

            void AssertData(BinaryData data)
            {
                Assert.That(
                    () => data.Deserialize <string>(serializer),
                    Throws.InstanceOf <Exception>());
                Assert.That(
                    async() => await data.DeserializeAsync <string>(serializer),
                    Throws.InstanceOf <Exception>());
            }
        }