英文:
Autofac RegisterInstance does not work as expected
问题
In my ASP.NET Core application there is a IMemoryCache
registration as an instance in startup. When IMemoryCache
is injected into a controller, the controller receives a different instance of memory cache than registered. How is this possible when IMemoryCache
was registered as an instance?
Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(s => s.AddAutofac())
.UseStartup<Startup>();
}
Startup.cs:
public class Startup
{
public static IMemoryCache MemoryCacheInstance;
public Startup()
{
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
MemoryCacheInstance = new MemoryCache(new MemoryCacheOptions());
var builder = new ContainerBuilder();
builder.RegisterInstance<IMemoryCache>(MemoryCacheInstance);
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Controller:
public class UtilsController : Controller
{
public UtilsController(IMemoryCache memoryCache)
{
if (Startup.MemoryCacheInstance == memoryCache)
{
// the debugger never gets here
}
}
[HttpGet("api/utils/test-method")]
public string TestMethod()
{
return null;
}
}
英文:
In my ASP.NET Core application there is a IMemoryCache
registration as an instance in startup. When IMemoryCache
is injected into a controller, the controller receives a different instance of memory cache than registered. How is this possible when IMemoryCache
was registered as an instance?
Program.cs:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(s => s.AddAutofac())
.UseStartup<Startup>();
}
Startup.cs:
public class Startup
{
public static IMemoryCache MemoryCacheInstance;
public Startup()
{
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
MemoryCacheInstance = new MemoryCache(new MemoryCacheOptions());
var builder = new ContainerBuilder();
builder.RegisterInstance<IMemoryCache>(MemoryCacheInstance);
builder.Populate(services);
return new AutofacServiceProvider(builder.Build());
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Controller:
public class UtilsController : Controller
{
public UtilsController(IMemoryCache memoryCache)
{
if (Startup.MemoryCacheInstance == memoryCache)
{
// the debugger never gets here
}
}
[HttpGet("api/utils/test-method")]
public string TestMethod()
{
return null;
}
}
答案1
得分: 1
你需要修复注册顺序。似乎services
已经注册了IMemoryCache
,所以builder.Populate(services);
将会覆盖你创建的那个。尝试:
builder.Populate(services);
builder.RegisterInstance<IMemoryCache>(MemoryCacheInstance);
英文:
You need to fix the order of registration. It seems that services
already has a registered IMemoryCache
so builder.Populate(services);
will override the one you have created. Try:
builder.Populate(services);
builder.RegisterInstance<IMemoryCache>(MemoryCacheInstance);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论