我正在尝试序列化事务,例如:
public static void Main()
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew, TimeSpan.FromMinutes(1)))
{
formatter.Serialize(stream, Transaction.Current);
stream.Seek(0, SeekOrigin.Begin);
MemoryStream result = Second(stream);
Transaction transaction = (Transaction)formatter.Deserialize(result);
Console.WriteLine(transaction.TransactionInformation.Status);
}
Console.ReadLine();
}
public static MemoryStream Second(MemoryStream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
Transaction transaction = (Transaction)formatter.Deserialize(stream);
using (TransactionScope scope2 = new TransactionScope(transaction))
{
}
Console.WriteLine(transaction.TransactionInformation.Status);
MemoryStream newStream = new MemoryStream();
formatter.Serialize(newStream, transaction);
newStream.Seek(0, SeekOrigin.Begin);
return newStream;
}
但是在这两个上WriteLines
,由于某种原因,交易被报告为活跃。如果我在没有序列化的情况下做同样的事情,那么行为应该是它应该的(它中止),但是当我突然序列化它时,它会停止正常运行。我究竟做错了什么?