Merge pull request #118 from aspnetzero/api-versioning

Fix service provider reference issue.
pull/129/head
Halil İbrahim Kalkan 7 years ago committed by GitHub
commit 99415753ff

@ -12,6 +12,7 @@ namespace Volo.Abp.AspNetCore.EmbeddedFiles
{
private readonly Lazy<IEmbeddedFileManager> _embeddedResourceManager;
private readonly Lazy<AspNetCoreEmbeddedFileOptions> _options;
private readonly IObjectAccessor<IServiceProvider> _serviceProviderAccessor;
public EmbeddedResourceFileProvider(IServiceProvider serviceProvider)
: this(new ObjectAccessor<IServiceProvider>(serviceProvider))
@ -19,21 +20,28 @@ namespace Volo.Abp.AspNetCore.EmbeddedFiles
}
public EmbeddedResourceFileProvider(IObjectAccessor<IServiceProvider> serviceProvider)
public EmbeddedResourceFileProvider(IObjectAccessor<IServiceProvider> serviceProviderAccessor)
{
_serviceProviderAccessor = serviceProviderAccessor;
_embeddedResourceManager = new Lazy<IEmbeddedFileManager>(
() => serviceProvider.Value.GetRequiredService<IEmbeddedFileManager>(),
() => serviceProviderAccessor.Value.GetRequiredService<IEmbeddedFileManager>(),
true
);
_options = new Lazy<AspNetCoreEmbeddedFileOptions>(
() => serviceProvider.Value.GetRequiredService<IOptions<AspNetCoreEmbeddedFileOptions>>().Value,
() => serviceProviderAccessor.Value.GetRequiredService<IOptions<AspNetCoreEmbeddedFileOptions>>().Value,
true
);
}
public IFileInfo GetFileInfo(string subpath)
{
if (!IsInitialized())
{
return new NotFoundFileInfo(subpath);
}
var resource = _embeddedResourceManager.Value.FindFile(subpath);
if (resource == null || IsIgnoredFile(resource))
@ -46,6 +54,11 @@ namespace Volo.Abp.AspNetCore.EmbeddedFiles
public IDirectoryContents GetDirectoryContents(string subpath)
{
if (!IsInitialized())
{
return new NotFoundDirectoryContents();
}
//TODO: Implement...?
return new NotFoundDirectoryContents();
@ -60,5 +73,10 @@ namespace Volo.Abp.AspNetCore.EmbeddedFiles
{
return resource.FileExtension != null && _options.Value.IgnoredFileExtensions.Contains(resource.FileExtension);
}
private bool IsInitialized()
{
return _serviceProviderAccessor.Value != null;
}
}
}

@ -4,10 +4,11 @@ using Volo.Abp.EmbeddedFiles;
namespace Volo.Abp.AspNetCore.EmbeddedFiles
{
//TODO: Remove to Volo.Abp.AspNetCore.Mvc project!
public class EmbeddedResourceViewFileProvider : EmbeddedResourceFileProvider
{
public EmbeddedResourceViewFileProvider(IObjectAccessor<IServiceProvider> serviceProvider)
: base(serviceProvider)
public EmbeddedResourceViewFileProvider(IObjectAccessor<IServiceProvider> serviceProviderAccessor)
: base(serviceProviderAccessor)
{
}

@ -0,0 +1,28 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc;
namespace Volo.Abp.Identity.HttpApi.Host.VersioningTests
{
[Route("api/calls")]
public class CallsController : AbpController
{
private static List<CallDto> _calls = new List<CallDto>
{
new CallDto {Id = 1, Number = "123456"},
new CallDto { Id = 2, Number = "123457" }
};
[HttpGet]
public List<CallDto> GetList()
{
return _calls;
}
}
public class CallDto : EntityDto<int>
{
public string Number { get; set; }
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Internal;
using Volo.Abp.Modularity;
@ -12,7 +13,7 @@ namespace Volo.Abp
[NotNull]
public Type StartupModuleType { get; }
public IServiceProvider ServiceProvider { get; protected set; }
public IServiceProvider ServiceProvider { get; private set; }
public IServiceCollection Services { get; }
@ -29,6 +30,8 @@ namespace Volo.Abp
StartupModuleType = startupModuleType;
Services = services;
services.TryAddObjectAccessor<IServiceProvider>();
var options = new AbpApplicationCreationOptions(services);
optionsAction?.Invoke(options);
@ -50,16 +53,11 @@ namespace Volo.Abp
{
}
private IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollection services, AbpApplicationCreationOptions options)
protected virtual void SetServiceProvider(IServiceProvider serviceProvider)
{
return services
.GetSingletonInstance<IModuleLoader>()
.LoadModules(
services,
StartupModuleType,
options.PlugInSources
);
ServiceProvider = serviceProvider;
ServiceProvider.GetRequiredService<ObjectAccessor<IServiceProvider>>().Value = ServiceProvider;
}
protected virtual void InitializeModules()
@ -71,5 +69,16 @@ namespace Volo.Abp
.InitializeModules(new ApplicationInitializationContext(scope.ServiceProvider));
}
}
private IReadOnlyList<IAbpModuleDescriptor> LoadModules(IServiceCollection services, AbpApplicationCreationOptions options)
{
return services
.GetSingletonInstance<IModuleLoader>()
.LoadModules(
services,
StartupModuleType,
options.PlugInSources
);
}
}
}

@ -22,7 +22,7 @@ namespace Volo.Abp
{
Check.NotNull(serviceProvider, nameof(serviceProvider));
ServiceProvider = serviceProvider;
SetServiceProvider(serviceProvider);
InitializeModules();
}

@ -34,8 +34,8 @@ namespace Volo.Abp
public void Initialize()
{
ServiceScope = Services.BuildServiceProviderFromFactory().CreateScope();
ServiceProvider = ServiceScope.ServiceProvider;
SetServiceProvider(ServiceScope.ServiceProvider);
InitializeModules();
}

@ -1,6 +1,4 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.ObjectMapping;
using Volo.Abp.Reflection;
@ -35,9 +33,7 @@ namespace Volo.Abp
services.AddLogging();
services.AddAssemblyOf<AbpKernelModule>();
services.TryAddObjectAccessor<IServiceProvider>();
services.Configure<ModuleLifecycleOptions>(options =>
{
options.Contributers.Add<OnPreApplicationInitializationModuleLifecycleContributer>();
@ -46,10 +42,5 @@ namespace Volo.Abp
options.Contributers.Add<OnApplicationShutdownModuleLifecycleContributer>();
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
context.ServiceProvider.GetRequiredService<ObjectAccessor<IServiceProvider>>().Value = context.ServiceProvider;
}
}
}

Loading…
Cancel
Save