MSDNのコードを使用してMTOMを作成するコードをいくつか使用しています。MTOMの作成と逆シリアル化
エラーがあり、フォーラムのユーザーの1人がエラーがあると指摘したため、問題がどこにあるのか理解できません。
デシリアライゼーション後にファイル(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の新機能で、どこでエラーが発生するのかを追跡するのが難しいと感じています。シリアライゼーションまたはデシリアライゼーションのいずれか。
ありがとうございました
正確なエラーは何ですか受け取っていますか? –
MIMEコンテンツタイプが正しくありません。まあ今は実際の言葉を覚えていません。しかし、MIMEコンテンツタイプが無効であると私に伝えます。 –
MIMEコンテンツタイプがシリアライズ可能ではないようです。試して[これ](http://stackoverflow.com/questions/2795588/how-to-serialize-an-object-of-type-system-net-mime-contenttype)、それが動作する場合、私は答えとしてそれを追加します。 –