Last active
August 18, 2020 19:04
-
-
Save zHaytam/c5b1c5eeaf2f915dd416ed1ae1fee75c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class ServicesExtensions | |
{ | |
public static void AddProxiedScoped<TInterface, TImplementation>(this IServiceCollection services) | |
where TInterface : class | |
where TImplementation : class, TInterface | |
{ | |
services.AddScoped<TImplementation>(); | |
services.AddScoped(typeof(TInterface), serviceProvider => | |
{ | |
var proxyGenerator = serviceProvider.GetRequiredService<ProxyGenerator>(); | |
var actual = serviceProvider.GetRequiredService<TImplementation>(); | |
var interceptors = serviceProvider.GetServices<IInterceptor>().ToArray(); | |
return proxyGenerator.CreateInterfaceProxyWithTarget(typeof(TInterface), actual, interceptors); | |
}); | |
} | |
} | |
// In ConfigureServices | |
services.AddProxiedScoped<IBlogService, BlogService>(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment