C#:输入流是不是一个有效的二进制格式 [英] C#: The input stream is not a valid binary format
问题描述
我有一个问题,在C#/ ASP.NET,这给确切的错误反序列化:
I have a problem with deserializing in C#/ASP.NET, which gives the exact error:
输入流不是有效的二进制格式。起始内容(以字节为单位)有:41-41-45-41-41-41-44-2F-2F-2F-2F-2F-41-51-41-41-41 ... 的
我所试图做的
我有3班的结构。我有一个A类是一个基类,然后B类和C这是从的。
I have a structure with 3 classes. I have a class A which is a base class, and then class B and C which are derived from A.
我想随机存储B型和C在使用LINQ to SQL数据库,与类型VARCHAR(MAX)列。
作为长度约为15.000我不能使用二进制。
I am trying to store random types of B and C in the database using LINQ to SQL, in a column with the type VARCHAR(MAX). I cannot use BINARY as the length is around 15.000.
我的code ...
错误是在最后$ C $使用cblock
Error is in the LAST codeblock
的商务C#code的第一层存储记录的
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#code在数据访问层 - 存储记录的
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#code获得在数据库中的条目的
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#工厂,从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方法运行时,与上述错误的错误happends
So the error happends when the first Deserialize method is run, with the above error.
我已经试过几件事,但我完全迷失在这一个。
I have tried several things but I am quite lost on this one..
谢谢! : - )
推荐答案
我觉得从丁文的base64为字节后面是错误的。
I think the coversion back from base64 to bytes is wrong.
试试这个:
var bytes = Convert.FromBase64String(base64);
=>
MemoryStream recieverStream = new MemoryStream(Convert.FromBase64String(trace.Reciever));
NPC reciever = formatter.Deserialize(recieverStream) as NPC;
这篇关于C#:输入流是不是一个有效的二进制格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!