我正在开发一个项目,我们希望通过Azure Service Bus(主题)发送消息。在这个例子中,我设置了一个C#Console应用程序和一个Java应用程序,用于在它们之间发送和接收消息。
从C#发送消息时,我在Java中收到一个格式奇怪的字符串,其中包含“schema.microsoft.com/2003/10/serialisation”。更改邮件内容类型没有任何区别。
当从Java向C#发送数据时,我得到一个例外。
发送/接收数据 - C#
private static void SendMessages()
{
topicClient = TopicClient.Create(TopicName);
List<BrokeredMessage> messageList = new List<BrokeredMessage>();
messageList.Add(CreateSampleMessage("1", "Test"));
Console.WriteLine("\nSending messages to topic...");
foreach (BrokeredMessage message in messageList)
{
while (true)
{
try
{
topicClient.Send(message);
}
catch (MessagingException e)
{
if (!e.IsTransient)
{
Console.WriteLine(e.Message);
throw;
}
else
{
HandleTransientErrors(e);
}
}
Console.WriteLine(string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
break;
}
}
topicClient.Close();
}
private static BrokeredMessage CreateSampleMessage(string messageId, string messageBody)
{
BrokeredMessage message = new BrokeredMessage(messageBody);
message.MessageId = messageId;
return message;
}
C#Stacktrace
{“反序列化System.String类型的对象时出错。文件意外结束。以下元素未关闭:。”}
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
at Microsoft.ServiceBus.Messaging.DataContractBinarySerializer.ReadObject(Stream stream)
at Microsoft.ServiceBus.Messaging.BrokeredMessage.GetBody[T](XmlObjectSerializer serializer)
at Microsoft.ServiceBus.Messaging.BrokeredMessage.GetBody[T]()
at Microsoft.Samples.MessagingWithTopics.program.ReceiveMessages() in d:\Downloads\Getting Started Messaging With Topics (1)\C#\MessagingWithTopics\program.cs:line 127
at Microsoft.Samples.MessagingWithTopics.program.Main(String[] args) in d:\Downloads\Getting Started Messaging With Topics (1)\C#\MessagingWithTopics\program.cs:line 39
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
答案 0 :(得分:0)
a)您不能使用.NET中的AMQP。只有这样你才能获得AMQP编码。将TransportType = Amqp附加到连接字符串。然后使用Apache Proton-J的JMS提供程序。
b)如果您使用Java中的HTTP,请尝试通过自序列化流(Stream参数化构造函数)或使用(对象,XmlObjectSerializer)参数化构造函数的DataContractSerializer明确构造消息。这将为您提供XML测试有效负载。默认情况下,我们使用NetDataContractSerializer,它产生一个紧凑的编码,但只是.NET。