allow to add delegating handlers by type (#943)

This commit is contained in:
Marcelo Castagna
2019-07-01 08:23:06 -03:00
committed by GitHub
parent c08f873dab
commit 176a7bb960
3 changed files with 58 additions and 0 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Middleware.Multiplexer;
using System;
using System.Net.Http;
namespace Ocelot.DependencyInjection
@ -13,6 +14,8 @@ namespace Ocelot.DependencyInjection
IMvcCoreBuilder MvcCoreBuilder { get; }
IOcelotBuilder AddDelegatingHandler(Type type, bool global = false);
IOcelotBuilder AddDelegatingHandler<T>(bool global = false)
where T : DelegatingHandler;

View File

@ -35,6 +35,7 @@ namespace Ocelot.DependencyInjection
using Ocelot.Security;
using Ocelot.Security.IPSecurity;
using Ocelot.ServiceDiscovery;
using System;
using System.Net.Http;
using System.Reflection;
@ -165,6 +166,28 @@ namespace Ocelot.DependencyInjection
Services.TryAddSingleton<ISecurityPolicy, IPSecurityPolicy>();
}
public IOcelotBuilder AddDelegatingHandler(Type delegateType, bool global = false)
{
if (!typeof(DelegatingHandler).IsAssignableFrom(delegateType)) throw new ArgumentOutOfRangeException(nameof(delegateType), delegateType.Name, "It is not a delegatin handler");
if (global)
{
Services.AddTransient(delegateType);
Services.AddTransient<GlobalDelegatingHandler>(s =>
{
var service = s.GetService(delegateType) as DelegatingHandler;
return new GlobalDelegatingHandler(service);
});
}
else
{
Services.AddTransient(typeof(DelegatingHandler), delegateType);
}
return this;
}
public IOcelotBuilder AddDelegatingHandler<THandler>(bool global = false)
where THandler : DelegatingHandler
{