mirror of https://github.com/abpframework/abp.git
committed by
GitHub
41 changed files with 516 additions and 834 deletions
@ -0,0 +1,20 @@ |
|||||
|
using Microsoft.AspNetCore.Mvc.Filters; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc |
||||
|
{ |
||||
|
internal static class AbpActionContextExtensions |
||||
|
{ |
||||
|
public static T GetRequiredService<T>(this FilterContext context) |
||||
|
where T : class |
||||
|
{ |
||||
|
return context.HttpContext.RequestServices.GetRequiredService<T>(); |
||||
|
} |
||||
|
|
||||
|
public static T GetService<T>(this FilterContext context, T defaultValue = default) |
||||
|
where T : class |
||||
|
{ |
||||
|
return context.HttpContext.RequestServices.GetService<T>() ?? defaultValue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,55 +1,22 @@ |
|||||
using System; |
using System; |
||||
using Microsoft.AspNetCore.Mvc; |
using Microsoft.AspNetCore.Mvc; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
using Volo.Abp.ObjectMapping; |
using Volo.Abp.ObjectMapping; |
||||
|
|
||||
namespace Volo.Abp.AspNetCore.Mvc |
namespace Volo.Abp.AspNetCore.Mvc |
||||
{ |
{ |
||||
public abstract class AbpViewComponent : ViewComponent |
public abstract class AbpViewComponent : ViewComponent |
||||
{ |
{ |
||||
public IServiceProvider ServiceProvider { get; set; } |
public IAbpLazyServiceProvider LazyServiceProvider { get; set; } |
||||
protected readonly object ServiceProviderLock = new object(); |
|
||||
|
|
||||
protected TService LazyGetRequiredService<TService>(ref TService reference) |
|
||||
=> LazyGetRequiredService(typeof(TService), ref reference); |
|
||||
|
|
||||
protected TRef LazyGetRequiredService<TRef>(Type serviceType, ref TRef reference) |
|
||||
{ |
|
||||
if (reference == null) |
|
||||
{ |
|
||||
lock (ServiceProviderLock) |
|
||||
{ |
|
||||
if (reference == null) |
|
||||
{ |
|
||||
reference = (TRef)ServiceProvider.GetRequiredService(serviceType); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return reference; |
public IServiceProvider ServiceProvider { get; set; } |
||||
} |
|
||||
|
|
||||
protected Type ObjectMapperContext { get; set; } |
protected Type ObjectMapperContext { get; set; } |
||||
protected IObjectMapper ObjectMapper |
|
||||
{ |
|
||||
get |
|
||||
{ |
|
||||
if (_objectMapper != null) |
|
||||
{ |
|
||||
return _objectMapper; |
|
||||
} |
|
||||
|
|
||||
if (ObjectMapperContext == null) |
|
||||
{ |
|
||||
return LazyGetRequiredService(ref _objectMapper); |
|
||||
} |
|
||||
|
|
||||
return LazyGetRequiredService( |
protected IObjectMapper ObjectMapper => LazyServiceProvider.LazyGetService<IObjectMapper>(provider => |
||||
typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext), |
ObjectMapperContext == null |
||||
ref _objectMapper |
? provider.GetRequiredService<IObjectMapper>() |
||||
); |
: (IObjectMapper) provider.GetRequiredService(typeof(IObjectMapper<>).MakeGenericType(ObjectMapperContext))); |
||||
} |
|
||||
} |
|
||||
private IObjectMapper _objectMapper; |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,73 @@ |
|||||
|
using System.Buffers; |
||||
|
using System.Text.Encodings.Web; |
||||
|
using System.Text.Json; |
||||
|
using Microsoft.AspNetCore.Mvc; |
||||
|
using Microsoft.AspNetCore.Mvc.Formatters; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.ObjectPool; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Json |
||||
|
{ |
||||
|
public class AbpHybridJsonOptionsSetup : IConfigureOptions<MvcOptions> |
||||
|
{ |
||||
|
private readonly IOptions<JsonOptions> _jsonOptions; |
||||
|
private readonly IOptions<MvcNewtonsoftJsonOptions> _mvcNewtonsoftJsonOptions; |
||||
|
private readonly ILoggerFactory _loggerFactory; |
||||
|
private readonly ArrayPool<char> _charPool; |
||||
|
private readonly ObjectPoolProvider _objectPoolProvider; |
||||
|
|
||||
|
public AbpHybridJsonOptionsSetup( |
||||
|
IOptions<JsonOptions> jsonOptions, |
||||
|
IOptions<MvcNewtonsoftJsonOptions> mvcNewtonsoftJsonOptions, |
||||
|
ILoggerFactory loggerFactory, |
||||
|
ArrayPool<char> charPool, |
||||
|
ObjectPoolProvider objectPoolProvider) |
||||
|
{ |
||||
|
_jsonOptions = jsonOptions; |
||||
|
_mvcNewtonsoftJsonOptions = mvcNewtonsoftJsonOptions; |
||||
|
_loggerFactory = loggerFactory; |
||||
|
_charPool = charPool; |
||||
|
_objectPoolProvider = objectPoolProvider; |
||||
|
} |
||||
|
|
||||
|
public void Configure(MvcOptions options) |
||||
|
{ |
||||
|
var systemTextJsonInputFormatter = new SystemTextJsonInputFormatter( |
||||
|
_jsonOptions.Value, |
||||
|
_loggerFactory.CreateLogger<SystemTextJsonInputFormatter>()); |
||||
|
|
||||
|
var newtonsoftJsonInputFormatter = new NewtonsoftJsonInputFormatter( |
||||
|
_loggerFactory.CreateLogger<NewtonsoftJsonInputFormatter>(), |
||||
|
_mvcNewtonsoftJsonOptions.Value.SerializerSettings, |
||||
|
_charPool, |
||||
|
_objectPoolProvider, |
||||
|
options, |
||||
|
_mvcNewtonsoftJsonOptions.Value); |
||||
|
|
||||
|
options.InputFormatters.RemoveType<SystemTextJsonInputFormatter>(); |
||||
|
options.InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>(); |
||||
|
options.InputFormatters.Add(new AbpHybridJsonInputFormatter(systemTextJsonInputFormatter, newtonsoftJsonInputFormatter)); |
||||
|
|
||||
|
var jsonSerializerOptions = _jsonOptions.Value.JsonSerializerOptions; |
||||
|
if (jsonSerializerOptions.Encoder is null) |
||||
|
{ |
||||
|
// If the user hasn't explicitly configured the encoder, use the less strict encoder that does not encode all non-ASCII characters.
|
||||
|
jsonSerializerOptions = new JsonSerializerOptions(jsonSerializerOptions) |
||||
|
{ |
||||
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
var systemTextJsonOutputFormatter = new SystemTextJsonOutputFormatter(jsonSerializerOptions); |
||||
|
var newtonsoftJsonOutputFormatter = new NewtonsoftJsonOutputFormatter( |
||||
|
_mvcNewtonsoftJsonOptions.Value.SerializerSettings, |
||||
|
_charPool, |
||||
|
options); |
||||
|
|
||||
|
options.OutputFormatters.RemoveType<SystemTextJsonOutputFormatter>(); |
||||
|
options.OutputFormatters.RemoveType<NewtonsoftJsonOutputFormatter>(); |
||||
|
options.OutputFormatters.Add(new AbpHybridJsonOutputFormatter(systemTextJsonOutputFormatter, newtonsoftJsonOutputFormatter)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,59 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.DependencyInjection |
||||
|
{ |
||||
|
public class AbpLazyServiceProvider : IAbpLazyServiceProvider, ITransientDependency |
||||
|
{ |
||||
|
protected IDictionary<Type, object> CachedServices { get; set; } |
||||
|
|
||||
|
protected IServiceProvider ServiceProvider { get; set; } |
||||
|
|
||||
|
public AbpLazyServiceProvider(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
ServiceProvider = serviceProvider; |
||||
|
CachedServices = new Dictionary<Type, object>(); |
||||
|
} |
||||
|
|
||||
|
public virtual T LazyGetRequiredService<T>() |
||||
|
{ |
||||
|
return (T) LazyGetRequiredService(typeof(T)); |
||||
|
} |
||||
|
|
||||
|
public virtual object LazyGetRequiredService(Type serviceType) |
||||
|
{ |
||||
|
return CachedServices.GetOrAdd(serviceType, () => ServiceProvider.GetRequiredService(serviceType)); |
||||
|
} |
||||
|
|
||||
|
public virtual T LazyGetService<T>() |
||||
|
{ |
||||
|
return (T) LazyGetService(typeof(T)); |
||||
|
} |
||||
|
|
||||
|
public virtual object LazyGetService(Type serviceType) |
||||
|
{ |
||||
|
return CachedServices.GetOrAdd(serviceType, () => ServiceProvider.GetService(serviceType)); |
||||
|
} |
||||
|
|
||||
|
public virtual T LazyGetService<T>(T defaultValue) |
||||
|
{ |
||||
|
return (T) LazyGetService(typeof(T), defaultValue); |
||||
|
} |
||||
|
|
||||
|
public virtual object LazyGetService(Type serviceType, object defaultValue) |
||||
|
{ |
||||
|
return LazyGetService(serviceType) ?? defaultValue; |
||||
|
} |
||||
|
|
||||
|
public virtual T LazyGetService<T>(Func<IServiceProvider, object> factory) |
||||
|
{ |
||||
|
return (T) LazyGetService(typeof(T), factory); |
||||
|
} |
||||
|
|
||||
|
public virtual object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory) |
||||
|
{ |
||||
|
return CachedServices.GetOrAdd(serviceType, () => factory(ServiceProvider)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.DependencyInjection |
||||
|
{ |
||||
|
public interface IAbpLazyServiceProvider |
||||
|
{ |
||||
|
T LazyGetRequiredService<T>(); |
||||
|
|
||||
|
object LazyGetRequiredService(Type serviceType); |
||||
|
|
||||
|
T LazyGetService<T>(); |
||||
|
|
||||
|
object LazyGetService(Type serviceType); |
||||
|
|
||||
|
T LazyGetService<T>(T defaultValue); |
||||
|
|
||||
|
object LazyGetService(Type serviceType, object defaultValue); |
||||
|
|
||||
|
object LazyGetService(Type serviceType, Func<IServiceProvider, object> factory); |
||||
|
|
||||
|
T LazyGetService<T>(Func<IServiceProvider, object> factory); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue