.net 使用KB2901983中断XmlWriterBackedStream类中的更改

.net 使用KB2901983中断XmlWriterBackedStream类中的更改,.net,web-services,wcf,.net,Web Services,Wcf,我有一个针对.NET Framework 4.0的应用程序,昨天我们的客户做了一些更新,我们的应用程序停止工作。在深入挖掘.NET Framework源代码之后,我在XmlWriterBackedStream类中找到了原因。我的机器上的代码如下所示: // C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.We

我有一个针对.NET Framework 4.0的应用程序,昨天我们的客户做了一些更新,我们的应用程序停止工作。在深入挖掘.NET Framework源代码之后,我在XmlWriterBackedStream类中找到了原因。我的机器上的代码如下所示:


// C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll
// System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

// Architecture: AnyCPU (64-bit preferred)
// Runtime: .NET 4.0

// System.ServiceModel.Channels.StreamBodyWriter.XmlWriterBackedStream
public override void Write(byte[] buffer, int offset, int count)
{
    if (this.writer.WriteState == WriteState.Start)
    {
        this.writer.WriteStartElement("Binary", string.Empty);
        this.writer.WriteBase64(buffer, offset, count);
        return;
    }
    if (this.writer.WriteState == WriteState.Content)
    {
        this.writer.WriteBase64(buffer, offset, count);
    }
}
而客户机器上的.NET 4.0框架代码如下所示:


// C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll
// System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

// Architecture: AnyCPU (64-bit preferred)
// Runtime: .NET 4.0

// System.ServiceModel.Channels.StreamBodyWriter.XmlWriterBackedStream
public override void Write(byte[] buffer, int offset, int count)
{
    if (this.writer.WriteState == WriteState.Content || this.isQuirkedTo40Behavior)
    {
        this.writer.WriteBase64(buffer, offset, count);
        return;
    }
    if (this.writer.WriteState == WriteState.Start)
    {
        this.writer.WriteStartElement("Binary", string.Empty);
        this.writer.WriteBase64(buffer, offset, count);
    }
}
请注意cutomers机器上的
this.isrequiredto40行为
。这迫使我根据.NETFramework4.5编译应用程序,以使其重新工作

这是.NET Framework中的错误吗?我如何才能让我的应用程序在不以4.5框架为目标的情况下重新运行

这是我的课引起的问题:


class MyMessageWriter : StreamBodyWriter
{
   private readonly Action<System.IO.Stream> writerAction;

   public MyMessageWriter(Action<System.IO.Stream> writer) : base(false)
   {
      this.writerAction = writer;
   }

   protected override void OnWriteBodyContents(System.IO.Stream stream)
   {
      this.writerAction(stream);
   }
}

类MyMessageWriter:StreamBodyWriter
{
私有只读操作writerAction;
公共MyMessageWriter(操作编写器):基本(false)
{
this.writerAction=writer;
}
受保护的重写无效OnWriteByContents(System.IO.Stream)
{
此.writeAction(流);
}
}

这似乎真的是一个突破性的改变,安装KB2901983(感谢微软!)。不过,我找到了一种解决这个问题的方法,因此您仍然可以将应用程序定位到.NET Framework 4.0(有点难看,但它可以工作):


类MyMessageWriter:StreamBodyWriter
{
私有只读操作writerAction;
公共MyMessageWriter(操作编写器):基本(false)
{
this.writerAction=writer;
}
受保护的重写无效OnWriteByContents(System.IO.Stream)
{
此.writeAction(流);
}
受保护的重写无效OnWriteByContents(XmlDictionaryWriter编写器)
{
writer.WriteStartElement(“二进制”,string.Empty);
writer.WriteBase64(新字节[0],0,0);//强制WriteState.Content
基于writebodycontents(writer)的基础;
}
}
更新

如果未安装KB2901983,此解决方案似乎不起作用

更新2

我必须添加
writer.WriteBase64(新字节[0],0,0)
,以强制XmlDictionaryWriter的状态为
WriteState.Content
,现在它应该在安装KB2901983之前和之后工作

更新3


另一种解决方案是将XmlDictionaryWriter包装到您自己的流派生类中

,可能是他们机器上的更新,而不是您的更新。您应该检查这两个DLL的版本号,然后搜索客户的版本号,以查找更新和正在进行的操作的详细信息。在和更新中添加“怪癖模式”并不是一个“错误”。(假设代码示例中的//ver编号不是来自程序集的强名称,而是来自代码中的某个内容?)下面是这个问题的知识库@Will:如果“怪癖模式”不会阻止应用程序,我会同意它。只有客户计算机上的修订号更高。。。感谢链接-我错过了:-(是的。他们有一个你在你的机器上没有的更新。问题是如何解决这个问题?也许你可以抓取更新,然后根据4.0重新编译?你可能必须修复运行时错误,但让他们在你的机器上比远程调试或通过转储要好。

class MyMessageWriter : StreamBodyWriter
{
   private readonly Action<System.IO.Stream> writerAction;

   public MyMessageWriter(Action<System.IO.Stream> writer) : base(false)
   {
      this.writerAction = writer;
   }

   protected override void OnWriteBodyContents(System.IO.Stream stream)
   {
      this.writerAction(stream);
   }

   protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
   {
      writer.WriteStartElement("Binary", string.Empty);
      writer.WriteBase64(new byte[0], 0, 0); // force WriteState.Content

      base.OnWriteBodyContents(writer);
   }
}