- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Autofac DynamicProxy2 中使用接口(interface)拦截器,我能够为每个寄存器启用接口(interface)拦截器:
var registration = builder.RegisterType<AType>().As<AInterface>();
registration.EnableInterfaceInterceptors().InterceptedBy<AInterceptor>()
我想将某些特性应用于所有已注册的类型。像这样的东西:
var registrations = builder.GetAllRegistrations(); // ops, this does not exist...
foreach (var registration in registrations) {
registration.EnableInterfaceInterceptors().InterceptedBy<AInterceptor>()
}
我找不到获取所有注册的方法。我知道我们可以做到:
builder.RegisterCallback(cr =>
{
foreach (var registration in cr.Registrations)
{
// registration is IComponentRegistration
}
});
但是这里的注册是一个IComponentRegistration
,我需要一个IRegistrationBuilder
来应用EnableInterfaceInterceptors()
。
最佳答案
您可以动态添加拦截器,但这需要一些工作。要走的路是创建一个自定义 Autofac.Module
附加到所有组件注册。我将在示例中向您展示。
您实际上不能在全局范围内执行 EnableInterfaceInterceptors
。我会在示例的末尾进行说明。
首先,示例设置:我们有一个简单的接口(interface)、一个简单的实现和一个将处理日志记录调用的拦截器。 (我正在窃取拦截器代码 from the Autofac wiki ):
public interface IInterface
{
void DoWork();
}
public class Implementation : IInterface
{
public void DoWork()
{
Console.WriteLine("Implementation doing work.");
}
}
public class CallLogger : IInterceptor
{
TextWriter _output;
public CallLogger(TextWriter output)
{
_output = output;
}
public void Intercept(IInvocation invocation)
{
_output.WriteLine("Calling method {0} with parameters {1}... ",
invocation.Method.Name,
string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));
invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
}
}
我们想用我们的调用记录器拦截一切(启用拦截器的)。我们通过创建一个自定义的 Autofac.Module
来实现,它都注册了拦截器本身Autofac 并动态附加到组件注册以添加拦截器元数据。
警告:这里有一些黑客攻击,但有点像将数据“戳”到某个已知位置。它有效,我不知道它为什么会改变,但请注意,因为它有点像在处理“私有(private)化”的东西,所以这可能会在未来的版本中中断。请注意。
好的,免责声明完成。这是模块:
public class InterceptorModule : Autofac.Module
{
// This is a private constant from the Autofac.Extras.DynamicProxy2 assembly
// that is needed to "poke" interceptors into registrations.
const string InterceptorsPropertyName = "Autofac.Extras.DynamicProxy2.RegistrationExtensions.InterceptorsPropertyName";
protected override void Load(ContainerBuilder builder)
{
// Register global interceptors here.
builder.Register(c => new CallLogger(Console.Out));
}
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
// Here is where you define your "global interceptor list"
var interceptorServices = new Service[] { new TypedService(typeof(CallLogger)) };
// Append the global interceptors to any existing list, or create a new interceptor
// list if none are specified. Note this info will only be used by registrations
// that are set to have interceptors enabled. It'll be ignored by others.
object existing;
if (registration.Metadata.TryGetValue(InterceptorsPropertyName, out existing))
{
registration.Metadata[InterceptorsPropertyName] =
((IEnumerable<Service>)existing).Concat(interceptorServices).Distinct();
}
else
{
registration.Metadata.Add(InterceptorsPropertyName, interceptorServices);
}
}
}
为了使其工作,您需要将模块与其他依赖项一起注册。对于这个例子,它看起来像: var builder = new ContainerBuilder();
// Notice this registration doesn't include
// the interceptor - that gets added by the
// module.
builder.RegisterType<Implementation>()
.As<IInterface>()
.EnableInterfaceInterceptors();
// Here's the magic module:
builder.RegisterModule<InterceptorModule>();
var container = builder.Build();
如果您运行这些注册并解决...
var impl = container.Resolve<IInterface>();
impl.DoWork();
您可以看到拦截器工作,您将看到控制台输出:
Calling method DoWork with parameters ...
Implementation doing work.
Done: result was .
(这有点奇怪,因为我的示例中有一个无参数/void 方法,但拦截器正在工作!)
至于EnableInterfaceInterceptors
调用... 执行EnableInterfaceInterceptors
或EnableClassInterceptors
实际上做了很多疯狂的DynamicProxy2在后端工作。它将一些重要的事件处理程序添加到组件上的激活事件中,将对象包装在动态代理中。这些事件处理程序当前未公开供单独使用,我不确定尝试并“事后”附加所有这些东西需要多少工作,就像我们在这里使用实际拦截器所做的那样。
欢迎您亲自尝试 - the source is on GitHub .但是,基本上,虽然“添加一个全局拦截器”是有效的,但在模块中执行全局 EnableInterfaceInterceptors
却很不寻常。你肯定会独自一人。
关于c# - 带有 Autofac.Extras.DynamicProxy2 的 Autofac 全局接口(interface)拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22782086/
我有一个泛型类( GenericClass ),它依赖于泛型类型( IGenericDependency )。这种依赖也是通用的。 public class GenericClass {
虽然我通常认为 Autofac 文档(在 wiki 上)很有帮助,但关于 XML 配置和模块的部分对我来说有点不清楚。现在,我有一个示例工作(我在下面介绍),但我不确定它是否代表了 Autofac 上
我是 autofac 的新手(使用 2.1.14.854),我仍在努力理解 我有一个接口(interface),这个接口(interface)有一个或多个实现,并且应该按特定顺序触发这些实现。 例如:
使用Autofac,可以做到这一点吗?我没有实例的类型。 var instance = container.Resolve("someStringKey"); 从一些谷歌搜索来看,这似乎不可能,但我想
我有一个进行数据处理的应用程序。有 class Pipeline { IEnumerable Filters {get; set;} 我将过滤器实现注册为 builder.RegisterType
我正在尝试将域事件引入项目中。 Udi Dahan 的帖子 - http://www.udidahan.com/2009/06/14/domain-events-salvation/ 中描述了这一概念
假设这个场景: public class B {}; public class C { public C(B b){} } 要从 Autofac 容器解析 C,我必须将 B 和 C 注册到容
我正在为第 3 方应用程序构建一个插件,我的插件使用 Autofac 来连接各种组件。容器是在应用程序启动时构建的,但主机应用程序稍后会调用我的命令。 调用命令时,主机应用程序会提供一些它定义的类型实
我有一个银行/集合,它在内存中缓存对象的实例,以便每个请求不需要返回到数据存储。我希望 Autofac 提供该银行的一个实例,但在 x 秒后使其过期,以便在下一个请求时创建一个新实例。我很难理解如何设
有人可以用简单的英语解释一下我放置问号的代码行的作用吗?或者给我指点一篇阐明这一点的文章。此代码用于在 autofac 容器中注册依赖项 var builder = new Autofac.Conta
我有以下类(class): public class Errors { private readonly string _connectionString; public Errors
假设所有依赖项都已在程序开始时注册。在程序的后面部分,如何使用 AutoFac 使用无参数构造函数创建一个新对象并将注册的属性注入(inject)到该对象中? 最佳答案 您可以使用 Propertie
我使用以下代码进行注册: builder.RegisterType().Named(".VCF"); builder.RegisterType().Named(".VCARD
我有一个工厂接口(interface)(连同具体实现): // foo.dll interface IFooProvider { T GetFoo() where T : Ba
我遇到的情况是,发布者和消费者位于同一个应用程序中。我正在使用 autofac。 据我了解,我需要 2 个具有 2 个端点的总线实例,一个用于发布者,一个用于订阅者。 我正在使用 autofac,但我
我正在使用 Autofac 构造函数注入(inject)。我需要弄清楚如何将单个对象实例注入(inject)多个构造函数参数,而不需要在容器设置阶段显式解析每个参数。 我有一个复杂的场景,可以通过这种
我们在我们的 PCL (Profile259) 中使用 Autofac 3.5.2,Nuget 告诉我们版本 4.1 可用,但当我们尝试更新它时,Nuget 失败并显示以下消息: “您正在尝试将此软件
我的情况是发布者和消费者坐在同一个应用程序中。我正在使用 autofac。 据我了解,我需要 2 个具有 2 个端点的总线实例,一个用于发布者,一个用于订阅者。 我正在使用 autofac,但我不知道
在 Autofac 2.1 的 beta 版本中,支持自动解析 Lazy如 Nicholas Blumhardt 的 Lazing Around with Autofac 中所述博客文章。 The c
我有这个类要在单元测试中实例化: public class Customer { internal Customer(Guid id) { // initialize prop
我是一名优秀的程序员,十分优秀!