Browse Source

Make console app working with autofac.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
d9a0a5ff86
  1. 1
      src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk.ConsoleDemo.csproj
  2. 12
      src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs
  3. 7
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs
  4. 20
      src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacExtensions.cs

1
src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk.ConsoleDemo.csproj

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

12
src/AbpDesk/AbpDesk.ConsoleDemo/AbpDesk/ConsoleDemo/Program.cs

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

7
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs

@ -43,7 +43,12 @@ namespace Volo.Abp.AspNetCore.Mvc
private static void AddApplicationParts(ApplicationInitializationContext context) 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>(); var moduleManager = context.ServiceProvider.GetRequiredService<IModuleManager>();
foreach (var module in moduleManager.Modules.Where(m => m.IsLoadedAsPlugIn)) foreach (var module in moduleManager.Modules.Where(m => m.IsLoadedAsPlugIn))

20
src/Volo.Abp.Autofac/Volo/Abp/AbpApplicationCreationOptionsAutofacExtensions.cs → src/Volo.Abp.Autofac/Volo/Abp/AbpAutofacExtensions.cs

@ -1,10 +1,11 @@
using Autofac; using System;
using Autofac;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
namespace Volo.Abp namespace Volo.Abp
{ {
public static class AbpApplicationCreationOptionsAutofacExtensions public static class AbpAutofacExtensions
{ {
public static void UseAutofac(this AbpApplicationCreationOptions options) public static void UseAutofac(this AbpApplicationCreationOptions options)
{ {
@ -26,5 +27,20 @@ namespace Volo.Abp
return builder; 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