Make console app working with autofac.

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

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\AbpDesk.Application\AbpDesk.Application.csproj" />
<ProjectReference Include="..\AbpDesk.Application.Contracts\AbpDesk.Application.Contracts.csproj" />
<ProjectReference Include="..\AbpDesk.EntityFrameworkCore\AbpDesk.EntityFrameworkCore.csproj" />

@ -25,15 +25,19 @@ namespace AbpDesk.ConsoleDemo
{
var services = new ServiceCollection();
var application = services.AddApplication<AbpDeskConsoleDemoModule>(options => { AddPlugIns(options); });
using (var scope = services.BuildServiceProvider().CreateScope())
var application = services.AddApplication<AbpDeskConsoleDemoModule>(options =>
{
options.UseAutofac();
AddPlugIns(options);
});
using (var scope = services.BuildAutofacServiceProvider().CreateScope())
{
application.Initialize(scope.ServiceProvider);
RunListers(application);
Console.WriteLine("Press ENTER to run again...");
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
application.Shutdown();

@ -43,7 +43,12 @@ namespace Volo.Abp.AspNetCore.Mvc
private static void AddApplicationParts(ApplicationInitializationContext context)
{
var partManager = context.ServiceProvider.GetRequiredService<ApplicationPartManager>();
var partManager = context.ServiceProvider.GetService<ApplicationPartManager>();
if (partManager == null)
{
return;
}
var moduleManager = context.ServiceProvider.GetRequiredService<IModuleManager>();
foreach (var module in moduleManager.Modules.Where(m => m.IsLoadedAsPlugIn))

@ -1,10 +1,11 @@
using Autofac;
using System;
using Autofac;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp
{
public static class AbpApplicationCreationOptionsAutofacExtensions
public static class AbpAutofacExtensions
{
public static void UseAutofac(this AbpApplicationCreationOptions options)
{
@ -26,5 +27,20 @@ namespace Volo.Abp
return builder;
}
public static IServiceProvider BuildAutofacServiceProvider([NotNull] this IServiceCollection services, Action<ContainerBuilder> builderAction = null)
{
Check.NotNull(services, nameof(services));
var serviceProviderFactory = services.GetSingletonInstanceOrNull<IServiceProviderFactory<ContainerBuilder>>();
if (serviceProviderFactory == null)
{
throw new AbpException($"Could not find {typeof(IServiceProviderFactory<ContainerBuilder>).FullName} in {services}. Use {nameof(UseAutofac)} before!");
}
var builder = serviceProviderFactory.CreateBuilder(services);
builderAction?.Invoke(builder);
return serviceProviderFactory.CreateServiceProvider(builder);
}
}
}
Loading…
Cancel
Save