BrokerProperties REST header 值未显示在 .NET 客户端代码中
BrokerProperties REST header values not showing up in .NET client code
我正在编写两小段 C# 代码。第一个是 client-side 便携式 Class 库。它所做的只是通过 Azure Service Bus REST API, using HttpClient.
向 Azure 服务总线主题发送消息。
我用我从客户端发送的值填充 BrokerProperties header on the REST call with valid JSON, and I expect that on the server side, when I receive the message through a subscription, that I'll get my instance of BrokeredMessage.Properties。
我在这方面遇到的一个问题是文档说要将 Content-Type 设置为 application/atom+xml;type=entry;charset=utf-8
,但即使我这样做了,我也会得到 application/json; charset=utf-8
,所以我'我只是使用 application/json
.
除此之外,据我所知,这是它应该做的。它创建客户端和请求消息,设置 headers,然后发送消息。我每次都会得到 201 Created。全部内容如下:
private async static void SendServiceBusMessage(Command command)
{
// Create the HttpClient and HttpRequestMessage objects
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, topicUri);
// Add the authorization header (CreateAuthToken does the SHA256 stuff)
request.Headers.Add("Authorization", CreateAuthToken(topicUri, authSasKeyName, authSasKey));
// Add the content (command is a normal POCO)
// I've tried application/atom+xml;type=entry;charset=utf-8, always see application/json in the request
request.Content = new StringContent(JsonConvert.SerializeObject(command), Encoding.UTF8, "application/json");
// Add the command name and SessionId as BrokeredMessage properties
var brokeredMessageProperties = new Dictionary<string, string>();
brokeredMessageProperties.Add("CommandName", command.GetType().Name);
brokeredMessageProperties.Add("SessionId", Guid.NewGuid().ToString());
// Add the BrokerProperties header to the request
request.Content.Headers.Add("BrokerProperties", JsonConvert.SerializeObject(brokeredMessageProperties));
// I've also tried adding it directly to the request, nothing seems different
// request.Headers.Add("BrokerProperties", JsonConvert.SerializeObject(brokeredMessageProperties));
// Send it
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
// Do some error-handling
}
}
这是它发送的 HTTP 请求的示例。将其与 Send Message 文档底部的示例进行比较...除了 Content-Type,它看起来(功能上)与我相同。
POST https://myawesomesbnamespace.servicebus.windows.net/commands/messages HTTP/1.1
Authorization: SharedAccessSignature sr=https%3A%2F%2Fmyawesomesbnamespace.servicebus.windows.net%2Fcommands%2Fmessages&sig=SomeValidAuthStuffHere
Content-Type: application/json; charset=utf-8
BrokerProperties: {"CommandName":"CreateJob_V1","SessionId":"94932660-54e9-4867-a020-883a9bb79fa1"}
Host: myawesomesbnamespace.servicebus.windows.net
Content-Length: 133
Expect: 100-continue
Connection: Keep-Alive
{"JobId":"6b76e7e6-9499-4809-b762-54c03856d5a3","Name":"Awesome New Job Name","CorrelationId":"47fc77d9-9470-4d65-aa7d-690b65a7dc4f"}
但是,当我在服务器上收到消息时,.Properties 是空的。这很烦人。
服务器代码如下所示。它只是获取一批消息并执行 foreach 循环。
private async Task ProcessCommandMessages()
{
List<BrokeredMessage> commandMessages = (await commandsSubscriptionClient.ReceiveBatchAsync(serviceBusMessageBatchSize, TimeSpan.FromMilliseconds(waitTime_ms))).ToList();
foreach (BrokeredMessage commandMessage in commandMessages)
{
// commandMessage.Properties should have CommandName and SessionId,
// like I sent from the client, but it's empty
// that's not good
if (commandMessage.Properties.ContainsKey("CommandName"))
{
string commandName = commandMessage.Properties["CommandName"] as string;
// Do some stuff
}
else
{
// This is bad, log an error
}
}
}
所以,我有点卡住了。谁能发现我在这里做错了什么?也许这是 Content-Type 的问题,有解决办法吗?
谢谢!
斯科特
美国华盛顿州西雅图
好的,终于回到这个话题了。我误解了(我认为文档不清楚)是任意属性不能通过 BrokerProperties header. Only named properties from the BrokeredMessage class 传递(如 SessionId、Label 等)将通过服务总线传递到服务器。
要在 BrokeredMessage.Properties 中显示属性,它们必须在请求中作为自定义 header 传递。所以,就我而言,
request.Headers.Add("CommandName", command.GetType().Name);
获取 CommandName 属性 以在消息通过服务总线传递后显示在服务器上。
为了传递 SessionId 值,我仍然想通过 BrokerProperties 传递它 header。
我正在编写两小段 C# 代码。第一个是 client-side 便携式 Class 库。它所做的只是通过 Azure Service Bus REST API, using HttpClient.
向 Azure 服务总线主题发送消息。我用我从客户端发送的值填充 BrokerProperties header on the REST call with valid JSON, and I expect that on the server side, when I receive the message through a subscription, that I'll get my instance of BrokeredMessage.Properties。
我在这方面遇到的一个问题是文档说要将 Content-Type 设置为 application/atom+xml;type=entry;charset=utf-8
,但即使我这样做了,我也会得到 application/json; charset=utf-8
,所以我'我只是使用 application/json
.
除此之外,据我所知,这是它应该做的。它创建客户端和请求消息,设置 headers,然后发送消息。我每次都会得到 201 Created。全部内容如下:
private async static void SendServiceBusMessage(Command command)
{
// Create the HttpClient and HttpRequestMessage objects
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, topicUri);
// Add the authorization header (CreateAuthToken does the SHA256 stuff)
request.Headers.Add("Authorization", CreateAuthToken(topicUri, authSasKeyName, authSasKey));
// Add the content (command is a normal POCO)
// I've tried application/atom+xml;type=entry;charset=utf-8, always see application/json in the request
request.Content = new StringContent(JsonConvert.SerializeObject(command), Encoding.UTF8, "application/json");
// Add the command name and SessionId as BrokeredMessage properties
var brokeredMessageProperties = new Dictionary<string, string>();
brokeredMessageProperties.Add("CommandName", command.GetType().Name);
brokeredMessageProperties.Add("SessionId", Guid.NewGuid().ToString());
// Add the BrokerProperties header to the request
request.Content.Headers.Add("BrokerProperties", JsonConvert.SerializeObject(brokeredMessageProperties));
// I've also tried adding it directly to the request, nothing seems different
// request.Headers.Add("BrokerProperties", JsonConvert.SerializeObject(brokeredMessageProperties));
// Send it
var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
{
// Do some error-handling
}
}
这是它发送的 HTTP 请求的示例。将其与 Send Message 文档底部的示例进行比较...除了 Content-Type,它看起来(功能上)与我相同。
POST https://myawesomesbnamespace.servicebus.windows.net/commands/messages HTTP/1.1
Authorization: SharedAccessSignature sr=https%3A%2F%2Fmyawesomesbnamespace.servicebus.windows.net%2Fcommands%2Fmessages&sig=SomeValidAuthStuffHere
Content-Type: application/json; charset=utf-8
BrokerProperties: {"CommandName":"CreateJob_V1","SessionId":"94932660-54e9-4867-a020-883a9bb79fa1"}
Host: myawesomesbnamespace.servicebus.windows.net
Content-Length: 133
Expect: 100-continue
Connection: Keep-Alive
{"JobId":"6b76e7e6-9499-4809-b762-54c03856d5a3","Name":"Awesome New Job Name","CorrelationId":"47fc77d9-9470-4d65-aa7d-690b65a7dc4f"}
但是,当我在服务器上收到消息时,.Properties 是空的。这很烦人。
服务器代码如下所示。它只是获取一批消息并执行 foreach 循环。
private async Task ProcessCommandMessages()
{
List<BrokeredMessage> commandMessages = (await commandsSubscriptionClient.ReceiveBatchAsync(serviceBusMessageBatchSize, TimeSpan.FromMilliseconds(waitTime_ms))).ToList();
foreach (BrokeredMessage commandMessage in commandMessages)
{
// commandMessage.Properties should have CommandName and SessionId,
// like I sent from the client, but it's empty
// that's not good
if (commandMessage.Properties.ContainsKey("CommandName"))
{
string commandName = commandMessage.Properties["CommandName"] as string;
// Do some stuff
}
else
{
// This is bad, log an error
}
}
}
所以,我有点卡住了。谁能发现我在这里做错了什么?也许这是 Content-Type 的问题,有解决办法吗?
谢谢!
斯科特
美国华盛顿州西雅图
好的,终于回到这个话题了。我误解了(我认为文档不清楚)是任意属性不能通过 BrokerProperties header. Only named properties from the BrokeredMessage class 传递(如 SessionId、Label 等)将通过服务总线传递到服务器。
要在 BrokeredMessage.Properties 中显示属性,它们必须在请求中作为自定义 header 传递。所以,就我而言,
request.Headers.Add("CommandName", command.GetType().Name);
获取 CommandName 属性 以在消息通过服务总线传递后显示在服务器上。
为了传递 SessionId 值,我仍然想通过 BrokerProperties 传递它 header。