Skip to content

Commit 496e249

Browse files
authored
Remove unnecessary trimming workarounds (#47241)
dotnet/linker#1981 has been fixed. We can now remove these workarounds in aspnetcore.
1 parent de8a676 commit 496e249

File tree

7 files changed

+8
-85
lines changed

7 files changed

+8
-85
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,12 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
169169
// UseStartup can be called multiple times. Only run the last one.
170170
_startupObject = startupType;
171171

172-
var state = new UseStartupState(startupType);
173-
174172
_builder.ConfigureServices((context, services) =>
175173
{
176174
// Run this delegate if the startup type matches
177-
if (object.ReferenceEquals(_startupObject, state.StartupType))
175+
if (object.ReferenceEquals(_startupObject, startupType))
178176
{
179-
UseStartup(state.StartupType, context, services);
177+
UseStartup(startupType, context, services);
180178
}
181179
});
182180

src/Hosting/Hosting/src/Internal/UseStartupState.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Hosting/Hosting/src/WebHostBuilderExtensions.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,19 @@ public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, [Dyna
144144

145145
hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);
146146

147-
var state = new UseStartupState(startupType);
148-
149147
return hostBuilder
150148
.ConfigureServices(services =>
151149
{
152-
if (typeof(IStartup).IsAssignableFrom(state.StartupType))
150+
if (typeof(IStartup).IsAssignableFrom(startupType))
153151
{
154-
services.AddSingleton(typeof(IStartup), state.StartupType);
152+
services.AddSingleton(typeof(IStartup), startupType);
155153
}
156154
else
157155
{
158156
services.AddSingleton(typeof(IStartup), sp =>
159157
{
160158
var hostingEnvironment = sp.GetRequiredService<IHostEnvironment>();
161-
return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, state.StartupType, hostingEnvironment.EnvironmentName));
159+
return new ConventionBasedStartup(StartupLoader.LoadMethods(sp, startupType, hostingEnvironment.EnvironmentName));
162160
});
163161
}
164162
});

src/Http/Authentication.Abstractions/src/AuthenticationOptions.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,13 @@ public void AddScheme(string name, Action<AuthenticationSchemeBuilder> configure
5353
/// <param name="displayName">The display name for the scheme.</param>
5454
public void AddScheme<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] THandler>(string name, string? displayName) where THandler : IAuthenticationHandler
5555
{
56-
var state = new AddSchemeState(typeof(THandler));
5756
AddScheme(name, b =>
5857
{
5958
b.DisplayName = displayName;
60-
b.HandlerType = state.HandlerType;
59+
b.HandlerType = typeof(THandler);
6160
});
6261
}
6362

64-
// Workaround for linker bug: https://github.com/dotnet/linker/issues/1981
65-
private readonly struct AddSchemeState
66-
{
67-
public AddSchemeState([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType)
68-
{
69-
HandlerType = handlerType;
70-
}
71-
72-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
73-
public Type HandlerType { get; }
74-
}
75-
7663
/// <summary>
7764
/// Used as the fallback default scheme for all the other defaults.
7865
/// </summary>

src/Http/Http.Abstractions/src/Extensions/UseMiddlewareExtensions.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,8 @@ public static IApplicationBuilder UseMiddleware(
9494
throw new InvalidOperationException(Resources.FormatException_UseMiddlewareNoParameters(InvokeMethodName, InvokeAsyncMethodName, nameof(HttpContext)));
9595
}
9696

97-
var state = new InvokeMiddlewareState(middleware);
98-
9997
return app.Use(next =>
10098
{
101-
var middleware = state.Middleware;
102-
10399
var ctorArgs = new object[args.Length + 1];
104100
ctorArgs[0] = next;
105101
Array.Copy(args, 0, ctorArgs, 1, args.Length);
@@ -268,16 +264,4 @@ private static object GetService(IServiceProvider sp, Type type, Type middleware
268264

269265
return service;
270266
}
271-
272-
// Workaround for linker bug: https://github.com/dotnet/linker/issues/1981
273-
private readonly struct InvokeMiddlewareState
274-
{
275-
public InvokeMiddlewareState([DynamicallyAccessedMembers(MiddlewareAccessibility)] Type middleware)
276-
{
277-
Middleware = middleware;
278-
}
279-
280-
[DynamicallyAccessedMembers(MiddlewareAccessibility)]
281-
public Type Middleware { get; }
282-
}
283267
}

src/Middleware/RateLimiting/src/RateLimiterOptions.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ public RateLimiterOptions AddPolicy<TPartitionKey>(string policyName, Func<HttpC
7373
throw new ArgumentException($"There already exists a policy with the name {policyName}.", nameof(policyName));
7474
}
7575

76-
var policyType = new PolicyTypeState(typeof(TPolicy));
7776
Func<IServiceProvider, DefaultRateLimiterPolicy> policyFunc = serviceProvider =>
7877
{
79-
var instance = (IRateLimiterPolicy<TPartitionKey>)ActivatorUtilities.CreateInstance(serviceProvider, policyType.PolicyType);
78+
var instance = (IRateLimiterPolicy<TPartitionKey>)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TPolicy));
8079
return new DefaultRateLimiterPolicy(ConvertPartitioner<TPartitionKey>(policyName, instance.GetPartition), instance.OnRejected);
8180
};
8281

@@ -116,16 +115,4 @@ internal static Func<HttpContext, RateLimitPartition<DefaultKeyType>> ConvertPar
116115
return new RateLimitPartition<DefaultKeyType>(partitionKey, static key => ((Func<TPartitionKey, RateLimiter>)key.Factory!)((TPartitionKey)key.Key!));
117116
};
118117
}
119-
120-
// Workaround for linker bug: https://github.com/dotnet/linker/issues/1981
121-
private readonly struct PolicyTypeState
122-
{
123-
public PolicyTypeState([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type policyType)
124-
{
125-
PolicyType = policyType;
126-
}
127-
128-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
129-
public Type PolicyType { get; }
130-
}
131118
}

src/Security/Authentication/Core/src/AuthenticationBuilder.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ public AuthenticationBuilder(IServiceCollection services)
3131
where TOptions : AuthenticationSchemeOptions, new()
3232
where THandler : class, IAuthenticationHandler
3333
{
34-
var state = new AddSchemeHelperState(typeof(THandler));
3534
Services.Configure<AuthenticationOptions>(o =>
3635
{
3736
o.AddScheme(authenticationScheme, scheme =>
3837
{
39-
scheme.HandlerType = state.HandlerType;
38+
scheme.HandlerType = typeof(THandler);
4039
scheme.DisplayName = displayName;
4140
});
4241
});
@@ -53,18 +52,6 @@ public AuthenticationBuilder(IServiceCollection services)
5352
return this;
5453
}
5554

56-
// Workaround for linker bug: https://github.com/dotnet/linker/issues/1981
57-
private readonly struct AddSchemeHelperState
58-
{
59-
public AddSchemeHelperState([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type handlerType)
60-
{
61-
HandlerType = handlerType;
62-
}
63-
64-
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
65-
public Type HandlerType { get; }
66-
}
67-
6855
/// <summary>
6956
/// Adds a <see cref="AuthenticationScheme"/> which can be used by <see cref="IAuthenticationService"/>.
7057
/// </summary>

0 commit comments

Comments
 (0)