MTOM creation and deserialization

I am using some code to create MTOM using MSDN code .

It seems that there is an error, and I can’t understand where the problem is, as one of the users on the forum indicated that there is an error.

File data (JPEG) is corrupted after de-serialization. The full code is given below.

 public class Post_7cb0ff86_5fe1_4266_afac_bcb91eaca5ec
            {
                [DataContract()]
                public partial class TestAttachment
                {
                    private byte[] fileField;
                    private string filenameField;

                    [DataMember()]
                    public byte[] File
                    {
                        get
                        {
                            return this.fileField;
                        }
                        set
                        {
                            this.fileField = value;
                        }
                    }
                    [DataMember()]
                    public string Filename
                    {
                        get
                        {
                            return this.filenameField;
                        }
                        set
                        {
                            this.filenameField = value;
                        }
                    }
                }
                public static void Test()
                {
                    string Filename = "Image.jpg";
                byte[] file = File.ReadAllBytes(Filename);

                TestAttachment Attachment = new TestAttachment();
                Attachment.Filename = Filename;
                Attachment.File = file;
                MemoryStream MTOMInMemory = new MemoryStream();
                XmlDictionaryWriter TW = XmlDictionaryWriter.CreateMtomWriter(MTOMInMemory, Encoding.UTF8, Int32.MaxValue, "");
                DataContractSerializer DCS = new DataContractSerializer(Attachment.GetType());
                DCS.WriteObject(TW, Attachment);
                TW.Flush();
                Console.WriteLine(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));
                var v = DeserializeMTOMMessage(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));
                File.WriteAllBytes(v.Filename,v.File);
                }

                public static TestAttachment DeserializeMTOMMessage(string MTOMMessage)
                {

                    try
                    {

                        MemoryStream MTOMMessageInMemory = new MemoryStream(UTF8Encoding.UTF8.GetBytes(MTOMMessage));

                        XmlDictionaryReader TR = XmlDictionaryReader.CreateMtomReader(MTOMMessageInMemory, Encoding.UTF8, XmlDictionaryReaderQuotas.Max);

                        DataContractSerializer DCS = new DataContractSerializer(typeof(TestAttachment));

                        return (TestAttachment)DCS.ReadObject(TR);

                    }
                    catch
                    {

                        return null;

                    }

                }
            }

I would appreciate it if someone could help me indicate where the problem is. I'm new to XOP / MTOM, and it's hard for me to keep track of where the error might be. Serialization or de-serialization.

thank

+3
source share
1 answer

There is an error in your code. Change the method call

MTOMInMemory.Position = 0;
DeserializeMTOMMessage(Encoding.UTF8.GetString(MTOMInMemory.ToArray()));

to

DeserializeMTOMMessage(MTOMInMemory.ToArray())

and implementation is

 public static TestAttachment DeserializeMTOMMessage(byte[] MTOMMessage) 
    { 
        try 
        { 
            MemoryStream MTOMMessageInMemory = new MemoryStream(MTOMMessage);
            XmlDictionaryReader TR = XmlDictionaryReader.CreateMtomReader(MTOMMessageInMemory, Encoding.UTF8, XmlDictionaryReaderQuotas.Max); 
            DataContractSerializer DCS = new DataContractSerializer(typeof(TestAttachment)); 
            return (TestAttachment)DCS.ReadObject(TR); 
        } 
        catch
        {

            return null;
        } 
    } 

, , utf8 , ,

+5

All Articles