c# - Using DI in SaveChangesInterceptor - Stack Overflow

I have an Entity Framework Core interceptor that has to get some configuration that's declared as

I have an Entity Framework Core interceptor that has to get some configuration that's declared as so:

builder.Services.Configure<AppConfig>(builder.Configuration.GetSection("AppConfig"));

I'm having a hard time registering the interceptor since I can't use the AppConfig when overriding OnConfiguring.

I found a way to add it when configuring services here and followed this SO question. The solution can't work here because (I presume) the AppConfig is only created after building.

var someInterceptor = new SomeInterceptor(/*this takes AppConfig as an argument*/);
builder.Services.AddSingleton(someInterceptor);

....AddInterceptors(someInterceptor);

What can I do with this? Is it possible to inject this into the interceptor?

I have an Entity Framework Core interceptor that has to get some configuration that's declared as so:

builder.Services.Configure<AppConfig>(builder.Configuration.GetSection("AppConfig"));

I'm having a hard time registering the interceptor since I can't use the AppConfig when overriding OnConfiguring.

I found a way to add it when configuring services here and followed this SO question. The solution can't work here because (I presume) the AppConfig is only created after building.

var someInterceptor = new SomeInterceptor(/*this takes AppConfig as an argument*/);
builder.Services.AddSingleton(someInterceptor);

....AddInterceptors(someInterceptor);

What can I do with this? Is it possible to inject this into the interceptor?

Share Improve this question edited Mar 13 at 1:39 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 2 at 22:43 davedave 155 bronze badges 1
  • The answer by Ivan seems good... if you're able to use recent versions of .NET. Otherwise what about something like var myConfig = JsonSerializer.Deserialize(File.ReadAllText("appconfig.sux.json"));? – The Thirsty Ape Commented Mar 3 at 3:28
Add a comment  | 

1 Answer 1

Reset to default 2

Actually, in more modern times (.NET6+ with ConfigurationManager) you don't need to build the configuration to get values from it. You can just:

var appConfig = builder.Configuration.GetSection("AppConfig").Get<AppConfig>();
var someInterceptor = new SomeInterceptor(appConfig);
builder.Services.AddSingleton(someInterceptor);

That being said, in the blogpost you linked, there is a cleaner approach used -

services.AddDbContext<AppDbContext>((provider, options) =>
{
    options.UseSqlServer(Configuration.GetConnectionString("<connection-string-name>"));

    // 3. Resolve the interceptor from the service provider
    options.AddInterceptors(provider.GetRequiredService<AadAuthenticationDbConnectionInterceptor>());
});

if you make IOptions<AppConfig> (or another options-pattern type with appropriate lifetime for your use case) a dependency of the interceptor it will get resolved correctly.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115558a4612106.html

相关推荐

  • c# - Using DI in SaveChangesInterceptor - Stack Overflow

    I have an Entity Framework Core interceptor that has to get some configuration that's declared as

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信