diff --git a/src/Autofac/Core/DependencyResolutionException.cs b/src/Autofac/Core/DependencyResolutionException.cs index cb5f82e05..538088e6f 100644 --- a/src/Autofac/Core/DependencyResolutionException.cs +++ b/src/Autofac/Core/DependencyResolutionException.cs @@ -24,6 +24,9 @@ // OTHER DEALINGS IN THE SOFTWARE. using System; +#if !NETSTANDARD1_1 +using System.Runtime.Serialization; +#endif namespace Autofac.Core { @@ -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 + /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The message. public DependencyResolutionException(string message) diff --git a/test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs b/test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs index ef8c4b58c..5e69dffa1 100644 --- a/test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs +++ b/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; @@ -55,5 +59,48 @@ public void ExceptionMessageUnwrapsNestedResolutionFailures() Assert.IsType(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); + } + } } }