Create a simple mvc project and add a lot of controllers.
Controller.cs
public abstract class MyControllerBase : Controller
{
}
[Route("api/testapi/test0api")]
public class Test0ControllerBase : MyControllerBase
{
[HttpGet]
[Route("Get")]
public string TestMethod()
{
return "test";
}
}
[Route("api/testapi/test1api")]
public class Test1ControllerBase : MyControllerBase
{
[HttpGet]
[Route("Get")]
public string TestMethod()
{
return "test";
}
}
//Test100ControllerBase
Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webHostBuilder =>
{
webHostBuilder.UseStartup<Startup>();
})
.Build();
host.Run();
}
}
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void ConfigureContainer(ContainerBuilder builder)
{
foreach (var type in Assembly.GetExecutingAssembly().GetTypes()
.Where(x => typeof(MyControllerBase).IsAssignableFrom(x)))
{
var stopwatch = Stopwatch.StartNew();
builder.RegisterType(type)
.As(type)
.EnableClassInterceptors();
stopwatch.Stop();
Console.WriteLine(type.Name + " => ElapsedMilliseconds:" + stopwatch.ElapsedMilliseconds);
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
My code enables class interception for each controller.
TargetFramework: netcoreapp3.1
Autofac.Extensions.DependencyInjection version 6.0.0 Autofac.Extras.DynamicProxy version 5.0.0
The steps to reproduce the problem are:
The source code of the project that can reproduce the problem: https://github.com/maliming/ClassInterceptorTest
Create a simple mvc project and add a lot of controllers.
Controller.cs
Program.cs
Startup.cs
My code enables class interception for each controller.
Ideally, the execution time of the
EnableClassInterceptors
method should be about the same.But the result is that time is gradually increasing.
Console output: