Transferring large payloads of data (Serialized Objects) using wsHttp in WCF with message security

前端 未结 5 1207
陌清茗
陌清茗 2020-12-08 01:34

I have a case where I need to transfer large amounts of serialized object graphs (via NetDataContractSerializer) using WCF using wsHttp. I\'m using message security and woul

5条回答
  •  死守一世寂寞
    2020-12-08 01:45

    i used to implement kinda passing big text to/from wcf. my trig is convert it to stream and use GZipStream to compress it then send it as byte[], luckily its never exceed 10 MB.

    In your case i recommend do fragmentation. Convert Serialized object to byte[] and then merg it and decompress

    psudo

    int transferSize = 5000000; // 5MB
    byte[] compressed = ...;
    var mem = new System.IO.MemoryStream(compressed);
    
    for(int i = 0; i < compressed .length; i+= transferSize )
    {
        byte[] buffer = new byte[transferSize];
        mem.Read(buffer, i, compressed);
        mem.Flush();
        sendFragmentToWCF(buffer);
    }
    

    edit 08 Dec 2010

    ok based on my understanding, the situation is client is download some large serialize object throught WCF. i didn't particularly test on this solution, but guess it should work. The key is save serialized object to file and use Response transmit that file.

    [WebMethod]
    public void GetSerializedObject()
    {
        string path = @"X:\temp.tmp";
    
        var serializer = new  System.Runtime.Serialization.NetDataContractSerializer();
        var file = new System.IO.FileStream(path, System.IO.FileMode.CreateNew);
    
        try
        {
            serializer.Serialize(file, ...);
            this.Context.Response.TransmitFile(path);
            this.Context.Response.Flush();
        }
        finally
        {
            file.Flush();
            file.Close();
            file.Dispose();
            System.IO.File.Delete(path);
        }
    }
    

    WCF shoud do file streaming automatically and u dont ahve to worry about serialized object size since we use file transmit. Dont forget to the config response limit.

提交回复
热议问题