Reputation: 627
I am recieving a Serialization Exception when attempting to parse the messaage recieved when subscribed to an Azure Service Bus topic. Anyone see what I am missing? I am using the Microsoft.ServiceBus.Samples.Messaging for the the Silverlight code.
I have a webservice with the following code:
public void PushCommand(Command command, int posLocationId)
{
var topicName = "topicName";
var topicClient = TopicClient.CreateFromConnectionString(ConnectionString, topicName);
try
{
var message = new BrokeredMessage("test");
topicClient.Send(message);
}
...
}
I have a SilverLightClient with the following code:
private void OnReceiveMessageCompleted(IAsyncResult result)
{
var subscriptionClient = (SubscriptionClient)result.AsyncState;
try
{
var message = subscriptionClient.EndReceive(result);
if (message != null)
{
String s = message.GetBody<string>();
}
// prep for next message
subscriptionClient.BeginReceive(this.OnReceiveMessageCompleted, subscriptionClient);
}
catch (Exception e)
{
//unknown error
}
}
SerializationException was caught There was an error deserializing the object of type System.String. Data at the root level is invalid. Line 1, position 1.
Stacktrace:
at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(XmlDictionaryReader reader)
at System.Runtime.Serialization.XmlObjectSerializer.ReadObject(Stream stream)
at Microsoft.Samples.ServiceBus.Messaging.BrokeredMessage.GetBody[T]()
at Common.SubscriptionManager.OnReceiveMessageCompleted(IAsyncResult result)
Upvotes: 1
Views: 3250
Reputation: 1860
You can serialize the string and send it as byte stream and at the receiving end you can deserialize to string. It will work.
Upvotes: 1
Reputation: 766
The issue isn't in your SL code, it's in your sender code. When sending the message, the default implementation uses a binary message serializer, but the SL implementation uses DataContractSerializer, which depends on XML formatted messages. To fix the issue, your message send needs to do something like this:
DataContractSerializer ser = new DataContractSerializer(typeof(string));
queueClient.Send(new BrokeredMessage("test", ser));
On Service Bus, the body content is pretty much a bag of bytes-- the sender and receiver need to agree on how those things will be encoded/decoded.
Upvotes: 7