3

我一直在使用一些代码通过使用MSDN中的代码来创建 MTOM 。

似乎有一个错误,我不明白问题出在哪里,因为论坛上的一位用户指出有一个错误。

文件 (JPEG) 数据在反序列化后损坏。下面列出了完整的代码。

 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;

                    }

                }
            }

如果有人可以帮助我指出问题所在,我将不胜感激。我是 XOP/MTOM 的新手,发现很难找出错误可能出在哪里。序列化或反序列化。

谢谢

4

1 回答 1

5

您的代码中有一个错误。更改您的方法调用

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

DeserializeMTOMMessage(MTOMInMemory.ToArray())

和实施

 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 到字节数组的双重转换,反之亦然,最终创建的不是您使用的原始字节数组

于 2012-05-20T09:05:05.970 回答