Skip to content

Commit

Permalink
Resolved #932: Added serialization constructor to DependencyResolutio…
Browse files Browse the repository at this point in the history
…nException for supported framework targets.
  • Loading branch information
alexmg committed Mar 6, 2019
1 parent 457c703 commit 8988293
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Autofac/Core/DependencyResolutionException.cs
Expand Up @@ -24,6 +24,9 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
#if !NETSTANDARD1_1
using System.Runtime.Serialization;
#endif

namespace Autofac.Core
{
Expand All @@ -38,8 +41,15 @@ namespace Autofac.Core
#endif
public class DependencyResolutionException : Exception
{
#if !NETSTANDARD1_1
protected DependencyResolutionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endif

/// <summary>
/// Initializes a new instance of the <see cref="DependencyResolutionException"/> class.
/// Initializes a new instance of the <see cref="DependencyResolutionException" /> class.
/// </summary>
/// <param name="message">The message.</param>
public DependencyResolutionException(string message)
Expand Down
47 changes: 47 additions & 0 deletions test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs
@@ -1,4 +1,8 @@
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;
using Autofac.Core;
using Xunit;

Expand Down Expand Up @@ -55,5 +59,48 @@ public void ExceptionMessageUnwrapsNestedResolutionFailures()
Assert.IsType<InvalidOperationException>(inner.InnerException);
Assert.Equal(A.Message, inner.InnerException.Message);
}

[Serializable]
public class CustomDependencyResolutionException : DependencyResolutionException
{
public int Value { get; }

public CustomDependencyResolutionException(int value)
: base(null)
{
Value = value;
}

protected CustomDependencyResolutionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
if (info == null) throw new ArgumentNullException(nameof(info));

Value = info.GetInt32(nameof(Value));
}

[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);

info.AddValue(nameof(Value), 123);
}
}

[Fact]
public void SupportCustomRuntimeSerialization()
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, new CustomDependencyResolutionException(123));

stream.Position = 0;
var exception = (CustomDependencyResolutionException)formatter.Deserialize(stream);

Assert.Equal(123, exception.Value);
}
}
}
}

0 comments on commit 8988293

Please sign in to comment.