I have different objects I would like to be able to load and save on a ASP.NET (IIS webserver) from my MONO client. So I have made them as Class objects.
As example I have [Score] which I will be using for producing a hiscore on a server.
Score.cs
using System.Runtime.Serialization;
[System.Serializable]
public class Score
{
public long Total;
public string Name;
public Score(){}
public Score(string name, long total)
{
Total = total;
Name = name;
}
}
This code I want to use as data structure (n-tier architecture) in both MONO as clientside structure and in my ASP.NET server I use it as serverside object too.
What I do is that I want to build a list-array like this:
List<Score> hiscores = new List<Score>();
hiscores.add (new Score("Player 1", 10000));
hiscores.add (new Score("Player 2", 20000));
hiscores.add (new Score("Player 3", 30000));
hiscores.add (new Score("Player 4", 40000));
... then I want to serialize it into a bytearray so I can POST it as binaryfile:
MemoryStream membytes = new MemoryStream();
BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(membytes, hiscores );
...
BUT - on the webserver, in ASP.NET/C# I get an error 500 telling me that it cannot load the assemble for deserializing the data???
HUH?! I've read someone suggesting to make the assemble as external? and then link it on both platforms, is that possible in my situation or am I on the wrong path somehow?
I can easily transfer a simle bytearray with values from 0x00 to 0xff to my ASP.NET and save it. But once I start using serialize/deserialize on my List objects and transfer them into a Byte[] things messes up?
QUESTION
Is deserialization not possible if the serializer isnt made on the same platform?
is it possible to place the dataobjects (business objects) in a DLL project and then link/use this on both platforms and that way gaining the same assemble value?
BinaryFormatter produces a non-interoperable binary format. Use it only if both the client and the server run the same version of the CLR. Otherwise you could use an interoperable format such as XML (XmlSerializer
) or JSON (JavaScriptSerializer
) to communicate between your client and server.
Your big mistake is using BinaryFormatter
for this. This has many issues.
- You should trust the data you deserialize. This is rarely the case in client-server applications.
- It uses some internal format, and I'm not sure how compatible that is between different versions of .net or even between .net and mono. Use a serializer with a well defined format.
Good alternatives for this use-case are Json or Xml serializers. Personally usually use Json.net, but .net has also a built in Json serializier.
BinaryFormatter
might be the right choice if you serialize a complex object graph, don't need versioning and only read it back on the same system, so you can trust the data. And even in that scenario I'd only use it because I know no better alternative, not because I like BinaryFormatter
.
Since you now stated that you transfer a lot of data I'd look into protobuf-net. It's a binary serializer that is very fast and produces compact data. But you should annotate all your properties with numeric IDs. It's author Marc Gravell is active here on SO, so you can expect good support.
Posting my own answer as I actually managed to get it working without extras
First of all, thanks for all the great suggestions for extra libraries, but my initial question was about if it was possible to make MONO and ASP.NET communicate through serialization.
The answer is: YES!
All I had to to was to build a seperate Class-project (DLL) where I put all the datastructures I need to serialize.
Then make it a reference on both platforms and voila, same DLL = same assembly and both platforms agrees that serialization and deserialization is no problem.
Note on binary result/traffic/space
I agree, the binary data seems to be a bit "overkill" compared to some of the other suggested plugins/libraries in here. But it IS possible without any extra libs.
That was my initial goal.
I have used Mike Talbot's Silverlight Serializer http://whydoidoit.com/2010/04/08/silverlight-serialization/
which is very lightweight and should work with both MONO and .NET