Search code examples
wcfserializationdeserializationdatacontractserializerxmlserializer

Force WCF to use a existing buffers during Deserialization?


I looked for some other topics here as well, but I didn't find a solution to my problem yet.

Imagine the following:
I've a very simple ServiceContract with different OperationContracts. One of these OperationContracts is a simple use-case "download a data transfer object".

The Service looks like:

...
[OperationContract]
DTO Download(strind Id)
...

Class DTO looks like:

[DataContract]
public class DTO
{
     [DataMember]
     public string Id;

     [DataMember]
     public byte[] Data;
}

Of course it's very simple and it works fine, but I need to allocate the byte[] in DTO by myself!

My Code is part of a framework componenent and it's working in parallel under massive memory restrictions. I don't want WCF to allocate all the byte[] and I don't want the ManagedHeap to deallocate them all again. I need to share and reuse all parallel existing buffers.

So when I finished my serialization I will reuse the buffer on serverside. On clientside I want WCF to read into my buffer!

I tried some solutions with own XmlObjectSerialiers and own OperationBehaviors, but it didn't work yet.

Does anyone have any other ideas?


Solution

  • Update:

    I found a first working solution using an own Serializer:XmlObjectSerializer, an own IContractBehavior and an own DataContractSerializerOperationBehavior.

    While reading from XmlDictionaryReader during Deserialization I use the following snippet:

    public bool DeserializeFrom(XmlDictionaryReader source)
    {
         ...
         readBytes = source.ReadElementContentAsBase64(MyOwnAlreadyAllocatedBuffer, offset, toRead);
         ...
    }
    

    This works, but it's still a bit slower than the DataContractSerializer.

    Any other options?

    Notice: I just want to transfer already binary serialized data!