我在C#/ ASP.NET中反序列化时遇到问题,这会产生确切的错误:
输入流不是有效的二进制格式。起始内容(以字节为单位)为:41-41-45-41-41-41-44-2F-2F-2F-2F-2F-41-51-41-41-41 ... < / p>
我想做什么
我有一个有3个班级的结构。我有一个A类,它是一个基类,然后是B和C,它们是从A。
派生的我试图在类型为VARCHAR(MAX)的列中使用LINQ to SQL在数据库中存储随机类型的B和C. 我不能使用BINARY,因为长度大约是15.000。
我的代码......
错误位于最后一个代码块
中业务层中的C#代码 - 存储记录
private void AddTraceToDatabase(FightTrace trace)
{
MemoryStream recieverStream = new MemoryStream();
MemoryStream firedStream = new MemoryStream();
MemoryStream moveStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(recieverStream,trace.Reciever);
binaryFormatter.Serialize(firedStream,trace.FiredBy);
binaryFormatter.Serialize(moveStream,trace.Move);
string reciever = Convert.ToBase64String(recieverStream.ToArray());
string fired = Convert.ToBase64String(firedStream.ToArray());
string move = Convert.ToBase64String(moveStream.ToArray());
this.dataAccess.AddFightTrace(trace.TraceType.ToString(),reciever,move,fired,trace.DateTime,this.FightId);
}
数据访问层中的C#代码 - 存储记录
public void AddFightTrace(string type, string reciever, string Move, string firedBy, DateTime firedAt, int fightid)
{
GameDataContext db = new GameDataContext();
dbFightTrace trace = new dbFightTrace();
trace.TraceType = type;
trace.Reciever = reciever;
trace.Move = Move;
trace.FiredBy = firedBy;
trace.FiredAt = firedAt;
trace.FightId = fightid;
db.dbFightTraces.InsertOnSubmit(trace);
db.SubmitChanges();
}
C#代码获取数据库中的条目
public List<dbFightTrace> GetNewTraces(int fightid, DateTime lastUpdate)
{
GameDataContext db = new GameDataContext();
var data = from d in db.dbFightTraces
where d.FightId==fightid && d.FiredAt > lastUpdate
select d;
return data.ToList();
}
C#Factory,从LINQ转换为SQL类到我的对象
这就是错误来了
public FightTrace CreateTrace(dbFightTrace trace)
{
TraceType traceType = (TraceType) Enum.Parse(typeof(TraceType), trace.TraceType);
BinaryFormatter formatter = new BinaryFormatter();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
MemoryStream recieverStream = new MemoryStream(enc.GetBytes(trace.Reciever));
recieverStream.Position = 0;
MemoryStream firedStream = new MemoryStream(enc.GetBytes(trace.FiredBy));
firedStream.Position = 0;
MemoryStream movedStream = new MemoryStream(enc.GetBytes(trace.Move));
movedStream.Position = 0;
// THE NEXT LINE HERE CAUSES THE ERROR
NPC reciever = formatter.Deserialize(recieverStream) as NPC;
Player fired = formatter.Deserialize(firedStream) as Player;
BaseAttack attack = formatter.Deserialize(movedStream) as BaseAttack;
FightTrace t = new FightTrace(traceType,reciever,attack,fired);
t.TraceId = trace.FightTraceId;
t.DateTime = trace.FiredAt;
return t;
}
因此,在运行第一个Deserialize方法时会发生错误,并出现上述错误。
我已经尝试过几件事,但我对这件事感到很遗憾......
谢谢! : - )
答案 0 :(得分:4)
我认为从base64到字节的转换是错误的。
试试这个:
var bytes = Convert.FromBase64String(base64);
=&GT;
MemoryStream recieverStream = new MemoryStream(Convert.FromBase64String(trace.Reciever));
NPC reciever = formatter.Deserialize(recieverStream) as NPC;
答案 1 :(得分:2)
我认为这是因为Convert.ToBase64String(recieverStream.ToArray())
和enc.GetBytes(trace.Reciever)
不是彼此对应的。
您需要在解码部分对base64进行取消编码。