WCF 流式处理将 Byte[] 转换为 Class 对象

WCF Streaming Convert Byte[] to Class Object

我有流式 WCF 服务。它接收名为 ContentObjectData 的序列化 class 流。我从流中收到的字节暂时放在 ArrayList 中,因为我不知道 Stream 有多大,老实说,我也不知道如何处理它们。

ContentObjectDataClass:

[Serializable]
    public class ContentObjectData
    {
     string Hash { get; set; }
     string Data { get; set; }
     string FileName { get; set; }
     string DisplayImage { get; set; }
    }

这是从客户端接收流的服务方法。

[OperationContract]
        public void SendContentObject(Stream data)
        {
            ArrayList alBytes = new ArrayList();
            Console.WriteLine("Client connected");
            int count;
            byte[] buffer = new byte[4096];
            while ((count = data.Read(buffer, 0, buffer.Length)) > 0)
            {
                alBytes.AddRange(buffer);
            }
            long i = alBytes.Count;
            data.Close();
        }

此刻我正在使用以下方法发送图像进行测试:

 private void btnLoadImage_Click(object sender, EventArgs e)
        {
            DialogResult dr = OFD.ShowDialog();
            if (dr == DialogResult.OK)
            {
                foreach (string filename in OFD.FileNames)
                {
                    try
                    {
                        ContentObject co = new ContentObject();

                        co.Data = LoadFile(filename);
                        co.Hash = Hashing.HashString(co.Data);
                        co.DisplayImage = co.Data;
                        co.FileName = co.Hash;
                        Stream stream = SerializeToStream(co);                        
                        SendContentObject(stream);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                }
            }
        }

        private void SendContentObject(Stream stream)
        {
            NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
            // TimeSpan.MaxValue is interpreted with a rounding error, so use 24 days instead
            binding.SendTimeout = TimeSpan.FromDays(24);
            binding.TransferMode = TransferMode.Streamed;
            ChannelFactory<RaptorStreamingHost> factory = new ChannelFactory<RaptorStreamingHost>(
                binding, new EndpointAddress("net.tcp://ccs-labs.com:804/"));
            RaptorStreamingHost service = factory.CreateChannel();
            service.SendContentObject(stream);
            ((IClientChannel)service).Close();
        }

        private string LoadFile(string filename)
        {
            return Hashing.BytesToString(File.ReadAllBytes(filename));
        }


        public static Stream SerializeToStream(object objectType)
        {
            MemoryStream stream = new MemoryStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, objectType);
            stream.Position = 0L; // REMEMBER to reset stream or WCF will just send the stream from the end resulting in an empty stream!
            return (Stream)stream;
        }

我有这个要反序列化,但对我来说没有多大意义:

public static object DeserializeFromStream(MemoryStream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);
            object objectType = formatter.Deserialize(stream);
            return objectType;
        }

如何将 ArrayList of Bytes(我猜是将它们反序列化)转换为新的 ContentObject?

更新一个 啊这么近! 这个方法好

public static ContentObjectData DeserializeFromStream(MemoryStream stream)
        {
            IFormatter formatter = new BinaryFormatter();
            stream.Seek(0, SeekOrigin.Begin);
            ContentObjectData objectType = (ContentObjectData)formatter.Deserialize(stream);
            return objectType;
        }

我有问题。反序列化器找不到 ContentObjectData,因为它正在客户端的命名空间中查找 ContentObjectData 而不是此主机的命名空间。

首先,不要使用 ArrayList to store your bytes. Since ArrayList is non-generic each individual byte will be boxed 和指向保存在数组中的字节的指针,这将使用比必要多 5(32 位)或 9(64 位)倍的内存。

相反,您可以将 Stream 复制到本地 MemoryStream,然后保存底层 byte [] 数组:

    public static void CopyStream(Stream input, Stream output)
    {
        byte[] buffer = new byte[32768];
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, read);
        }        
    }

    [OperationContract]
    public void SendContentObject(Stream data)
    {
        Console.WriteLine("Client connected");
        var memoryStream = new MemoryStream();
        using (data)
            CopyStream(data, memoryStream);
        byte [] alBytes = memoryStream.ToArray();
    }

然后你可以将byte []数组转回MemoryStream进行反序列化:

    public static object DeserializeFromStream(byte [] allBytes)
    {
        using (var stream = new MemoryStream(allBytes))
            return DeserializeFromStream(stream);
    }

    public static object DeserializeFromStream(MemoryStream stream)
    {
        IFormatter formatter = new BinaryFormatter();
        stream.Seek(0, SeekOrigin.Begin);
        object objectType = formatter.Deserialize(stream);
        return objectType;
    }

(或者,只保留原来的 MemoryStream 并传递它。)

更新

BinaryFormatter serializes full .Net type information (i.e. the fully qualified type name) 进出序列化流。如果接收系统没有名称完全相同、名称空间完全相同、程序集中完全相同的类型,则反序列化将失败。

如果这是您的情况,您有以下解决方法:

  1. 提取包含相关类型的共享 DLL 并将其 link 放入客户端和服务器;问题解决了。

  2. 写一个 SerializationBinder to map the type assemblies and names. See here and also here or here 说明如何操作。

  3. 考虑一种不同的、更面向契约的二进制格式。 Bson is one option. protobuf-net is another, albeit one I have not used. More here.