Skip to content

Commit 0b78ed3

Browse files
committed
Breaking: DRE is no longer serializable
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.
1 parent 334ae01 commit 0b78ed3

File tree

2 files changed

+0
-63
lines changed

2 files changed

+0
-63
lines changed

src/Autofac/Core/DependencyResolutionException.cs

-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Autofac Project. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using System.Runtime.Serialization;
5-
64
namespace Autofac.Core;
75

86
/// <summary>
@@ -11,19 +9,8 @@ namespace Autofac.Core;
119
/// been made during the operation. For example, 'on activated' handlers may have already been
1210
/// fired, or 'single instance' components partially constructed.
1311
/// </summary>
14-
[Serializable]
1512
public class DependencyResolutionException : Exception
1613
{
17-
/// <summary>
18-
/// Initializes a new instance of the <see cref="DependencyResolutionException"/> class.
19-
/// </summary>
20-
/// <param name="info">The serialization info.</param>
21-
/// <param name="context">The serialization streaming context.</param>
22-
protected DependencyResolutionException(SerializationInfo info, StreamingContext context)
23-
: base(info, context)
24-
{
25-
}
26-
2714
/// <summary>
2815
/// Initializes a new instance of the <see cref="DependencyResolutionException" /> class.
2916
/// </summary>

test/Autofac.Test/Core/DependencyResolutionExceptionTests.cs

-50
Original file line numberDiff line numberDiff line change
@@ -58,54 +58,4 @@ public void ExceptionMessageUnwrapsNestedResolutionFailures()
5858
Assert.IsType<InvalidOperationException>(inner.InnerException);
5959
Assert.Equal(A.Message, inner.InnerException.Message);
6060
}
61-
62-
#if !NET5_0_OR_GREATER
63-
64-
[Serializable]
65-
public class CustomDependencyResolutionException : DependencyResolutionException
66-
{
67-
public int Value { get; }
68-
69-
public CustomDependencyResolutionException(int value)
70-
: base(null)
71-
{
72-
Value = value;
73-
}
74-
75-
protected CustomDependencyResolutionException(SerializationInfo info, StreamingContext context)
76-
: base(info, context)
77-
{
78-
if (info == null)
79-
{
80-
throw new ArgumentNullException(nameof(info));
81-
}
82-
83-
Value = info.GetInt32(nameof(Value));
84-
}
85-
86-
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
87-
public override void GetObjectData(SerializationInfo info, StreamingContext context)
88-
{
89-
base.GetObjectData(info, context);
90-
91-
info.AddValue(nameof(Value), 123);
92-
}
93-
}
94-
95-
// Consider removing this test, or marking it as .NET Standard Only. Binary formatter serialisation has been obsoleted by Microsoft.
96-
[Fact]
97-
public void SupportCustomRuntimeSerialization()
98-
{
99-
using (var stream = new MemoryStream())
100-
{
101-
var formatter = new BinaryFormatter();
102-
formatter.Serialize(stream, new CustomDependencyResolutionException(123));
103-
104-
stream.Position = 0;
105-
var exception = (CustomDependencyResolutionException)formatter.Deserialize(stream);
106-
107-
Assert.Equal(123, exception.Value);
108-
}
109-
}
110-
#endif
11161
}

0 commit comments

Comments
 (0)