mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:08:15 +08:00
allow to add delegating handlers by type (#943)
This commit is contained in:
@ -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;
|
||||
|
||||
|
@ -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
|
||||
{
|
||||
|
Reference in New Issue
Block a user