Created basic Autofac integration.

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent c26862bcd3
commit aca324d1f1

@ -1,5 +1,7 @@
using AbpDesk.EntityFrameworkCore;
using AbpDesk.Web.Mvc.Navigation;
using AbpDesk.Web.Mvc.Temp;
using Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@ -42,6 +44,8 @@ namespace AbpDesk.Web.Mvc
services.AddMvc();
services.AddAssemblyOf<AbpDeskWebMvcModule>();
services.GetContainerBuilder().RegisterType<MyClassToTestAutofacCustomRegistration>();
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)

@ -1,11 +1,12 @@
using Microsoft.AspNetCore.Mvc;
using AbpDesk.Web.Mvc.Temp;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
namespace AbpDesk.Web.Mvc.Controllers
{
public class HomeController : AbpController
{
public IActionResult Index()
public IActionResult Index(MyClassToTestAutofacCustomRegistration obj)
{
return View();
}

@ -1,20 +1,16 @@
using System;
using System.IO;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Volo.Abp;
using Volo.Abp.Modularity.PlugIns;
namespace AbpDesk.Web.Mvc
{
public class Startup
{
public IContainer ApplicationContainer { get; private set; }
private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env)
@ -22,10 +18,12 @@ namespace AbpDesk.Web.Mvc
_env = env;
}
public IServiceProvider ConfigureServices(IServiceCollection services)
public void ConfigureServices(IServiceCollection services)
{
services.AddApplication<AbpDeskWebMvcModule>(options =>
{
options.UseAutofac();
/* @halil: I added Abp.MongoDb package as a dependency to the main application in order to use the blog plugin.
* Otherwise, we should add all dependencies (Recursively) into plugin folder
* and load in correct order. We should carefully think on that problem in the future.
@ -36,11 +34,6 @@ namespace AbpDesk.Web.Mvc
@"../Web_PlugIns/")
);
});
var builder = new ContainerBuilder();
builder.Populate(services);
ApplicationContainer = builder.Build();
return new AutofacServiceProvider(ApplicationContainer);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
@ -54,8 +47,6 @@ namespace AbpDesk.Web.Mvc
.CreateLogger()
);
appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());
app.InitializeApplication();
}
}

@ -0,0 +1,7 @@
namespace AbpDesk.Web.Mvc.Temp
{
/* Will be removed later */
public class MyClassToTestAutofacCustomRegistration
{
}
}

@ -16,4 +16,8 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp\Volo.Abp.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,30 @@
using Autofac;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp
{
public static class AbpApplicationCreationOptionsAutofacExtensions
{
public static void UseAutofac(this AbpApplicationCreationOptions options)
{
var builder = new ContainerBuilder();
options.Services.AddObjectAccessor(builder);
options.Services.AddSingleton((IServiceProviderFactory<ContainerBuilder>) new AbpAutofacServiceProviderFactory(builder));
}
[NotNull]
public static ContainerBuilder GetContainerBuilder([NotNull] this IServiceCollection services)
{
Check.NotNull(services, nameof(services));
var builder = services.GetObjectOrNull<ContainerBuilder>();
if (builder == null)
{
throw new AbpException($"Could not find ContainerBuilder. Be sure that you have called {nameof(UseAutofac)} method before!");
}
return builder;
}
}
}

@ -0,0 +1,39 @@
using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp
{
/// <summary>
/// A factory for creating a <see cref="T:Autofac.ContainerBuilder" /> and an <see cref="T:System.IServiceProvider" />.
/// </summary>
public class AbpAutofacServiceProviderFactory : IServiceProviderFactory<ContainerBuilder>
{
private readonly ContainerBuilder _builder;
public AbpAutofacServiceProviderFactory(ContainerBuilder builder)
{
_builder = builder;
}
/// <summary>
/// Creates a container builder from an <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.
/// </summary>
/// <param name="services">The collection of services</param>
/// <returns>A container builder that can be used to create an <see cref="T:System.IServiceProvider" />.</returns>
public ContainerBuilder CreateBuilder(IServiceCollection services)
{
AutofacRegistration.Populate(_builder, services);
return _builder;
}
public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder)
{
Check.NotNull(containerBuilder, nameof(containerBuilder));
return new AutofacServiceProvider(containerBuilder.Build());
}
}
}

@ -11,7 +11,7 @@ namespace Microsoft.Extensions.DependencyInjection
.FirstOrDefault(d => d.ServiceType == typeof(T))
?.ImplementationInstance;
}
public static T GetSingletonInstance<T>(this IServiceCollection services)
{
var service = services.GetSingletonInstanceOrNull<T>();

@ -39,5 +39,17 @@ namespace Microsoft.Extensions.DependencyInjection
return accessor;
}
public static T GetObjectOrNull<T>(this IServiceCollection services)
where T : class
{
return services.GetSingletonInstanceOrNull<IObjectAccessor<T>>()?.Value;
}
public static T GetObject<T>(this IServiceCollection services)
where T : class
{
return services.GetObjectOrNull<T>() ?? throw new Exception($"Could not find an object of {typeof(T).AssemblyQualifiedName} in services. Be sure that you have used AddObjectAccessor before!");
}
}
}

@ -26,7 +26,7 @@ namespace Volo.Abp
StartupModuleType = startupModuleType;
var options = new AbpApplicationCreationOptions();
var options = new AbpApplicationCreationOptions(services);
optionsAction?.Invoke(options);
services.AddCoreAbpServices(this);

@ -1,4 +1,5 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity.PlugIns;
namespace Volo.Abp
@ -6,10 +7,14 @@ namespace Volo.Abp
public class AbpApplicationCreationOptions
{
[NotNull]
public PlugInSourceList PlugInSources { get; private set; }
public IServiceCollection Services { get; }
public AbpApplicationCreationOptions()
[NotNull]
public PlugInSourceList PlugInSources { get; }
public AbpApplicationCreationOptions([NotNull] IServiceCollection services)
{
Services = Check.NotNull(services, nameof(services));
PlugInSources = new PlugInSourceList();
}
}

Loading…
Cancel
Save