diff --git a/src/Autofac/Builder/RegistrationExtensions.cs b/src/Autofac/Builder/RegistrationExtensions.cs
index ca5723656..c0641a3fd 100644
--- a/src/Autofac/Builder/RegistrationExtensions.cs
+++ b/src/Autofac/Builder/RegistrationExtensions.cs
@@ -25,6 +25,7 @@ public static class RegistrationExtensions
/// Registration builder allowing the registration to be configured.
/// Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType)
{
@@ -48,6 +49,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured.
/// Factory delegates are provided automatically in Autofac 2, and
/// this method is generally not required.
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
RegisterGeneratedFactory(this ContainerBuilder builder, Type delegateType, Service service)
{
@@ -68,6 +70,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured.
/// Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
RegisterGeneratedFactory(this ContainerBuilder builder, Service service)
where TDelegate : class
@@ -88,6 +91,7 @@ public static IRegistrationBuilderRegistration builder allowing the registration to be configured.
/// Factory delegates are provided automatically in Autofac 2,
/// and this method is generally not required.
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
RegisterGeneratedFactory(this ContainerBuilder builder)
where TDelegate : class
@@ -115,6 +119,7 @@ public static IRegistrationBuilder
/// Thrown if is .
///
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
NamedParameterMapping(
this IRegistrationBuilder registration)
@@ -142,6 +147,7 @@ public static IRegistrationBuilder
/// Thrown if is .
///
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
PositionalParameterMapping(
this IRegistrationBuilder registration)
@@ -169,6 +175,7 @@ public static IRegistrationBuilder
/// Thrown if is .
///
+ [Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public static IRegistrationBuilder
TypedParameterMapping(
this IRegistrationBuilder registration)
diff --git a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs
index 5740e2b9e..bd0c5e43b 100644
--- a/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs
+++ b/src/Autofac/Core/Registration/ServiceRegistrationInfo.cs
@@ -177,20 +177,12 @@ public void AddImplementation(IComponentRegistration registration, bool preserve
{
if (originatedFromSource)
{
- if (_sourceImplementations == null)
- {
- _sourceImplementations = new List();
- }
-
+ _sourceImplementations ??= new List();
_sourceImplementations.Add(registration);
}
else
{
- if (_preserveDefaultImplementations == null)
- {
- _preserveDefaultImplementations = new List();
- }
-
+ _preserveDefaultImplementations ??= new List();
_preserveDefaultImplementations.Add(registration);
}
}
@@ -214,11 +206,7 @@ public void AddImplementation(IComponentRegistration registration, bool preserve
/// The insertion mode for the pipeline.
public void UseServiceMiddleware(IResolveMiddleware middleware, MiddlewareInsertionMode insertionMode = MiddlewareInsertionMode.EndOfPhase)
{
- if (_customPipelineBuilder is null)
- {
- _customPipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
- }
-
+ _customPipelineBuilder ??= new ResolvePipelineBuilder(PipelineType.Service);
_customPipelineBuilder.Use(middleware, insertionMode);
}
@@ -234,11 +222,7 @@ public void UseServiceMiddlewareRange(IEnumerable middleware
return;
}
- if (_customPipelineBuilder is null)
- {
- _customPipelineBuilder = new ResolvePipelineBuilder(PipelineType.Service);
- }
-
+ _customPipelineBuilder ??= new ResolvePipelineBuilder(PipelineType.Service);
_customPipelineBuilder.UseRange(middleware, insertionMode);
}
@@ -280,10 +264,7 @@ public void BeginInitialization(IEnumerable sources)
// Build the pipeline during service info initialization, so that sources can access it
// while getting a registration recursively.
- if (_resolvePipeline is null)
- {
- _resolvePipeline = BuildPipeline();
- }
+ _resolvePipeline ??= BuildPipeline();
}
///
diff --git a/src/Autofac/Core/Resolving/SegmentedStack.cs b/src/Autofac/Core/Resolving/SegmentedStack.cs
index 5833d58c2..e75b774d2 100644
--- a/src/Autofac/Core/Resolving/SegmentedStack.cs
+++ b/src/Autofac/Core/Resolving/SegmentedStack.cs
@@ -112,7 +112,7 @@ IEnumerator IEnumerable.GetEnumerator()
return new Enumerator(this);
}
- private struct StackSegment : IDisposable
+ private readonly struct StackSegment : IDisposable
{
private readonly SegmentedStack _stack;
private readonly int _resetPosition;
diff --git a/src/Autofac/Core/ServiceRegistration.cs b/src/Autofac/Core/ServiceRegistration.cs
index 63935cc57..2a51d0e8e 100644
--- a/src/Autofac/Core/ServiceRegistration.cs
+++ b/src/Autofac/Core/ServiceRegistration.cs
@@ -9,7 +9,7 @@ namespace Autofac.Core;
///
/// Defines a combination of a service pipeline and a registration. Used to instantiate a .
///
-public struct ServiceRegistration : IEquatable
+public readonly struct ServiceRegistration : IEquatable
{
///
/// Initializes a new instance of the struct.
diff --git a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs
index 2f815ed20..2fbd78b83 100644
--- a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs
+++ b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryActivatorData.cs
@@ -10,6 +10,7 @@ namespace Autofac.Features.GeneratedFactories;
///
/// Data used to create factory activators.
///
+[Obsolete("Update your code to use the Func implicit relationship or delegate factories. See https://autofac.readthedocs.io/en/latest/resolve/relationships.html and https://autofac.readthedocs.io/en/latest/advanced/delegate-factories.html for more information.")]
public class GeneratedFactoryActivatorData : IConcreteActivatorData
{
private readonly Type _delegateType;
diff --git a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs
index 131aa9fd7..8944ec3eb 100644
--- a/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs
+++ b/src/Autofac/Features/GeneratedFactories/GeneratedFactoryRegistrationExtensions.cs
@@ -6,6 +6,8 @@
namespace Autofac.Features.GeneratedFactories;
+#pragma warning disable CS0618
+
///
/// Helper methods for registering factories.
///
diff --git a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs
index 2075f8dfd..09bb80db7 100644
--- a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs
+++ b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs
@@ -96,16 +96,14 @@ private static void ScanAssemblies(IEnumerable assemblies, IComponentR
.Where(t => t.IsGenericTypeDefinition)
.CanBeRegistered(rb.ActivatorData);
- Func> scannedConstructor =
- (type) => new RegistrationBuilder