Autofac RegisterInstance 不按预期工作。

huangapple go评论61阅读模式
英文:

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) =&gt;
        WebHost.CreateDefaultBuilder(args)
            .ConfigureServices(s =&gt; s.AddAutofac())
            .UseStartup&lt;Startup&gt;();
}

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&lt;IMemoryCache&gt;(MemoryCacheInstance);
        builder.Populate(services);

        return new AutofacServiceProvider(builder.Build());
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseRouting();
        app.UseEndpoints(endpoints =&gt;
        {
            endpoints.MapControllers();
        });
    }

}

Controller:

public class UtilsController : Controller
{
    public UtilsController(IMemoryCache memoryCache)
    {
        if (Startup.MemoryCacheInstance == memoryCache)
        {
            // the debugger never gets here
        }
    }

    [HttpGet(&quot;api/utils/test-method&quot;)]
    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&lt;IMemoryCache&gt;(MemoryCacheInstance);

huangapple
  • 本文由 发表于 2023年5月23日 00:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定