https://github.com/moattarwork/foil
Extensions to allow interception for IServiceCollection in .Net standard and core
https://github.com/moattarwork/foil
aspnet-core aspnetcore castle-core csharp dependency-injection dotnet-core dotnet-standard inteceptor
Last synced: 11 months ago
JSON representation
Extensions to allow interception for IServiceCollection in .Net standard and core
- Host: GitHub
- URL: https://github.com/moattarwork/foil
- Owner: moattarwork
- License: apache-2.0
- Created: 2017-09-05T20:12:53.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T09:00:39.000Z (over 3 years ago)
- Last Synced: 2025-03-27T23:03:21.774Z (over 1 year ago)
- Topics: aspnet-core, aspnetcore, castle-core, csharp, dependency-injection, dotnet-core, dotnet-standard, inteceptor
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 14
- Watchers: 5
- Forks: 4
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# foil
[](https://ci.appveyor.com/project/moattarwork/foil-nha98)
[](https://www.nuget.org/packages/Foil/)
foil is a set of extensions which enable interception support for .Net Core dependency injection framework. It uses Castle Core framework to enable on the fly proxy creation of container elements.
The package can be downloaded from NuGet using
```
install-package Foil
install-package Foil.Logging
```
or
```
dotnet add package Foil
dotnet add package Foil.Logging
```
## Usage
The package consists of extensions to register services as Transient, Scoped or Singleton with the interceptors.
```
services.AddTransientWithInterception(m => m.InterceptBy()
.UseMethodConvention());
```
or
```
services.AddSingletonWithInterception(m => m.InterceptBy()
.UseMethodConvention());
```
Convention give this option to specify which methods need to be selected for interception. The are couple of predefined convention which can be used:
- AllMethodsConvention (Default)
- NonQueryMethodsConvention
Custom conventions can be provided by implementing IMethodConvention.
## Code sample
class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddTransientWithInterception(m => m.InterceptBy());
var provider = services.BuildServiceProvider();
var service = provider.GetRequiredService();
service.Call();
}
}
public interface ISampleService
{
void Call();
string State { get; }
}
public class SampleService : ISampleService
{
public string State { get; private set; } = string.Empty;
public virtual void Call()
{
State = "Changed";
Console.WriteLine("Hello Sample");
}
}
public class LogInterceptor : IInterceptor
{
private readonly ISampleLogger _logger;
public LogInterceptor(ISampleLogger logger)
{
_logger = logger;
}
public virtual void Intercept(IInvocation invocation)
{
_logger.Log("Before invocation");
invocation.Proceed();
_logger.Log("After invocation");
}
}