public async Task CreateFromReceivedMessageCopiesProperties()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: true, enableSession: true))
            {
                var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
                var sender = client.GetSender(scope.QueueName);
                var msg    = new ServiceBusMessage();
                msg.Body          = GetRandomBuffer(100);
                msg.ContentType   = "contenttype";
                msg.CorrelationId = "correlationid";
                msg.Label         = "label";
                msg.MessageId     = "messageId";
                msg.PartitionKey  = "key";
                msg.Properties.Add("testProp", "my prop");
                msg.ReplyTo              = "replyto";
                msg.ReplyToSessionId     = "replytosession";
                msg.ScheduledEnqueueTime = DateTimeOffset.Now;
                msg.SessionId            = "key";
                msg.TimeToLive           = TimeSpan.FromSeconds(60);
                msg.To = "to";
                await sender.SendAsync(msg);

                var receiver = await client.GetSessionReceiverAsync(
                    scope.QueueName,
                    new ServiceBusReceiverOptions
                {
                    ReceiveMode = ReceiveMode.ReceiveAndDelete
                });

                var received = await receiver.ReceiveAsync();

                AssertMessagesEqual(msg, received);
                var toSend = ServiceBusMessage.CreateFrom(received);
                AssertMessagesEqual(toSend, received);

                void AssertMessagesEqual(ServiceBusMessage sentMessage, ServiceBusReceivedMessage received)
                {
                    Assert.IsTrue(received.Body.ToArray().SequenceEqual(sentMessage.Body.ToArray()));
                    Assert.AreEqual(received.ContentType, sentMessage.ContentType);
                    Assert.AreEqual(received.CorrelationId, sentMessage.CorrelationId);
                    Assert.AreEqual(received.Label, sentMessage.Label);
                    Assert.AreEqual(received.ContentType, sentMessage.ContentType);
                    Assert.AreEqual(received.CorrelationId, sentMessage.CorrelationId);
                    Assert.AreEqual(received.MessageId, sentMessage.MessageId);
                    Assert.AreEqual(received.PartitionKey, sentMessage.PartitionKey);
                    Assert.AreEqual((string)received.Properties["testProp"], (string)sentMessage.Properties["testProp"]);
                    Assert.AreEqual(received.ReplyTo, sentMessage.ReplyTo);
                    Assert.AreEqual(received.ReplyToSessionId, sentMessage.ReplyToSessionId);
                    Assert.AreEqual(received.ScheduledEnqueueTime.UtcDateTime.Second, sentMessage.ScheduledEnqueueTime.UtcDateTime.Second);
                    Assert.AreEqual(received.SessionId, sentMessage.SessionId);
                    Assert.AreEqual(received.TimeToLive, sentMessage.TimeToLive);
                    Assert.AreEqual(received.To, sentMessage.To);
                    Assert.AreEqual(received.ViaPartitionKey, sentMessage.ViaPartitionKey);
                }
            }
        }
        public async Task CanSendReceivedMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var client = new ServiceBusClient(
                    TestEnvironment.FullyQualifiedNamespace,
                    GetTokenCredential());
                await using var sender             = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString).GetSender(scope.QueueName);
                using ServiceBusMessageBatch batch = await sender.CreateBatchAsync();

                var messageCt = 10;
                IEnumerable <ServiceBusMessage> messages = AddMessages(batch, messageCt).AsEnumerable <ServiceBusMessage>();
                await sender.SendBatchAsync(batch);

                var receiver = client.GetReceiver(scope.QueueName, new ServiceBusReceiverOptions()
                {
                    ReceiveMode = ReceiveMode.ReceiveAndDelete
                });

                var remainingMessages = messageCt;
                IList <ServiceBusReceivedMessage> receivedMessages = new List <ServiceBusReceivedMessage>();
                while (remainingMessages > 0)
                {
                    foreach (var msg in await receiver.ReceiveBatchAsync(messageCt))
                    {
                        remainingMessages--;
                        receivedMessages.Add(msg);
                    }
                }
                foreach (ServiceBusReceivedMessage msg in receivedMessages)
                {
                    await sender.SendAsync(ServiceBusMessage.CreateFrom(msg));
                }

                var messageEnum = receivedMessages.GetEnumerator();

                remainingMessages = messageCt;
                while (remainingMessages > 0)
                {
                    foreach (var msg in await receiver.ReceiveBatchAsync(remainingMessages))
                    {
                        remainingMessages--;
                        messageEnum.MoveNext();
                        Assert.AreEqual(messageEnum.Current.MessageId, msg.MessageId);
                    }
                }
                Assert.AreEqual(0, remainingMessages);
            }
        }
        public async Task CanSendReceivedMessage()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                var client = new ServiceBusClient(
                    TestEnvironment.FullyQualifiedNamespace,
                    GetTokenCredential());
                await using var sender             = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString).GetSender(scope.QueueName);
                using ServiceBusMessageBatch batch = await sender.CreateBatchAsync();

                var messageCt = 10;
                IEnumerable <ServiceBusMessage> messages = AddMessages(batch, messageCt).AsEnumerable <ServiceBusMessage>();
                await sender.SendBatchAsync(batch);

                var receiver = client.GetReceiver(scope.QueueName, new ServiceBusReceiverOptions()
                {
                    ReceiveMode = ReceiveMode.PeekLock
                });
                var receivedMessages = await receiver.ReceiveBatchAsync(messageCt);

                foreach (ServiceBusReceivedMessage msg in receivedMessages)
                {
                    await sender.SendAsync(ServiceBusMessage.CreateFrom(msg));
                }

                int receivedMessageCount = 0;
                var messageEnum          = messages.GetEnumerator();

                foreach (var item in await receiver.ReceiveBatchAsync(messageCt))
                {
                    receivedMessageCount++;
                    messageEnum.MoveNext();
                    Assert.AreEqual(messageEnum.Current.MessageId, item.MessageId);
                }
                Assert.AreEqual(messageCt, receivedMessages.Count);
            }
        }