I have created an application where interprocess communication is done using sockets. The procedure starts when a client connects with the server that I created and sends a serialized message. This message, I serialize using Protobuf-net, using SerializeWithLengthPrefix and deserialize it using DeserializeWithLengthPrefix. The client sends messages to the server who deserializes it perfectly, but the same is not true in the case of server to client.
The main class is BaseMessage, which is abstract.
[Serializable, ProtoContract, ProtoInclude(5001, typeof(LogonMessage))]
abstract public class BaseMessage
{
public BaseMessage()
{
}
abstract public int MessageType { get; }
}
And LogonMessage implements the BaseMessage Class.
[Serializable, ProtoContract]
public class LogonMessage : BaseMessage
{
public LogonMessage()
{
}
[ProtoMember(1)]
public string Broker { get; set; }
[ProtoMember(2)]
public int ClientType { get; set; }
public override int MessageType
{
get { return 1; }
}
}
After the initial handshake, the client requests some service serialized with the help of protobuf-net and the local server at my end serves it by requesting data from another server on the web. This message transfer from the client to the server is done flawlessly.
When my server receives the data from the web server, it serializes it and sends the data to the client. But this time, when I try to deserialize the data on the client-side using the same procedure, I get the following exception: "No parameterless Constructor found for BaseMessage"
I deserialize using the following line of code(this is where the exception occurs).
BaseMessage baseMessage = Serializer.DeserializeWithLengthPrefix<BaseMessage>(networkStream, PrefixStyle.Base128);
And this is how the message was serialized on the server.
Serializer.SerializeWithLengthPrefix(networkStream, baseMessage, PrefixStyle.Base128);
The NetworkStream used at the start of the connection between the client and server is stored in an object and that object is stored in a dictionary. I pick out the same NetworkStream from that object in the dictionary and use it to send serialized data to the client(from the server). But the above mentioned problem occurs. Any help?
Thanks in advance...
That should work fine in any v2 release; 2.0.0.480 is the currently advertised download on NuGet, but 2.0.0.580 is also available. I've checked both 1.0.0.280 and 2.0.0.480, and neither of them show this symptom, so I'm guessing you're using a different build. My advice, therefore, is: make sure you're on one of those two (or higher, where available).
For info, you don't need [Serializable]
for protobuf-net, but it doesn't hurt either. Also, your BaseMessage
constructor should probably be protected
(public
doesn't really make sense on the constructor of an abstract
type). But since the compiler does all that for you automatically, you can simplify:
[ProtoContract, ProtoInclude(5001, typeof(LogonMessage))]
abstract public class BaseMessage
{
abstract public int MessageType { get; }
}
[ProtoContract]
public class LogonMessage : BaseMessage
{
[ProtoMember(1)]
public string Broker { get; set; }
[ProtoMember(2)]
public int ClientType { get; set; }
public override int MessageType
{
get { return 1; }
}
}
Other thoughts: 5001
is a bit on the high-side; you will get better efficiency from low-value include-numbers. 1
leaps to mind. They don't have to be universally unique: just unique inside that type.
Just to add, I was trying to deserialize a MemoryStream that was not seeked to the origin and got the "No parameterless Constructor found for BaseObject" exception even if the class did have a parameterless constructor.
var tdp = new DerivedObject("Test");
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms,tdp);
//was missing this line
ms.Seek(0, SeekOrigin.Begin);
var tdp2 = Serializer.Deserialize<DerivedObject>(ms);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With