-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathServiceCollectionExtensions.cs
35 lines (29 loc) · 1.18 KB
/
ServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
namespace TestBuildingBlocks;
internal static class ServiceCollectionExtensions
{
public static void ReplaceControllers(this IServiceCollection services, TestControllerProvider provider)
{
services.AddMvcCore().ConfigureApplicationPartManager(manager =>
{
RemoveExistingControllerFeatureProviders(manager);
foreach (Assembly assembly in provider.ControllerAssemblies)
{
manager.ApplicationParts.Add(new AssemblyPart(assembly));
}
manager.FeatureProviders.Add(provider);
});
}
private static void RemoveExistingControllerFeatureProviders(ApplicationPartManager manager)
{
IApplicationFeatureProvider<ControllerFeature>[] providers = manager.FeatureProviders.OfType<IApplicationFeatureProvider<ControllerFeature>>()
.ToArray();
foreach (IApplicationFeatureProvider<ControllerFeature> provider in providers)
{
manager.FeatureProviders.Remove(provider);
}
}
}