Exception.TargetSite is null after deserialization with binary formatter
We have a problem with deserializing an exception. I am fully aware that it's a bad practice to send exceptions over the network but the current setup requires this for the integration of our systems to work.
The server system is a .net 4 wcf service and the caller is a .net 2 that calls the .4 using a standard web reference.
The server side simply serializes the exception and encodes it in a Base64 string.
var formatter = new BinaryF开发者_如何学运维ormatter();
var mem = new MemoryStream();
formatter.Serialize(mem, ex);
EncodedException = Convert.ToBase64String(mem.ToArray());
On the receiving end
var formatter = new BinaryFormatter();
messageChannel.ServerException = (Exception)
formatter.Deserialize(new MemoryStream(Convert.FromBase64String(base64string)));
All is there except the TargetSite.
Any hints are greatly appreciated.
Thanks
Johan
Exception.TargetSite Gets the method that throws the current exception. According to MSDN: The TargetSite property may not accurately report the name of the method in which an exception was thrown if the exception handler handles an exception across application domain boundaries.
Which makes sense since the return value can be assigned only if the TargetSite of the expetion exist on your remote code/application (and it probably doesn't since it can be a different code/application).
It is the same thing as to serialize a reflection of the C# object send it over network de serialize it and expect it to works as it worked on local site even if the reflected objects does not exist on the remote site. In short: reflection of the class without the class is nothing.
精彩评论