Skip to content

Commit

Permalink
Breaking: DRE is no longer serializable
Browse files Browse the repository at this point in the history
dotnet/docs#34893
.NET has long been deprecating BinaryFormatter and Serializable items.
Analyzers as of .NET 8 are warning about obsolete serialization
constructors. Given most of the underpinnings of this mechanism are also
being deprecated, it seemed reasonable to include deprecation for that in
Autofac.

For projects that continue to use AppDomains and need to serialize
exceptions over the wire, it's recommended to not upgrade Autofac to
this version.
  • Loading branch information
tillig committed Nov 7, 2023
1 parent 334ae01 commit 0b78ed3
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 63 deletions.
13 changes: 0 additions & 13 deletions src/Autofac/Core/DependencyResolutionException.cs
@@ -1,8 +1,6 @@
// Copyright (c) Autofac Project. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Runtime.Serialization;

namespace Autofac.Core;

/// <summary>
Expand All @@ -11,19 +9,8 @@ namespace Autofac.Core;
/// been made during the operation. For example, 'on activated' handlers may have already been
/// fired, or 'single instance' components partially constructed.
/// </summary>
[Serializable]
public class DependencyResolutionException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="DependencyResolutionException"/> class.
/// </summary>
/// <param name="info">The serialization info.</param>
/// <param name="context">The serialization streaming context.</param>
protected DependencyResolutionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DependencyResolutionException" /> class.
/// </summary>
Expand Down
50 changes: 0 additions & 50 deletions test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs
Expand Up @@ -58,54 +58,4 @@ public void ExceptionMessageUnwrapsNestedResolutionFailures()
Assert.IsType<InvalidOperationException>(inner.InnerException);
Assert.Equal(A.Message, inner.InnerException.Message);
}

#if !NET5_0_OR_GREATER

[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);
}
}

// Consider removing this test, or marking it as .NET Standard Only. Binary formatter serialisation has been obsoleted by Microsoft.
[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);
}
}
#endif
}

0 comments on commit 0b78ed3

Please sign in to comment.