错误:流“无效”中的类型代码无效

Posted

技术标签:

【中文标题】错误:流“无效”中的类型代码无效【英文标题】:Error: Invalid type code in stream 'Invalid' 【发布时间】:2014-05-22 19:27:32 【问题描述】:

目前,我正在尝试序列化对象队列,通过命名管道发送它们,然后在接收应用程序上反序列化并重新构建队列。此时我正在使用 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

到目前为止,我已经找到了与 Microsoft 错误修复相关的网页,

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 函数的地方:

How to convert an object to a byte array in C#

【问题讨论】:

【参考方案1】:

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

ObjectToByteArray(memoryStream)

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

memoryStream.ToArray()

无论如何感谢您的考虑。

【讨论】:

以上是关于错误:流“无效”中的类型代码无效的主要内容,如果未能解决你的问题,请参考以下文章

客户端的客户端密码验证失败,授权代码流中的客户端密码无效

max(.) 中的错误:使用 dplyr dbplyr 和 bigrquery 时参数的“类型”(列表)无效

iOS 中的 JSON 错误:JSON 写入中的***类型无效

错误:一元“*”的类型参数无效(有“int”)

R plot legend plot.xy中的错误(xy,type,...):无效的绘图类型

JSONSerialization JSON 写入中的类型无效 (_SwiftValue)