从本地 windows 10 框连接到 azure 服务总线失败

Connecting to azure service bus from local windows 10 box failing

我有一个尝试写入服务总线的 HTTP 触发器/Azure 函数。当我尝试通过 POSTMAN 在本地触发时,这是我在 VSCODE:

中遇到的错误
[2022-02-25T17:47:27.426Z] Executed 'mytestmethod' (Failed, Id=61291e4d-92de-4306-93ba-c0902dbaae3b, Duration=96242ms)
[2022-02-25T17:47:27.428Z] System.Private.CoreLib: Exception while executing function: O3mWorkspaceNotifications. System.Private.CoreLib: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ErrorCode: TimedOut (ServiceCommunicationProblem). System.Private.CoreLib: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

所以我找到了这篇文章:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-troubleshooting-guide

并且我尝试添加一个防火墙规则来打开所有传入/传出到我的特定 IP 地址的内容。要获取 ip 地址,我 运行 在 powershell 中执行此命令:

PS C:\Users\me\.azure> tnc myresourcegroup-bus.servicebus.windows.net -port 5671
WARNING: TCP connect to (111.11.1.11 : 5671) failed
WARNING: Ping to 111.11.11.11 failed with status: TimedOut

ComputerName           : myresourcegroup-bus.servicebus.windows.net
RemoteAddress          : 111.11.11.11
RemotePort             : 5671
InterfaceAlias         : Ethernet
SourceAddress          : 10.111.11.111
PingSucceeded          : False
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded       : False

我在本地 windows 10 开发箱中添加了传入和传出 FW 规则。我添加了一个“自定义规则”,允许该特定 IP 地址的所有程序、所有端口。

然后我尝试远程登录到我的 azure 服务总线,但它在我面前爆炸了:(尝试通过 windows 10 上的 ubuntu 子系统进行远程登录)

admin@CAMXL0332ZPD:/mnt/c/Users/me$ telnet 111.11.11.11 5671
Trying 111.11.11.11...
telnet: Unable to connect to remote host: Resource temporarily unavailable

不确定还要检查什么。 任何提示将不胜感激。

编辑 1

这就是我的代码现在的样子:

public class mywidgetsclass {
       [FunctionName("widgetsNotifications")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "mywidgets/sbnotification")] HttpRequest req,
             [ServiceBus("widgets", ServiceBusEntityType.Queue, Connection = "WidgetsServiceBus")] IAsyncCollector<WidgetsNotification> outputQueue,
            ILogger log)
        {         
            log.LogInformation($"WidgetsNotificationPOST method invoked");

            var content = await new StreamReader(req.Body).ReadToEndAsync();
            log.LogInformation($"Received following payload: {content}");

            var widgetsNotification= JsonConvert.DeserializeObject<WidgetsNotification>(content);
            
            await outputQueue.AddAsync(widgetsNotification);
            var response = new {widgetsNotification, QueuedResult=true};
            return new OkObjectResult(response);
        }

  
  }  

您的网络环境似乎不允许端口 5671 和 5672 的流量。这通常可以通过配置 ServiceBusClient 使用使用端口 443(默认 HTTPS 端口)的网络套接字传输来解决:

var options = new ServiceBusClientOptions
{
    TransportType = ServiceBusTransportType.AmqpWebSockets
};

var client = new ServiceBusClient("<< CONNECTION STRING >>", options);

对于服务总线触发器和绑定的 Azure Functions 配置,使用 Microsoft.Azure.WebJobs.Extensions.ServiceBus 包的 v5.0+ 时,可以在 host.json 中指定传输。

{
  "extensions": {
    "serviceBus": {
      "transportType" : "amqpWebSockets"          
  }
}