MassTransit Azure ServiceBus强制连接使用HTTPS而不是AMQP

问题描述 投票:1回答:1

我们如何强制MassTransit使用HTTPS而不是AMQP连接到Azure Service Bus?

我们的应用程序位于企业防火墙后面。它需要从Azure主题发送/接收消息。我们宁愿在AMQP上使用HTTPS,以便更轻松地用于安全管理。

该应用具有以下设置

  • 。NET Framework 4.6.2
  • MassTransit.Azure.ServiceBus.Core 6..1.0
  • Microsoft.Azure.ServiceBus 4.1.1

根据Azure Service Bus文档,我们应该能够将传输类型设置为AmqpWebSockets,该传输类型应使用HTTPS进行通信。连接字符串如下所示:

Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=secret;TransportType=AmqpWebSockets

(ref:https://github.com/MicrosoftDocs/azure-docs/issues/14133

如果使用TopicClient中的Microsoft.Azure.ServiceBus 4.1.1,我们可以看到流量通过端口443。示例代码为

class Program
{
    public static async Task Main(string[] args)
    {
        var serviceBus = new ServiceBus();
        await serviceBus.SendMessageAsync();

        Console.ReadKey();
    }
}

public class ServiceBus
{
    public async Task SendMessageAsync()
    {
        const int numberOfMessages = 10;
        topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
        // Send messages.
        await SendMessagesAsync(numberOfMessages);

        Console.ReadKey();

        await topicClient.CloseAsync();
    }

    public async Task SendMessagesAsync(int numberOfMessagesToSend)
    {
        try
        {
            for (var i = 0; i < numberOfMessagesToSend; i++)
            {
                // Create a new message to send to the topic.
                string messageBody = $"Message {i}";
                var message = new Message(Encoding.UTF8.GetBytes(messageBody));

                // Write the body of the message to the console.
                Console.WriteLine($"Sending message: {messageBody}");

                // Send the message to the topic.
                await topicClient.SendAsync(message);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
        }
    }
}

但是,当我们使用具有TransportType=AmqpWebSockets的相同连接字符串时,MassTransit仍然使用AMQP端口5671

public class Order : IModel
{
    public int Id { get; set; }
    public double Amount { get; set; }
    public string Description { get; set; }
}


public static async Task Main(string[] args)
{
    var bus = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
    {
        cfg.Host(ConnectionString);
    });
    await bus.StartAsync();
    for (int i = 0; i < 100; i++)
    {
        var order = new Order() { Id = i, Amount = 20, Description = "Test order" };
        await bus.Publish(order);
    }
}

以下是Wireshark捕获的流量:enter image description here

有趣的是它使用端口443来处理前几个数据包,然后更改为端口5671

.net azure azureservicebus masstransit
1个回答
0
投票

找到解决方案,您可以在设置总线时设置传输类型:

var bus = Bus.Factory.CreateUsingAzureServiceBus(cfg =>
{
    cfg.Host(ConnectionString, hc => hc.TransportType = TransportType.AmqpWebSockets);
    cfg.PrefetchCount = 1;
    AddSubscriptionEndpoints(context, cfg);
});

Bingo,它现在使用端口443enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.