Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed serialization of exceptions without the default constructor (#64). #76

Merged
merged 2 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Hyperion.Tests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,41 @@ public void CanSerializeParameterExpression()
}
}

[Fact]
public void CanSerializeTargetInvocationException()
{
var exc = new TargetInvocationException(null);
var serializer = new Hyperion.Serializer();

using (var stream = new MemoryStream())
{
serializer.Serialize(exc, stream);
stream.Position = 0;
var deserialized = serializer.Deserialize<TargetInvocationException>(stream);
Assert.Equal(exc.Message, deserialized.Message);
Assert.Equal(exc.StackTrace, deserialized.StackTrace);
}
}

[Fact]
public void CanSerializeObjectDisposedException()
{
var exc = new ObjectDisposedException("Object is already disposed", new ArgumentException("One level deeper"));
var serializer = new Hyperion.Serializer();

using (var stream = new MemoryStream())
{
serializer.Serialize(exc, stream);
stream.Position = 0;
var deserialized = serializer.Deserialize<ObjectDisposedException>(stream);
Assert.Equal(exc.Message, deserialized.Message);
Assert.Equal(exc.StackTrace, deserialized.StackTrace);
Assert.Equal(exc.InnerException.GetType(), deserialized.InnerException.GetType());
Assert.Equal(exc.InnerException.Message, deserialized.InnerException.Message);
Assert.Equal(exc.InnerException.StackTrace, deserialized.InnerException.StackTrace);
}
}

[Fact]
public void CanSerializeCatchBlock()
{
Expand Down
16 changes: 14 additions & 2 deletions Hyperion/SerializerFactories/ExceptionSerializerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,31 @@ public ExceptionSerializerFactory()

public override bool CanDeserialize(Serializer serializer, Type type) => CanSerialize(serializer, type);

// Workaround for CoreCLR where FormatterServices.GetUninitializedObject is not public
private static readonly Func<Type, object> GetUninitializedObject =
(Func<Type, object>)
typeof(string).GetTypeInfo().Assembly.GetType("System.Runtime.Serialization.FormatterServices")
.GetMethod("GetUninitializedObject", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
.CreateDelegate(typeof(Func<Type, object>));

private static object FormatterServicesGetUninitializedObject(Type type) => GetUninitializedObject.Invoke(type);

public override ValueSerializer BuildSerializer(Serializer serializer, Type type,
ConcurrentDictionary<Type, ValueSerializer> typeMapping)
{
var exceptionSerializer = new ObjectSerializer(type);
var hasDefaultConstructor = type.GetTypeInfo().GetConstructor(new Type[0]) != null;
exceptionSerializer.Initialize((stream, session) =>
{
var exception = Activator.CreateInstance(type);
var className = stream.ReadString(session);
var message = stream.ReadString(session);
var remoteStackTraceString = stream.ReadString(session);
var stackTraceString = stream.ReadString(session);
var innerException = stream.ReadObject(session);

var exception = hasDefaultConstructor ? Activator.CreateInstance(type) :
FormatterServicesGetUninitializedObject(type);
Copy link
Contributor

@Horusiath Horusiath Sep 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this check here - it's not like having default constructor or not will change depending on the binary payload ;) You can move this check outside the deserializer function i.e:

Func<Object> initialize = hasDefaultConstructor 
    ? (() => Activator.CreateInstance(type)) 
    : (FormatterServicesGetUninitializedObject(type));

or (without measures I'm not sure which one is faster):

Func<Type, Object> initialize = hasDefaultConstructor 
    ? Activator.CreateInstance 
    : FormatterServicesGetUninitializedObject;

and use initialize inside deserializer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now it introduces unnecessary delegate allocation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens once per exception type, while constructor check will happen once per each object deserialization call. In practice to be 100% sure, we'd need to check both approaches in performance test - I'll be happy to do that once #57 will finally get merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for the suggestion.


_className.SetValue(exception,className);
_message.SetValue(exception, message);
_remoteStackTraceString.SetValue(exception, remoteStackTraceString);
Expand All @@ -63,7 +75,7 @@ public override ValueSerializer BuildSerializer(Serializer serializer, Type type
var remoteStackTraceString = (string)_remoteStackTraceString.GetValue(exception);
var stackTraceString = (string)_stackTraceString.GetValue(exception);
var innerException = _innerException.GetValue(exception);
StringSerializer.WriteValueImpl(stream,className,session);
StringSerializer.WriteValueImpl(stream, className, session);
StringSerializer.WriteValueImpl(stream, message, session);
StringSerializer.WriteValueImpl(stream, remoteStackTraceString, session);
StringSerializer.WriteValueImpl(stream, stackTraceString, session);
Expand Down