views:

651

answers:

1

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.

A: 

If you need to access the channel in the data contract, I would say that your class is incorrectly designed. Your contract should be self contained and not be dependent on the header values used in passing the message through the channel.

You could always create an extension (there are a number of areas you could do this) which would take the deserialized instance and then add to it the header information you need to add.

And of course, you could always create a separate object which is an amalgam of the deserialized instance (passed through the parameters) and the header information.

casperOne
updated the post with some further clarification on why I am trying to get access to the headers in the IDataContractSurrogate implementation. reply was to long for a comment
spoon16