I'm using the BinaryFormatter.Deserialize(Stream, HeaderHandler).
I may have missed something obvious here but I can't find any examples online so I'm hoping someone can shed some light. I've passed in my delegate HeaderHandler that returns object but i don't know how to get hold of that object that it returns?

- 11,704
- 8
- 43
- 67

- 612
- 5
- 21
-
I'm always really confused about when people edit my questions and remove the pleases, thank yous and sign offs? I want people to know I appreciate thier help. – Sara Mar 28 '12 at 11:47
-
It is hard to tell what you are asking. why do you need the "object" it returns... if you are just looking to "see" what it returns you could always publish and event from your HeaderHandler and then pass a reference to the "object" to the event listeners and then you could "see" into what it is doing... or you could just put a break point in your debugger. Again I am not sure what you are trying to do. – John Sobolewski Mar 28 '12 at 11:52
-
I need the headers, in the header I am storing a fileLocation that i need to pull out, which I can and I can store it in a field variable but it seems a bit dirty. I would have thought if it is returning an object I should be able to get hold of it some how through the formatter? Does that make more sense? – Sara Mar 28 '12 at 11:54
-
I can repro this not respecting the "The deserialized object or the top object (root) of the object graph." behaviour. – Marc Gravell Mar 28 '12 at 11:56
-
Looking further, the headers are actually parsed **after** the main object has actually been deserialized. – Marc Gravell Mar 28 '12 at 12:04
-
And looking further still, at the implementation level: the MethodResponse constructor indeed does nothing whatsoever with this value. – Marc Gravell Mar 28 '12 at 12:08
-
@MarcGravell Sounds very headerlike doesn't it :P. Sara: Wouldn't it be a better idea to wrap the object on the other side and include extra information in the wrapper object. This whole header stuff seems like a typical MS afterthought :). – albertjan Mar 28 '12 at 12:09
-
1@the_ajp it looks to me like an internal detail of remoting that should **never** have leaked into the public `BinaryFormatter` API. I agree that using a wrapper object for the extra info (or better: a full dedicated DTO) is the way to go, but personally I'd also add: "and don't use `BinaryFormatter` - it has... kinks". – Marc Gravell Mar 28 '12 at 12:18
2 Answers
K; tracked through reflector. The only time the regular implementation uses this value is, when handling some data via remoting, if the value returned from the HeaderHandler
is a MarshalByRefObject
, in which case an identity is obtained and used for linking back. Specifically, the System.Runtime.Remoting.Messaging.MethodCall
constructor.
But all of that is an implementation detail! In most sane scenarios, the answer is: it isn't used.
Indeed, the header-handling happens after the main deserialization, which rules out cheekily using the header-handler to set some values on the context object, which you then process.
However, your header-handler can still update local variables:
string someValue = null;
object obj = serializer.Deserialize(source, headers => {
// check the headers and assign someValue based on
// what you find there; for brevity, make it up!
someValue = "something from the headers";
return null;
});
Console.WriteLine(someValue);
Gotta love full lexical closures.
Personally, though, I conclude: this isn't the way to do this. I would simply transfer a DTO with exactly the data you want to send.

- 1,026,079
- 266
- 2,566
- 2,900
-
You forgot to tell @Sara about protocol-buffers. ;) Which is a very nice and fast way of serializing and de-serializing DTO's. I can attest to that. We've had a very positive experience with it. – albertjan Mar 28 '12 at 13:29
-
This is a most awesome response! Thank you for the effort, I tried to look through it in dotpeek but could only give it a certain about of time and ended up doing it the way you suggested :). Due to some design restrictions I couldn't just add it to the DTO that I am passing over but I will look into it a bit more tomorrow! Thanks for looking into this, I might just write a blog post about it as there seems to be nothing about it! I'll be sure to links to everyone that contributed. :) – Sara Mar 28 '12 at 16:49
As far as I can tell the BinaryFormatter does nothing with the returned object.
var deserializedObject = (TypeOfDeserializedObject)
BinaryFormatter.Deserialize(stream, headers =>
{
//do stuff with your headers here
foreach (var header in headers)
{
}
return new object();
});

- 7,739
- 6
- 44
- 74