Innovabit.DotNet.Interceptors.Abstractions 2.0.108

There is a newer version of this package available.
See the version list below for details.
dotnet add package Innovabit.DotNet.Interceptors.Abstractions --version 2.0.108
                    
NuGet\Install-Package Innovabit.DotNet.Interceptors.Abstractions -Version 2.0.108
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Innovabit.DotNet.Interceptors.Abstractions" Version="2.0.108" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Innovabit.DotNet.Interceptors.Abstractions" Version="2.0.108" />
                    
Directory.Packages.props
<PackageReference Include="Innovabit.DotNet.Interceptors.Abstractions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Innovabit.DotNet.Interceptors.Abstractions --version 2.0.108
                    
#r "nuget: Innovabit.DotNet.Interceptors.Abstractions, 2.0.108"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Innovabit.DotNet.Interceptors.Abstractions@2.0.108
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Innovabit.DotNet.Interceptors.Abstractions&version=2.0.108
                    
Install as a Cake Addin
#tool nuget:?package=Innovabit.DotNet.Interceptors.Abstractions&version=2.0.108
                    
Install as a Cake Tool

Innovabit.DotNet.Interceptors.Abstractions

Core building blocks for aspect-oriented programming (AOP) in .NET using Castle DynamicProxy and Microsoft.Extensions.DependencyInjection. This is the base package every other Innovabit.DotNet.Interceptors.* package builds on (logging, validation, notifications, retry, ...).

What it provides

  • InterceptorBase — the abstract base for an interceptor (sync, Task/Task<T> and ValueTask/ValueTask<T> aware).

  • AddInterceptors() / InterceptorBuilder — register the proxy generator and your interceptors.

  • Add*WithInterception / TryAdd*WithInterception — register a service so its interface is proxied and intercepted. The concrete implementation is kept under a private key, so it can't be resolved — and its interception bypassed — by accident.

  • Add*ClassWithInterception / TryAdd*ClassWithInterception — proxy a service that has no interface (class proxy; only virtual members are intercepted).

  • An ordered, multi-interceptor pipeline: InterceptorBase.Order, InterceptorOrders (recommended scale) and InterceptorOrderOptions (per-registration overrides).

  • [Sensitive] / [Mask], RedactionRule and IValueRedactor — mark a parameter, return value, property or field once and every interceptor in the suite redacts it (see below).

  • [Intercepted] + AddInterceptedServicesFromAssembly — register services by scanning instead of a line each (see below).

  • DescribeInterceptorPipeline() / LogInterceptorPipeline() — print what the pipeline actually does, per service and per method.

  • InterceptorToggleOptions + Disable<T>() / Enable<T>() — take an interceptor out of every proxy without touching the registration that put it there (see below).

  • A bundled Roslyn analyzer that flags interceptor attributes with no effect at compile time:

    Rule Flags
    INTX001 An attribute on a static method.
    INTX002 A parameter attribute such as [DoNotLog] on a method that isn't intercepted.
    INTX003 An attribute on a private method — its callers are inside the class, past the proxy.
    INTX004 An attribute on a sealed class with no interface: neither kind of proxy can be built.
    INTX005 [Cache] on a method that returns nothing to store.
    INTX006 [Idempotent] with no KeyFrom.
    INTX007 KeyFrom or PartitionBy naming a parameter the method does not have.

Install

dotnet add package Innovabit.DotNet.Interceptors.Abstractions

Usage

using Innovabit.DotNet.Interceptors;

public sealed class TracingInterceptor : InterceptorBase
{
    public override bool CanIntercept(IInvocation invocation) => true;
    public override void ProcessInterceptionSync(IInvocation invocation) => invocation.Proceed();
    public override Task ProcessInterceptionAsync(IInvocation invocation) { invocation.Proceed(); return (Task)invocation.ReturnValue!; }
    public override Task<TResult> ProcessInterceptionAsync<TResult>(IInvocation invocation) { invocation.Proceed(); return (Task<TResult>)invocation.ReturnValue!; }
}

services.AddInterceptors()
    .Register<TracingInterceptor>();          // or Register<T>(order) to place it in the pipeline

