Azure服务总线-数据传输问题
本文关键字:数据传输 问题 总线 服务 Azure | 更新日期: 2023-09-27 18:00:56
我正在进行一个项目,我们希望通过Azure服务总线(主题(发送消息。在这个例子中,我设置了一个C#控制台应用程序和一个Java应用程序,用于在它们之间发送和接收消息。
当从C#发送消息时,我在Java中得到一个格式奇怪的字符串,其中包含"schema.microsoft.com/2003/10/serialization"。更改消息内容类型没有什么区别。
当从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#堆栈跟踪
{"反序列化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()
a(您不能使用.NET中的AMQP。只有这样您才能获得AMQP编码。将TransportType=Amqp附加到连接字符串。然后使用Apache Proton-J的JMS提供程序。
b( 如果使用Java中的HTTP,请尝试通过自序列化流(stream参数化构造函数(或使用DataContractSerializer显式与(object,XmlObjectSerializer(参数化构造函数来构造消息。这将为您提供一个XML测试负载。默认情况下,我们使用NetDataContractSerializer,它产生紧凑的编码,但仅为.NET。