Wcf non-memory deserialization

I have a WCF service that processes a very large number of requests (thousands per second). Each request contains objects, so they are created inside the DataContractSerializer during deserialization. My service processes the messages and they are cleared by the .net garbage collector.

The problem is that garbage collection causes problems for my service (sometimes requests are 100 Ξs more than they should). I need to keep them to a minimum. Therefore, I am looking for a way to use a pool of objects. In other words, I want the data serializer to be able to get the object from the object pool (instead of getting it through GetUninitializedObject), and then when I finished processing the message, I released it back to the pool for cleaning and reuse, thereby avoiding thousands of memory allocations per second.

I saw that this is possible with protobuf-net ( Using protobuf-net, is it possible to deserialize a message without allocating memory? ) And actually I'm using protobuf elsewhere, but for this particular situation, which is not an option

+4
source share
1 answer

DataContractSerializer sealed and cannot be updated. Therefore, unfortunately, you cannot delete the call to FormatterServices.GetUninitializedObject .

Instead, you need to create your own serializer that inherits from XmlObjectSerializer so that you can take full control of instantiation.

The next step is to create a DataContractSerializerOperationBehavior and override the CreateSerializer methods to return the configured serializer.

The last thing to do is to remove the default DataContractSerializerOperationBehavior from the endpoint and replace it with a custom one that implements your own serializer. Carlos Figueira has an entry on his blog showing how to do this (go to the "Real World: Using the New Serializer" section).

+1
source

All Articles