Remove EncoderServices and add default services directly in the service collection extension
This commit is contained in:
parent
89ccdf56bf
commit
602f638a8c
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using Microsoft.Framework.Internal;
|
using Microsoft.Framework.Internal;
|
||||||
|
using Microsoft.Framework.OptionsModel;
|
||||||
using Microsoft.Framework.WebEncoders;
|
using Microsoft.Framework.WebEncoders;
|
||||||
|
|
||||||
namespace Microsoft.Framework.DependencyInjection
|
namespace Microsoft.Framework.DependencyInjection
|
||||||
|
|
@ -17,12 +18,35 @@ namespace Microsoft.Framework.DependencyInjection
|
||||||
public static IServiceCollection AddWebEncoders([NotNull] this IServiceCollection services, Action<WebEncoderOptions> configureOptions)
|
public static IServiceCollection AddWebEncoders([NotNull] this IServiceCollection services, Action<WebEncoderOptions> configureOptions)
|
||||||
{
|
{
|
||||||
services.AddOptions();
|
services.AddOptions();
|
||||||
services.TryAdd(EncoderServices.GetDefaultServices());
|
|
||||||
|
// Register the default encoders
|
||||||
|
// We want to call the 'Default' property getters lazily since they perform static caching
|
||||||
|
services.TryAdd(ServiceDescriptor.Singleton<IHtmlEncoder>(
|
||||||
|
CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter))));
|
||||||
|
services.TryAdd(ServiceDescriptor.Singleton<IJavaScriptStringEncoder>(
|
||||||
|
CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter))));
|
||||||
|
services.TryAdd(ServiceDescriptor.Singleton<IUrlEncoder>(
|
||||||
|
CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter))));
|
||||||
|
|
||||||
if (configureOptions != null)
|
if (configureOptions != null)
|
||||||
{
|
{
|
||||||
services.Configure(configureOptions);
|
services.Configure(configureOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Func<IServiceProvider, T> CreateFactory<T>(
|
||||||
|
Func<T> defaultFactory,
|
||||||
|
Func<ICodePointFilter, T> customFilterFactory)
|
||||||
|
{
|
||||||
|
return serviceProvider =>
|
||||||
|
{
|
||||||
|
var codePointFilter = serviceProvider?.GetService<IOptions<WebEncoderOptions>>()?
|
||||||
|
.Options?
|
||||||
|
.CodePointFilter;
|
||||||
|
return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory();
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using Microsoft.Framework.DependencyInjection;
|
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
|
|
||||||
namespace Microsoft.Framework.WebEncoders
|
|
||||||
{
|
|
||||||
public static class EncoderServices
|
|
||||||
{
|
|
||||||
public static IEnumerable<ServiceDescriptor> GetDefaultServices()
|
|
||||||
{
|
|
||||||
// Register the default encoders
|
|
||||||
// We want to call the 'Default' property getters lazily since they perform static caching
|
|
||||||
yield return ServiceDescriptor.Singleton<IHtmlEncoder>(CreateFactory(() => HtmlEncoder.Default, filter => new HtmlEncoder(filter)));
|
|
||||||
yield return ServiceDescriptor.Singleton<IJavaScriptStringEncoder>(CreateFactory(() => JavaScriptStringEncoder.Default, filter => new JavaScriptStringEncoder(filter)));
|
|
||||||
yield return ServiceDescriptor.Singleton<IUrlEncoder>(CreateFactory(() => UrlEncoder.Default, filter => new UrlEncoder(filter)));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Func<IServiceProvider, T> CreateFactory<T>(Func<T> defaultFactory, Func<ICodePointFilter, T> customFilterFactory)
|
|
||||||
{
|
|
||||||
return serviceProvider =>
|
|
||||||
{
|
|
||||||
var codePointFilter = serviceProvider?.GetService<IOptions<WebEncoderOptions>>()?.Options?.CodePointFilter;
|
|
||||||
return (codePointFilter != null) ? customFilterFactory(codePointFilter) : defaultFactory();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue