错误:流'invalidate '中无效类型代码

本文关键字:无效 类型 代码 invalidate 错误 | 更新日期: 2023-09-27 18:04:36

目前,我正在尝试序列化一个对象队列,通过命名管道发送它们,然后在接收应用程序上反序列化并重新构造该队列。此时我正在使用BinaryFormatter,但是当接收端调用Deserialize(即,在binFormat.Deserialize(memoryStream)行)时,我得到这个错误:

A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
'MySysTrayApp.exe' (CLR v4.0.30319: MySysTrayApp.exe): Loaded 'C:'WINDOWS'assembly'GAC_MSIL'Microsoft.VisualStudio.Debugger.Runtime'12.0.0.0__b03f5f7f11d50a3a'Microsoft.VisualStudio.Debugger.Runtime.dll'. 
Error: Invalid type code in stream 'Invalid'.
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadValue(InternalPrimitiveTypeE code)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadMemberPrimitiveUnTyped()
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at EnvisionSystemTray.Form1.DeserializeNamedPipeData(Byte[] buffer) in c:'MyApp'Form1.cs:line 937
   at EnvisionSystemTray.Form1.WaitForConnectionCallBack(IAsyncResult ar) in c:'MyApp'Form1.cs:line 889

到目前为止,我发现这个网页与微软的错误修复有关,

http://www.ikriv.com/blog/?p=1358

但是当我尝试安装更新时,我得到这个消息:

KB982638 does not apply, or is blocked by another condition on your computer.
在这个时候,我使用的是。net Framework 4.5.1。以下是我正在使用的一些简化代码:
    // Pipe Server application:
    public Form1()
    {
        // ...
        // Create the new async pipe 
        NamedPipeServerStream pipeServer = new NamedPipeServerStream(
            Constants.SYS_TRAY_PIPE_NAME, PipeDirection.In, 1, 
            PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
        // Wait for a connection
        pipeServer.BeginWaitForConnection(
            new AsyncCallback(WaitForConnectionCallBack), pipeServer);
        // ...
    }
    private void WaitForConnectionCallBack(IAsyncResult ar)
    {
        try
        {
            int rxBufferSize = Constants.MAX_MESSAGE_QUEUE_COUNT;
            // Get the pipe
            NamedPipeServerStream pipeServer = (NamedPipeServerStream)ar.AsyncState;
            // End waiting for the connection
            pipeServer.EndWaitForConnection(ar);
            while (!_shutdownEvent.WaitOne(0))
            {
                byte[] buffer = new byte[rxBufferSize];
                // Read the incoming message
                pipeServer.Read(buffer, 0, rxBufferSize);
                BinaryFormatter binFormat = new BinaryFormatter();
                MemoryStream memoryStream = new MemoryStream(buffer);
                // Convert byte buffer to list of data items
                MyQueueObject[] dataObjects =
                    binFormat.Deserialize(memoryStream);
                // ...
               memoryStream.Close();
                // ...
                System.Threading.Thread.Sleep(_ackInterval);
            }
        }

    // Pipe Client application:
    private void ConstructExternalClientNamedPipes()
    {
        NamedPipeClientStream pipeClientStream = new NamedPipeClientStream(".",
            Constants.SYS_TRAY_PIPE_NAME, PipeDirection.Out,
            PipeOptions.Asynchronous);
        _lstExternalNamedPipeClients.Add(pipeClientStream);
    }
   private void ExternalClientNamedPipeCommunication()
    {
        // The connect function will indefinitely wait for the pipe to 
        // become available
        foreach (NamedPipeClientStream pipeClientStream in
            _lstExternalNamedPipeClients)
        {
            if (!pipeClientStream.IsConnected)
            {
                pipeClientStream.Connect();
            }
        }
        while (!_shutdownEvent.WaitOne(0))
        {
            for (int i = 0; i < _lstExternalNamedPipeClients.Count; i++)
            {
                NamedPipeClientStream pipeClientStream = 
                    _lstExternalNamedPipeClients[i];
                if (pipeClientStream.IsConnected)
                {
                    ExternalClientWriteSerialization(pipeClientStream);
                }
                else
                {
                    _lstExternalNamedPipeClients[i] = null;
                }
            }
            // Remove all clients that are no longer connected
            _lstExternalNamedPipeClients.RemoveAll(
                    x => x == null);
            System.Threading.Thread.Sleep(_ackInterval);
        }
    }
    private void ExternalClientWriteSerialization(
        NamedPipeClientStream pipeClientStream)
    {
        BinaryFormatter binFormat = new BinaryFormatter();
        MemoryStream memoryStream =
            new MemoryStream(MyQueue.Count);
        binFormat.Serialize(memoryStream,
            MyQueue.ToArray);
        pipeClientStream.BeginWrite(
            Utility.ObjectToByteArray(memoryStream),
            0,
            Convert.ToInt32(memoryStream.Length),
            AsyncTransmitCallback,
            pipeClientStream);
        memoryStream.Close();
    }
有谁对如何解决这个问题有什么建议吗?TIA。

更新:

仅供参考,这就是我得到ObjectToByteArray函数的地方:

如何在c#中将对象转换为字节数组

错误:流'invalidate '中无效类型代码

请注意,我刚刚解决了这个问题。而不是使用这个函数:

ObjectToByteArray(memoryStream)

我简单地调用以下代码,因为MemoryStream对象本质上是一个字节数组:

memoryStream.ToArray()

谢谢你的考虑。