// Proxy IMyService so registered interceptors wrap MyService. Anything that depends on
// IMyService — even services registered without interception — receives the proxied instance.
services.AddScopedWithInterception<IMyService, MyService>();

// No interface? Use a class proxy (only virtual members are intercepted; the class must be non-sealed).
services.AddScopedClassWithInterception<MyService>();

The un-proxied MyService is registered under a private key, so consumers must depend on IMyService (or, for class proxies, MyService itself) and always get the intercepted instance — there is no publicly resolvable concrete to inject and silently skip the pipeline.

Sensitive data

[Sensitive] and [Mask] mark a parameter, a return value (with [return: ...]), a property or a field as something the interceptors may not write down verbatim. Mark it once and it stays redacted in logging, auditing, tracing and exception notifications alike, including in features you adopt later:

[Log]
[Audited]
[NotifyOnException]
public Task<Receipt> ChargeAsync(
    string orderId,
    [Sensitive] string cardHolder,           // recorded as ***
    [Mask(ShowLast = 4)] string cardNumber)  // recorded as ************1111
{ ... }
[Sensitive] [Mask]
Recorded as Placeholder (default ***) The value with only ShowFirst/ShowLast characters left visible
A null value Still replaced, so the record never reveals that the secret was missing Stays null — nothing to hide, no fragment to show
Use it for Passwords, tokens, whole payloads Values where a fragment is genuinely useful: a card, an e-mail, an IBAN

A parameter carrying both is treated as [Sensitive] — between "never record this" and "record a fragment", the stricter rule is the safe reading. [Mask] never reveals a value in full either: if the visible window would cover the whole value, it is masked entirely, so [Mask(ShowLast = 4)] on a four-digit PIN does not print it verbatim.

Inside objects

A parameter-level rule can only cover the argument itself. Put the attributes on the type's own members and the rule travels with the data, however deep inside an argument it turns up:

public class Customer
{
    public string Name { get; set; }

    [Sensitive] public string Email { get; set; }

    [Mask(ShowLast = 4)] public string Phone { get; set; }
}

[Audited]
public Task PlaceAsync(Order order) { ... }   // order.Customer.Email is recorded as ***

Walking the object graph is the serializer's job, not the interceptor's, so this applies wherever a serializer that honours the rules is used. The audit trail's default one (SystemTextJsonAuditSerializer) does, through nested objects and collections alike, and it keeps doing so when you hand it your own JsonSerializerOptions — options that quietly dropped the rules would turn a configuration tweak into a data leak. To honour them in a serializer of your own, add one modifier:

options.TypeInfoResolver = (options.TypeInfoResolver ?? new DefaultJsonTypeInfoResolver())
    .WithAddedModifier(RedactionJsonModifier.Apply);

A redacted value is always written as text, whatever the member's declared type: a masked decimal stops being a number. That is the point — the record keeps the shape of the data, not data you could compute with. Masking uses the invariant culture, so a masked number reads the same whatever machine wrote the record.

What redaction does not touch

  • The call itself. The method receives the real values; only what the interceptors record changes.
  • Cache keys. [Cache] hashes arguments rather than storing them, and redacting first would make two different cards share an entry. Use [CacheKeyIgnore] to shape a key.
  • Validation. [Validate] checks the real value, as it must.

These attributes are about what leaves the process in a record, not about hiding values from your own code.

Custom rules

services.AddInterceptors().UseValueRedactor<HashingRedactor>();

Implement IValueRedactor to hash instead of replacing, to keep a format-preserving mask, or to honour your own data-classification attributes. It is resolved once and must not throw — an interceptor recording a value is never a good reason to fail the call it is observing.

Registering by convention

A registration line per service adds up. Mark the classes instead and scan once:

[Intercepted]
public class OrderService : IOrderService { ... }
services.AddInterceptedServicesFromAssemblyContaining<Program>();

A marked class is registered under the single interface it implements, or — when it implements none — as a class proxy of itself. One that implements several has to say which it means with [Intercepted(ServiceType = typeof(IOrderService))]: guessing would pick a different one the day somebody adds IDisposable. Lifetime comes from [Intercepted(ServiceLifetime.Singleton)] or the scanner's default (Scoped).

Option Meaning
DefaultLifetime For classes that do not name one. Defaults to Scoped.
UseNameConvention() Also register unmarked OrderService → IOrderService pairs.
Filter Applied first, so an excluded class cannot fail the scan on its way past.
Replace false by default, so a hand-written registration is left alone.

The attribute is opt-in on purpose. Scanning every IFoo/Foo pair also registers the ones you did not mean to intercept, and finding that out costs a debugging session; one attribute is still one line, and it says what it does where the service is.

Seeing what is actually intercepted

Ordering is the part of this suite hardest to reason about from the source: the numbers live on the interceptors, the overrides live in DI, and whether one applies to a given method lives in its CanIntercept. This puts all three in one place:

var host = builder.Build();
host.Services.LogInterceptorPipeline(Console.WriteLine);
Interceptor pipeline (4 interceptor(s), outermost first)
    -200  TraceInterceptor
       0  LogInterceptor
     200  CacheInterceptor
     500  RetryInterceptor

Intercepted services (2)
  IOrderService -> OrderService (Scoped)
      PlaceAsync                       TraceInterceptor, LogInterceptor, RetryInterceptor
      CountAsync                       TraceInterceptor
  ReportBuilder (class proxy, Transient)
      BuildAsync                       LogInterceptor
      Validate                         not reachable (not virtual, behind a class proxy)

The report asks each interceptor rather than reading the attributes, so it describes what the pipeline does rather than what the attributes suggest — and the two drift the moment a CanIntercept looks at anything else. DescribeInterceptorPipeline() returns the same thing as data when you want to assert on it in a test.

That last line is the one worth having: a non-virtual member behind a class proxy can never be intercepted, so any attribute on it silently does nothing.

Pipeline order

When several interceptors are registered they run as a pipeline. InterceptorBase.Order controls position: lower runs first (outermost); higher runs closer to the intercepted method. Ties keep registration order. Override per registration with Register<T>(int order) or SetOrder<T>(int order). See InterceptorOrders for the recommended scale.

Switching an interceptor off

services.AddInterceptors()
    .AddLogging()
    .AddTracing()
    .Disable<TraceInterceptor>();   // registered, and left out of every proxy

The toggle is read when a proxy is built, not when the interceptor is registered, so it can also be flipped afterwards — through InterceptorToggleOptions resolved from the container, or from appsettings.json with the Configuration package. A proxy keeps the pipeline it was built with, so a change reaches services resolved after it: the next request, for a scoped service, and never a singleton already created.

Disabled interceptors are still listed by LogInterceptorPipeline(), marked (disabled) — so "it is registered and does nothing" has a visible answer.

Part of

The Innovabit.DotNet.Interceptors suite — github.com/Innovabit/dotnet-interceptors.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (15)

Showing the top 5 NuGet packages that depend on Innovabit.DotNet.Interceptors.Abstractions:

Package Downloads
Innovabit.DotNet.Interceptors.Logging.Abstractions

Base package for logging using interceptor.

Innovabit.DotNet.Interceptors.Collections

Package for some useful interceptors like retry on exception.

Innovabit.DotNet.Interceptors.Notifications.Abstractions

Base package for sending notifications when an intercepted method throws an exception.

Innovabit.DotNet.Interceptors.Validation.Abstractions

Package for data validation using interceptor.

Innovabit.DotNet.Interceptors.Caching.Abstractions

Base package for method result caching using interceptors.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.119 118 11/14/2026
2.0.118 806 11/7/2026
2.0.117 818 10/31/2026
2.0.116 768 10/26/2026
2.0.115 749 10/17/2026
2.0.114 724 10/10/2026
2.0.113 762 10/3/2026
2.0.112 722 9/28/2026
2.0.111 688 9/28/2026
2.0.110 702 9/27/2026
2.0.109 719 9/26/2026
2.0.108 698 9/26/2026
2.0.107 763 9/26/2026
2.0.106 574 9/25/2026
2.0.105 705 9/25/2026
1.0.0.104 669 9/25/2026
1.0.0.103 542 9/25/2026
1.0.0.102 412 9/25/2026
1.0.0.101 401 9/25/2026
1.0.0.100 402 9/25/2026
Loading failed