I have a RESTful service that I am developing in WCF. I am exposing a contract that does not have serializable types so I am using a DataContractSurrogate (implementation of IDataContractSurrogate
) to create surrogates that can be serialized with the DataContractSerializer
.
My question is, how can I access request/response headers in my DataContractSurrogate class?
In the service it's possible by simply using WebOperationContext
and in the client the same WebOperationContext
is accessible within the scope of an OperationContextScope
object which can be created with a reference to the current WCF Channel (IContextChannel
).
I could create an OperatoinContextScope
if I were able to get a reference to the WCF channel within IDataContractSurrogate.GetDeserializedObject
but I'm not sure how to do that either.
Any ideas?
@casperOne:
I want to get the header value in the DataContractSurrogate implementation because to convert from the surrogate to the target type I need to reference an entry in a singleton pool of factory objects.
Something like this:
SingletonFactoryPool.Factories[factoryIdFromHeader].CreateTargetType(surrogateValues);
I'm already passing the data I need in the headers for other reasons, it seems like it would be cleaner if I could just read the value from the header. Your point is valid though, I could easily pass the same data in the surrogate.