PHP Azure SDK 服务总线 returns 格式错误的响应

PHP Azure SDK Service Bus returns Malformed Response

我正在研究各种跟踪记录器,将日志消息请求推送到服务总线上的队列,稍后由工作者角色挑选,该工作者角色会将它们插入 table 存储。虽然 运行 在我的机器上工作得很好(因为我是唯一一个使用它的人),但是一旦我把它放在服务器上进行测试,它就会产生以下错误:

HTTP_Request2_MessageException: Malformed response: in D:\home\site\wwwroot\vendor\pear-pear.php.net\HTTP_Request2\HTTP\Request2\Adapter\Socket.php on line 1013
0   HTTP_Request2_Response->__construct('', true, Object(Net_URL2)) D:\home\site\wwwroot\vendor\pear-pear.php.net\HTTP_Request2\HTTP\Request2\Adapter\Socket.php:1013
1   HTTP_Request2_Adapter_Socket->readResponse()    D:\home\site\wwwroot\vendor\pear-pear.php.net\HTTP_Request2\HTTP\Request2\Adapter\Socket.php:139
2   HTTP_Request2_Adapter_Socket->sendRequest(Object(HTTP_Request2))    D:\home\site\wwwroot\vendor\pear-pear.php.net\HTTP_Request2\HTTP\Request2.php:939
3   HTTP_Request2->send()   D:\home\site\wwwroot\vendor\microsoft\windowsazure\WindowsAzure\Common\Internal\Http\HttpClient.php:262
4   WindowsAzure\Common\Internal\Http\HttpClient->send(Array, Object(WindowsAzure\Common\Internal\Http\Url))    D:\home\site\wwwroot\vendor\microsoft\windowsazure\WindowsAzure\Common\Internal\RestProxy.php:141
5   WindowsAzure\Common\Internal\RestProxy->sendContext(Object(WindowsAzure\Common\Internal\Http\HttpCallContext))  D:\home\site\wwwroot\vendor\microsoft\windowsazure\WindowsAzure\Common\Internal\ServiceRestProxy.php:86
6   WindowsAzure\Common\Internal\ServiceRestProxy->sendContext(Object(WindowsAzure\Common\Internal\Http\HttpCallContext))   D:\home\site\wwwroot\vendor\microsoft\windowsazure\WindowsAzure\ServiceBus\ServiceBusRestProxy.php:139
7   WindowsAzure\ServiceBus\ServiceBusRestProxy->sendMessage('<queuename>/mes…', Object(WindowsAzure\ServiceBus\Models\BrokeredMessage))    D:\home\site\wwwroot\vendor\microsoft\windowsazure\WindowsAzure\ServiceBus\ServiceBusRestProxy.php:155
⋮

我以前看过描述类似问题的帖子;即:

这意味着这是关于 PHP Azure 存储库的已知问题,其中允许的 HTTPS 连接数量有限。在更改要求之前,我直接访问 table 商店,运行 进入同样的问题,并按照第一个 link 描述的方式修复它。

问题是连接字符串中的服务总线端点,与 Table 存储(等)连接字符串端点不同,必须是 'HTTPS'。尝试将它与 'HTTP' 一起使用将 return 400 - 错误请求错误。

我想知道是否有人对可能的解决方法有任何想法。任何建议将不胜感激。

谢谢!

编辑(在 Gary Liu 的评论之后):

这是我用来将项目添加到队列的代码:

private function logToAzureSB($source, $msg, $severity, $machine)
{
    // Gather all relevant information
    $msgInfo = array(
        "Severity" => $severity,
        "Message" => $msg,
        "Machine" => $machine,
        "Source" => $source
        );
    // Encode it to a JSON string, and add it to a Brokered message.
    $encoded = json_encode($msgInfo);
    $message = new BrokeredMessage($encoded);
    $message->setContentType("application/json");
    // Attempt to push the message onto the Queue
    try
    {
        $this->sbRestProxy->sendQueueMessage($this->azureQueueName, $message);
    }
    catch(ServiceException $e)
    {
        throw new \DatabaseException($e->getMessage, $e->getCode, $e->getPrevious);
    }
}

这里,$this->sbRestProxy 是一个服务总线 REST 代理,在日志 class 初始化时设置。

在接收端,这是工人角色端的代码:

public override void Run()
{
     // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump.
     Client.OnMessage((receivedMessage) =>
     {
         try
         {
             // Pull the Message from the recieved object.
             Stream stream = receivedMessage.GetBody<Stream>();
             StreamReader reader = new StreamReader(stream);
             string message = reader.ReadToEnd();
             LoggingMessage mMsg = JsonConvert.DeserializeObject<LoggingMessage>(message);

              // Create an entry with the information given.
              LogEntry entry = new LogEntry(mMsg);

              // Set the Logger to the appropriate table store, and insert the entry into the table.
              Logger.InsertIntoLog(entry, mMsg.Service);
          }
          catch
          {
              // Handle any message processing specific exceptions here
          }
      });

      CompletedEvent.WaitOne();
}

其中 Logging Message 是一个简单对象,基本上包含与 PHP 中记录的消息相同的字段(用于 JSON 反序列化),LogEntry 是一个 TableEntity,其中包含这些字段也是如此,并且 Logger 是 Table Store Logger 的实例,在辅助角色的 OnStart 方法期间设置。

这是 Windows Azure PHP 的一个已知问题,很长一段时间都没有关注,也没有得到修复。从我发布这篇文章到现在,我们最终编写了一个单独的 API 网络服务用于日志记录,并让我们的 PHP 代码通过 cURL 向它发送 JSON 字符串,这有效作为临时解决方案就足够了。我们现在要离开 PHP,所以无论如何这不会成为一个问题。