I'm trying to serialize my MMF to file and here is the code:
class MMF {
private const string filename = #"c:\NFS";
private long offset = 0;
private long length = 194;
private byte[] buffer;
public MMF() {
using (var mmf =
MemoryMappedFile.CreateFromFile(filename, FileMode.OpenOrCreate, null, offset + length, MemoryMappedFileAccess.ReadWriteExecute)) {
using (var accessor = mmf.CreateViewAccessor(offset, length, MemoryMappedFileAccess.ReadWriteExecute)) {
buffer = new byte[194];
/*FS fs = new FS();
fs.Files[0].Path = "test";
accessor.WriteArray<byte>(0, buffer, 0, (int)length);*/
accessor.ReadArray<byte>(0, buffer, 0, (int)length);
FS fs = (FS)ToObject(buffer);
Console.WriteLine(fs.Files[0].Path);
}
}
}
private byte[] ToByteArray(object source) {
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream()) {
formatter.Serialize(stream, source);
return stream.ToArray();
}
}
private object ToObject(byte[] source) {
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream(source)) {
formatter.Deserialize(stream);
return stream;
}
}
}
On deserialization part I'm getting error:
An unhandled exception of type
'System.Runtime.Serialization.SerializationException' occurred in
mscorlib.dll
Additional information: A binary stream "0" does not contain a valid
binary header BinaryHeader. Possible causes: invalid stream or object
version change between serialization and deserialization.
How to Deserialize the file properly? Where is my mistake?
thank you
You might want to let MMF implement ISerializable and implement the GetObjectData method.
Related
private byte[] ToByteArray(Message msg)
{
if(msg == null)
{
return null;
}
BinaryFormatter bf = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, msg);
return ms.ToArray();
}
}
private Message ToMessageObject(Byte[] bytes)
{
if(bytes == null)
{
return null;
}
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(bytes))
{
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;
Message msg = (Message)bf.Deserialize(ms);
return msg;
}
}
I am using the two above methods for serialization and deserialization, but it keeps on throwing the error during deserializaion.
system.runtime.serialization.serializationexception: 'end of stream encountered before parsing was completed.'
My message class has the "Serializable" attribute.
One of the things I noticed is when the text is small, like two words, it deserializes fine but when it contains a lot of characters, close to 100, that's when I get that error. I have been checking other solutions()frome here and other places to this problem but none seems to work for me.
From the comments, I was able to figure out that the problem was caused by the initialization of the byte array,
var responseStream = client.GetStream()
var bytes = new byte[1024];
if (responseStream.DataAvailable)
{
await responseStream.ReadAsync(bytes, 0, bytes.Length);
var responseMessage =ToMessageObject(bytes);
messages.Add(responseMessage);
}
Initially, I set the bytes array length to 256. The error was thrown anytime I had to deserialize a byte array of length greater than 256. So, I am wondering if it's possible to use a dynamic array when reading from a stream.
I have a Streaming WCF service. It receives a stream of a serialized class called ContentObjectData. The bytes I receive in from the stream I have temporarily placed in an ArrayList as I don't know how big the Stream is and to be honest I don't know what to do with them anyway.
The ContentObjectData Class:
[Serializable]
public class ContentObjectData
{
string Hash { get; set; }
string Data { get; set; }
string FileName { get; set; }
string DisplayImage { get; set; }
}
This is the Service's Method that receives the stream from the client.
[OperationContract]
public void SendContentObject(Stream data)
{
ArrayList alBytes = new ArrayList();
Console.WriteLine("Client connected");
int count;
byte[] buffer = new byte[4096];
while ((count = data.Read(buffer, 0, buffer.Length)) > 0)
{
alBytes.AddRange(buffer);
}
long i = alBytes.Count;
data.Close();
}
At this moment in time I am sending an Image for testing using the following methods:
private void btnLoadImage_Click(object sender, EventArgs e)
{
DialogResult dr = OFD.ShowDialog();
if (dr == DialogResult.OK)
{
foreach (string filename in OFD.FileNames)
{
try
{
ContentObject co = new ContentObject();
co.Data = LoadFile(filename);
co.Hash = Hashing.HashString(co.Data);
co.DisplayImage = co.Data;
co.FileName = co.Hash;
Stream stream = SerializeToStream(co);
SendContentObject(stream);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
private void SendContentObject(Stream stream)
{
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
// TimeSpan.MaxValue is interpreted with a rounding error, so use 24 days instead
binding.SendTimeout = TimeSpan.FromDays(24);
binding.TransferMode = TransferMode.Streamed;
ChannelFactory<RaptorStreamingHost> factory = new ChannelFactory<RaptorStreamingHost>(
binding, new EndpointAddress("net.tcp://ccs-labs.com:804/"));
RaptorStreamingHost service = factory.CreateChannel();
service.SendContentObject(stream);
((IClientChannel)service).Close();
}
private string LoadFile(string filename)
{
return Hashing.BytesToString(File.ReadAllBytes(filename));
}
public static Stream SerializeToStream(object objectType)
{
MemoryStream stream = new MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, objectType);
stream.Position = 0L; // REMEMBER to reset stream or WCF will just send the stream from the end resulting in an empty stream!
return (Stream)stream;
}
I have this to DeSerialize but it doesn't make much sense to me:
public static object DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object objectType = formatter.Deserialize(stream);
return objectType;
}
How do I convert the ArrayList of Bytes (I guess DeSerialize them) into a New ContentObject?
Update One
Ah So close!
Ok in this method
public static ContentObjectData DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
ContentObjectData objectType = (ContentObjectData)formatter.Deserialize(stream);
return objectType;
}
I have a problem. The Deserializer can not find the ContentObjectData because it is looking in the client's Namespace for ContentObjectData and not this Host's namespace.
Firstly, don't use an ArrayList to store your bytes. Since ArrayList is non-generic each individual byte will be boxed and a pointer to the byte saved in the array, which will use 5 (32 bit) or 9 (64 bit) times more memory than necessary.
Instead, you can copy the Stream to a local MemoryStream, then save the underlying byte [] array:
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
[OperationContract]
public void SendContentObject(Stream data)
{
Console.WriteLine("Client connected");
var memoryStream = new MemoryStream();
using (data)
CopyStream(data, memoryStream);
byte [] alBytes = memoryStream.ToArray();
}
Then later you can turn the byte [] array back to a MemoryStream for deserialization:
public static object DeserializeFromStream(byte [] allBytes)
{
using (var stream = new MemoryStream(allBytes))
return DeserializeFromStream(stream);
}
public static object DeserializeFromStream(MemoryStream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object objectType = formatter.Deserialize(stream);
return objectType;
}
(Or, just keep the original MemoryStream and pass it around.)
Update
BinaryFormatter serializes full .Net type information (i.e. the fully qualified type name) to and from the serialization stream. If the receiving system doesn't have a type with exactly the same name, in exactly the same namespace, in exactly the same assembly, then deserialization fails.
If this is your situation, you have the following workarounds:
Extract a shared DLL containing the type in question and link it into both client and server; problem solved.
Write a SerializationBinder to map the type assemblies and names. See here and also here or here for instructions how to do it.
Consider a different, more contract-oriented binary format. Bson is one option. protobuf-net is another, albeit one I have not used. More here.
I am new to stocks in C#, I wish to send a Object in C#. Have been using BinaryWriter to send data (works fine for string), but it doesn't seem to have a method like
writer.Writer(new SerliaizedObject());
How do we achieve this using BinaryReader/BinaryWriter
UPDATE:
I used the following functions to convert by object to byte and send it across to the client
public static byte[] SerializeToBytes<T>(T item)
{
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, item);
stream.Seek(0, SeekOrigin.Begin);
return stream.ToArray();
}
}
public static object DeserializeFromBytes(byte[] bytes)
{
var formatter = new BinaryFormatter();
using (var stream = new MemoryStream(bytes))
{
return formatter.Deserialize(stream);
}
}
To send the data is used:
formatter = new BinaryFormatter();
MessageBox.Show(SerializeToBytes<mydata>(new mydata()).Length+"");
writer.Write(SerializeToBytes<mydata>(new mydata()));
ChatBox.AppendText("Client Says :" + UserMessage.Text + "\r\n");
And to read the data I used:
while (true)
{
byte[] bytes = reader.ReadBytes(120);
mydata temp = DeserializeFromBytes(bytes) as mydata;
ChatBox.AppendText("Server Says " + temp + "\r\n");
}
But the reader doesn't seem to work, Any Ideas?
Use BinaryFormatter to write serializable objects to streams in binary format:
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, mySerializable);
You should use the first 4 bytes as length header, and in the receive loop you add a variable bytesReadSoFar. Then you know when everything is received.
I'm trying to do custom serialization/deserialization of an object as well as compressing/decompressing the serialized data with DeflateStreams. I originally did this for more complex objects but cut it down to try and figure out the problem, however it just became more puzzling as it is still there. Here is the class to be serialized/deserialized:
[Serializable]
public class RandomObject : ISerializable
{
public String Name { get; set; }
public String SavePath { get; set; }
public RandomObject()
{
}
public RandomObject(String name, String savepath)
{
Name = name;
SavePath = savepath;
}
public RandomObject(SerializationInfo info, StreamingContext context)
: this(info.GetString("name"), info.GetString("savepath"))
{
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("name", Name);
info.AddValue("savepath", SavePath);
}
}
And here is the code that is supposed to serialize it(which seems to work):
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, profile);
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress))
{
try
{
using (FileStream fs = File.Create(path))
{
ds.Flush();
Miscellaneous.CopyStream(ds.BaseStream, fs);
fs.Flush();
fs.Close();
}
}
catch (IOException e)
{
MessageBox.Show(e.Message);
success = false;
}
ds.Close();
}
ms.Close();
}
And here is the deserialization:
RandomObject profile = null;
using (FileStream fs = File.OpenRead(path))
{
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress))
{
BinaryFormatter bf = new BinaryFormatter();
ds.Flush();
using (MemoryStream ms = new MemoryStream())
{
Miscellaneous.CopyStream(ds.BaseStream, ms);
profile = bf.Deserialize(ms) as RandomObject;
profile.SavePath = path;
ms.Close();
}
ds.Close();
}
fs.Close();
}
Now, to the problem. Deserialization throws a SerializationException with the message {"No map for object '201326592'."} I have no idea how to troubleshoot or figure out what exactly is causing the problem. Very basic serialization works when I just run BinaryFormatter's Serialize and Deserialize methods on the same MemoryStream.
I tried removing the DeflateStream stuff from both methods, but it's still the same problem. When I look at the examples at MSDN and other places it looks like I'm doing it just right, and googling for the exception message doesn't give any meaningful results(or perhaps I'm just bad at searching).
PS. As you can see I use Miscellaneous.CopyStream(src, dest) which is a basic stream copier, as I can't get src.CopyTo(dest) to work at all, so any hints on that is welcome as well.
Below is a link to the whole VS2010 project if you would like to look at it more closely:
http://www.diredumplings.com/SerializationTesting.zip
UPDATE:
The_Smallest: I tried using the Compress method you posted on my serialization:
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
bf.Serialize(stream, profile);
byte[] array = Compress(stream);
using (MemoryStream ms = new MemoryStream(array))
{
using (FileStream fs = File.Create(path))
{
ms.WriteTo(fs);
fs.Flush();
}
}
}
However, it seems to give me the same problems that I had with srcStream.CopyTo(destStream) earlier, which is that it doesn't seem to get written to the stream. The result is a 0 kb file when I try to save it to disk. Any ideas?
Pieter: I removed the MemoryStream from the deserialization method and it seems have the same functionality as before. However I'm not sure how to implement the serialization the way you suggested. Is this what you had in mind?
BinaryFormatter bf = new BinaryFormatter();
using (FileStream fs = File.Create(path))
{
using (DeflateStream ds = new DeflateStream(fs, CompressionMode.Compress))
{
bf.Serialize(ds, profile);
fs.Flush();
ds.Close();
}
fs.Close();
}
Thanks to both of you!
I dowloaded you example and digged a little in there. See changes for your project below:
Replace LoadFromFile in Loader.cs
private static RandomObject LoadFromFile(string path)
{
try
{
var bf = new BinaryFormatter();
using (var fileStream = File.OpenRead(path))
using (var decompressed = new MemoryStream())
{
using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Decompress))
deflateStream.CopyTo(decompressed);
decompressed.Seek(0, SeekOrigin.Begin);
var profile = (RandomObject)bf.Deserialize(decompressed);
profile.SavePath = path;
return profile;
}
}
catch (IOException e)
{
MessageBox.Show(e.Message);
return null;
}
}
Replace Save in Saver.cs as follows:
public static bool Save(RandomObject profile, String path)
{
try
{
var bf = new BinaryFormatter();
using (var uncompressed = new MemoryStream())
using (var fileStream = File.Create(path))
{
bf.Serialize(uncompressed, profile);
uncompressed.Seek(0, SeekOrigin.Begin);
using (var deflateStream = new DeflateStream(fileStream, CompressionMode.Compress))
uncompressed.CopyTo(deflateStream);
}
return true;
}
catch (IOException e)
{
MessageBox.Show(e.Message);
return false;
}
}
You should serialize into the DeflateStream, not the base (MemoryStream) stream.
For serializing: begin with the File.Create. Then around that stream, create the DeflateStream. Then to the DefaulteStream, serialize your objects.
For deserializing: do not create the MemoryStream and deserialize directly from the DeflateStream.
I believe there is no need for the added MemoryStream. If however you do have problems writing directly to/reading directly from the file streams, just change the serialize routine to write to the DeflateStream instead of the MemoryStream.
That should solve your issues.
There is error in streams logics, while compressing you should write to CompressStream, which writes to MemoryStream, after this you will have result in MemoryStream (not in CompressStream)
Here is example how to compress and decompress bytes
private static byte[] Compress(Stream stream)
{
using (var resultStream = new MemoryStream())
{
using (var gzipStream = new DeflateStream(resultStream, CompressionMode.Compress))
stream.CopyTo(gzipStream);
return resultStream.ToArray();
}
}
private static byte[] Decompress(byte[] bytes)
{
using (var readStream = new MemoryStream(bytes))
using (var resultStream = new MemoryStream())
{
using (var gzipStream = new DeflateStream(readStream, CompressionMode.Decompress))
gzipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
I am created a post before " Object to byte not working " .
I fixed problems that users said me , but there is still problem .
Error Message : The constructor to deserialize an object of type 'WindowsFormsApplication1.Form1+Item' was not found.;
void start()
{
Item item = new Item();
item.files.Add(#"test");
byte[] b = ObjectToByteArray(item);
Item k = Desriles(b);
}
[Serializable]
public class Item : ISerializable
{
public Item()
{
files = new List<string>();
Exclude = false;
CleanEmptyFolder = false;
}
public List<string> files;
public string MusicProfileName;
public bool Exclude;
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("files", files);
info.AddValue("MusicProfileName", MusicProfileName);
info.AddValue("Exclude", Exclude);
}
#endregion
}
public byte[] ObjectToByteArray(object _Object)
{
using (var stream = new MemoryStream())
{
// serialize object
var formatter = new BinaryFormatter();
formatter.Serialize(stream, _Object);
// get a byte array
var bytes = new byte[stream.Length];
using (BinaryReader br = new BinaryReader(stream))
{
bytes = br.ReadBytes(Convert.ToInt32(stream.Length));
}
return bytes;
}
}
public Item Desriles(byte[] items)
{
using (MemoryStream stream = new MemoryStream())
{
stream.SetLength(items.LongLength);
stream.write(items, 0, items.Length);
var formatter = new BinaryFormatter();
stream.Seek(0, SeekOrigin.Begin);
object item = formatter.Deserialize(stream); // Here I will get error
return (Item)item;
}
}
The serialization code can't work properly, you forgot to reset the stream back to the beginning. The better mouse trap:
public byte[] ObjectToByteArray(object _Object) {
using (var stream = new MemoryStream()) {
var formatter = new BinaryFormatter();
formatter.Serialize(stream, _Object);
return stream.ToArray();
}
}
The deserialization code can similarly be simplified:
public Item Desriles(byte[] items) {
using (MemoryStream stream = new MemoryStream(items)) {
var formatter = new BinaryFormatter();
return (Item)formatter.Deserialize(stream);
}
}
And you don't need GetObjectData().
In this section:
using (MemoryStream stream = new MemoryStream())
{
stream.SetLength(items.LongLength);
stream.Read(items, 0, items.Length);
[...]
object item = formatter.Deserialize(stream);
it seems you are creating a new, empty memory stream, then attempting to read from it, and Deserialize from it.
Of course it fails. The stream is empty.
abelenky makes a good point, but I don't think your:
public byte[] ObjectToByteArray(object _Object)
works either.
C# Code Snippet - Object to byte array
C# Code Snippet - Byte array to object
Thanks to All :
I found problem : I should base that to ISerializable