AutoFac-从.NET Core 2.2升级到3.1

发布于 2025-01-28 17:36:31 字数 768 浏览 2 评论 0原文

我正在关注autofac 指南要根据他们的指南迁移到.NET 3.1

,我需要(除其他外)此功能:

public void ConfigureContainer(ContainerBuilder builder)

这将被称为自动称为,

问题在于它内部的代码正在注册服务服务是否有条件适合我们的应用程序,因此我需要将布尔值传递给该功能

public void ConfigureContainer(ContainerBuilder builder)
{
 if (enableTokenAutoRefresh)
 {
   builder.RegisterType<AuthenticationWrapper>()
    .As<IApiProxy>()
 }
 else
 {
   builder.RegisterType<ApiProxy>()
    .As<IApiProxy>()
 }
}

我可以在 configurecontainer 方法中添加布尔值吗? 似乎这会打破呼吁吗? 如果没有 - 如何将数据传递给该数据?

请帮忙

i'm following Autofac guide to migrate to .net 3.1

According to their guide, I need to add (among other things) this function:

public void ConfigureContainer(ContainerBuilder builder)

And this will be called automatically

The problem is that the code inside it that is registering the services is conditional for our app so I need to pass a boolean to the function

for example:

public void ConfigureContainer(ContainerBuilder builder)
{
 if (enableTokenAutoRefresh)
 {
   builder.RegisterType<AuthenticationWrapper>()
    .As<IApiProxy>()
 }
 else
 {
   builder.RegisterType<ApiProxy>()
    .As<IApiProxy>()
 }
}

Can I just add a boolean to the ConfigureContainer method?
seems this will break the calling for it?
and if not - how to pass data to it ?

please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

許願樹丅啲祈禱 2025-02-04 17:36:31

configurecontainer,由ASP.NET核心框架提供,仅采用DI Framework的容器构建器类型(在这种情况下为AutoFac ContainerBuilder)。要在其中获取其他数据,您需要在startup类上设置一个变量,然后在启动管道中的某个地方设置并使用它。

// NOT a complete Startup, but gives you the idea.
public class Startup
{
  public Startup(IConfiguration config)
  {
    // appSettings.json has the value you want
    this.EnableTokenAutoRefresh = config.GetValue<bool>("path:to:key");
  }

  public bool EnableTokenAutoRefresh { get; set; }

  public void ConfigureContainer(ContainerBuilder builder)
  {
    if(this.EnableTokenAutoRefresh)
    {
      // Do what you need to based on config
    }
  }
}

ConfigureContainer, provided by the ASP.NET Core framework, only takes the container builder type of your DI framework (in this case, an Autofac ContainerBuilder). To get additional data in there, you'd need to set a variable on the Startup class somewhere earlier in the startup pipeline and use it.

// NOT a complete Startup, but gives you the idea.
public class Startup
{
  public Startup(IConfiguration config)
  {
    // appSettings.json has the value you want
    this.EnableTokenAutoRefresh = config.GetValue<bool>("path:to:key");
  }

  public bool EnableTokenAutoRefresh { get; set; }

  public void ConfigureContainer(ContainerBuilder builder)
  {
    if(this.EnableTokenAutoRefresh)
    {
      // Do what you need to based on config
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文