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

时间:2014-05-22 19:27:33

标签: c# serialization ipc pipe named

目前,我正在尝试序列化对象队列,通过命名管道发送它们,然后在接收应用程序上反序列化并重新构造队列。这时我正在使用BinaryFormatter,但是当在接收端调用Deserialize时(即在binForFormat.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 个答案:

答案 0 :(得分:0)

请注意我刚解决了这个问题。而不是使用此功能:

ObjectToByteArray(memoryStream)

我只是调用以下内容,因为MemoryStream对象本质上是一个内部字节数组:

memoryStream.ToArray()

感谢您的考虑。