gpt4 book ai didi

c# - 在 Autofac 中为同一类型选择多个注册

转载 作者:行者123 更新时间:2023-12-02 04:59:06 26 4
gpt4 key购买 nike

我正在使用带 Autofac 的 MVC4 开发网络应用程序。我有一个全局异常过滤器,我在其中注入(inject)了一个记录器服务,所以我在 App_Start 中像这样初始化它:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(DependencyResolver.Current.GetService<IExceptionFilter>());
}

这是过滤器的总体布局: 公共(public)类 ErrorHandlerAttribute : HandleErrorAttribute { 私有(private)只读 ILoggerService 记录器;

    public ErrorHandlerAttribute(ILoggerService logger)
{
this.logger = logger;
}

public override void OnException(ExceptionContext filterContext)
{
//dostuff
}

public void LogError(ExceptionContext context)
{
try
{
logger.Error(context.Exception.Message, context.Exception);
}
catch (Exception) { }
}
}

如果我不使用 Autofac,我会得到这样的东西:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new ErrorHandlerAttribute());

filters.Add(new ErrorHandlerAttribute
{
View = "UnauthorizedException",
ExceptionType = typeof(UnauthorizedAccessException)
});

filters.Add(new ErrorHandlerAttribute
{
View = "PageNotFound",
ExceptionType = typeof(NotImplementedException)
});
}

ErrorHandlerAttribute 是我的自定义异常过滤器,派生自 MVC 的 HandleErrorAttribute。

我希望能够保留重定向到自定义错误页面的能力,同时使用 Autofac 的注入(inject)和单个过滤器(因为我构建它所以它可以处理任何异常)。不幸的是,尽管我在网上和其他论坛上搜索了可能的解决方案,但我似乎找不到任何方法来做到这一点。我尝试了很多配置更改、不同的注册、集合解析等。

我希望它的工作方式与此类似:

    builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>().InstancePerHttpRequest();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedParameter>() { new NamedParameter("ExceptionType", typeof(UnauthorizedAccessException)), new NamedParameter("View", "UnauthorizedAccess") })
.InstancePerHttpRequest();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedParameter>() { new NamedParameter("ExceptionType", typeof(NotImplementedException)), new NamedParameter("View", "UnderConstruction") })
.InstancePerHttpRequest();
builder.RegisterFilterProvider();

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
DependencyResolver.Current.GetServices<IExceptionFilter>().ForEach(f => filters.Add(f));
}

令人惊讶的是,它编译并运行了,但是所有 3 个 IExceptionFilter 实例都是正常的,默认的 ErrorHandlerAttribute(带有 View="Error"和 ExceptionType=typeof(object))。

我知道 Autofac 将服务的最后一次注册作为默认值,并且我尝试对三个注册中的两个进行注释,以及使用 PreserveExistingDefaults,但我的所有异常过滤器仍然带有默认值。

我是不是误解了 WithProperties 扩展方法,还是有另一种类似的方法来实现我想要的?

编辑 1:

感谢 Alex 的建议,我通过使用 NamedPropertyParameter 并切换语句的顺序解决了这个问题:

builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedPropertyParameter> { new NamedPropertyParameter("ExceptionType", typeof(UnauthorizedAccessException)), new NamedPropertyParameter("View", "UnauthorizedAccess") })
.InstancePerHttpRequest();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedPropertyParameter> { new NamedPropertyParameter("ExceptionType", typeof(NotImplementedException)), new NamedPropertyParameter("View", "UnderConstruction") })
.InstancePerHttpRequest();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>().InstancePerHttpRequest();

最佳答案

您需要使用 NamedPropertyParameter 而不是 NamedParameter

builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.SingleInstance();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedPropertyParameter> { new NamedPropertyParameter("ExceptionType", typeof(UnauthorizedAccessException)), new NamedPropertyParameter("View", "UnauthorizedAccess") })
.SingleInstance();
builder.RegisterType<ErrorHandlerAttribute>().As<IExceptionFilter>()
.WithProperties(new List<NamedPropertyParameter> { new NamedPropertyParameter("ExceptionType", typeof(NotImplementedException)), new NamedPropertyParameter("View", "UnderConstruction") })
.SingleInstance();

您也可以将全局过滤器注册为 SingleInstance,因为它们会被解析并直接添加到过滤器集合中。 MVC 不会为每个 HTTP 请求请求这些过滤器的实例。它只会使用您添加到集合中的实例。

关于c# - 在 Autofac 中为同一类型选择多个注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com