Azure Service Bus无法接收JSON正文
调试此代码时,应用程序停止。 没有显示错误消息或exception。
X计划:
client.OnMessage((message) => { using (var stream = message.GetBody()) using (var streamReader = new StreamReader(stream, Encoding.UTF8)) { var body = streamReader.ReadToEnd(); } }
下面我通过REST API发布JSON对象。
项目Y:
public void CreateMessage(T messageToSend, string queueAddress, WebClient webclient) { var apiVersion = "&api-version=2014-01"; var serializedMessage = JsonConvert.SerializeObject(messageToSend, Formatting.Indented); string receivedMessageBody = webclient.UploadString(queueAddress + "/messages" + "?timeout=60&" + apiVersion, "POST", serializedMessage); }
我可以看到在Azure门户中收到消息,因此没有身份validation问题。 同样早些时候,当我在BrokeredMessage的帮助下使用Azure SDK传递数据时,我能够按预期接收JSON对象。 但是,我需要使用REST API。
如图所示,MessageID就在那里,我也能够获得属性。 但我想阅读身体的内容。
收到的对象的图片
。
任何想法我怎么能得到身体?
根据你的描述,我检查了这个问题并按照发送消息将我的消息发送到服务总线队列。
对于Content-Type
to application/atom+xml;type=entry;charset=utf-8
,您的有效负载需要使用带有二进制XmlDictionaryWriter的DataContractSerializer进行序列化。 因此,您需要按如下方式构建有效负载:
定义您的对象并发送消息:
[DataContract] public class DemoMessage { [DataMember] public string Title { get; set; } } wc.Headers["Content-Type"] = "application/atom+xml;type=entry;charset=utf-8"; MemoryStream ms = new MemoryStream(); DataContractSerializer serializer = new DataContractSerializer(typeof(DemoMessage)); serializer.WriteObject(ms, new DemoMessage() { Title = messageBody }); wc.UploadString(sendAddress, "POST",System.Text.UTF8Encoding.UTF8.GetString(ms.ToArray()));
然后,您可以使用以下代码来接收消息:
var message = message.GetBody(new DataContractSerializer(typeof(DemoMessage)));
对于Content-Type to text/plain
或不指定,您可以按如下方式序列化有效负载:
var messageBody = JsonConvert.SerializeObject(new DemoMessage(){ Title = messageBody }, Newtonsoft.Json.Formatting.Indented); wc.UploadString(sendAddress, "POST", messageBody);
要接收消息,您可以使用以下代码:
using (var stream = message.GetBody()) { using (var streamReader = new StreamReader(stream, Encoding.UTF8)) { msg = streamReader.ReadToEnd(); var obj=JsonConvert.DeserializeObject(msg); } }
更新:
调试此代码时,应用程序停止。 没有显示错误消息或exception。
对于控制台应用程序,在配置client.OnMessage
,您需要使用Console.ReadKey()
或Console.ReadLine()
来阻止您的应用程序退出。 此外,您可以try-catch-throw
在client.OnMessage
try-catch-throw
处理您的处理以检索详细的错误消息以进行故障排除。
UPDATE2:
我刚刚在我的控制台应用程序中使用了以下代码与目标框架4.6.2并引用了WindowsAzure.ServiceBus.4.1.3。
static void Main(string[] args) { //send the message var wc = new WebClient(); wc.Headers["Authorization"] = createToken("https://brucesb.servicebus.windows.net/order", "RootManageSharedAccessKey", "{your-key}"); var messageBody = JsonConvert.SerializeObject(new DemoMessage() { Title = "hello world!!!!" }, Newtonsoft.Json.Formatting.Indented); wc.UploadString("https://brucesb.servicebus.windows.net/order/messages", "POST", messageBody); //receive the message QueueClient client = QueueClient.CreateFromConnectionString(connectionString, "order"); client.OnMessage(message => { using (var stream = message.GetBody()) { using (var streamReader = new StreamReader(stream, Encoding.UTF8)) { var msg = streamReader.ReadToEnd(); var obj = JsonConvert.DeserializeObject(msg); Console.WriteLine(msg); } } }); Console.WriteLine("Press any key to exit..."); Console.ReadLine(); } static string createToken(string resourceUri, string keyName, string key) { var expiry = (long)(DateTime.UtcNow.AddDays(1) - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds; string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry; HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign))); var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName); return sasToken; }
测试:
- DocumentDB中每个集合的单个或多个实体
- 是否可以通过编程方式扩展Azure应用服务
- 从Azure Service Fabric群集模拟10,000个Azure IoT Hub设备连接
- 在.NET安全实践中使用Thread.Abort()并处理ThreadAbortException?
- Azure webrole循环和“无法加载文件或程序集System.Web.Http 5.2.0.0”
- entity framework批量插入虚幻慢
- Azure Fluent API创建SQL Server时出错 – 缺少x-ms-request-id标头
- 如何在Azure Key Vault中序列化和反序列化PFX证书?
- 无法加载文件或程序集“Microsoft.AI.Web”或其依赖项之一。 该系统找不到指定的文件