Browse Source

Resolved #5006: Console applications should participate to the main host builder

pull/5011/head
Halil İbrahim Kalkan 6 years ago
parent
commit
c07b29c557
  1. 3
      templates/console/src/MyCompanyName.MyProjectName/MyCompanyName.MyProjectName.csproj
  2. 41
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs
  3. 10
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs
  4. 9
      templates/console/src/MyCompanyName.MyProjectName/Program.cs
  5. 1
      templates/console/src/MyCompanyName.MyProjectName/appsettings.json

3
templates/console/src/MyCompanyName.MyProjectName/MyCompanyName.MyProjectName.csproj

@ -13,14 +13,13 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.6" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<None Remove="appsettings.json" />
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

41
templates/console/src/MyCompanyName.MyProjectName/MyProjectNameHostedService.cs

@ -1,34 +1,41 @@
using System.Threading;
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Volo.Abp;
namespace MyCompanyName.MyProjectName
{
public class MyProjectNameHostedService : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
private readonly IAbpApplicationWithExternalServiceProvider _application;
private readonly IServiceProvider _serviceProvider;
private readonly HelloWorldService _helloWorldService;
public MyProjectNameHostedService(
IAbpApplicationWithExternalServiceProvider application,
IServiceProvider serviceProvider,
HelloWorldService helloWorldService)
{
using (var application = AbpApplicationFactory.Create<MyProjectNameModule>(options =>
{
options.UseAutofac(); //Autofac integration
options.Services.AddLogging(c => c.AddSerilog());
}))
{
application.Initialize();
_application = application;
_serviceProvider = serviceProvider;
_helloWorldService = helloWorldService;
}
//Resolve a service and use it
var helloWorldService = application.ServiceProvider.GetService<HelloWorldService>();
helloWorldService.SayHello();
public Task StartAsync(CancellationToken cancellationToken)
{
_application.Initialize(_serviceProvider);
application.Shutdown();
}
_helloWorldService.SayHello();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken)
{
_application.Shutdown();
return Task.CompletedTask;
}
}
}

10
templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs

@ -1,4 +1,6 @@
using Volo.Abp.Autofac;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;
namespace MyCompanyName.MyProjectName
@ -9,6 +11,12 @@ namespace MyCompanyName.MyProjectName
)]
public class MyProjectNameModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var configuration = context.Services.GetConfiguration();
var hostEnvironment = context.Services.GetSingletonInstance<IHostEnvironment>();
context.Services.AddHostedService<MyProjectNameHostedService>();
}
}
}

9
templates/console/src/MyCompanyName.MyProjectName/Program.cs

@ -1,5 +1,6 @@
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
@ -43,9 +44,15 @@ namespace MyCompanyName.MyProjectName
internal static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseAutofac()
.UseSerilog()
.ConfigureAppConfiguration((context, config) =>
{
//setup your additional configuration sources
})
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<MyProjectNameHostedService>();
services.AddApplication<MyProjectNameModule>();
});
}
}

1
templates/console/src/MyCompanyName.MyProjectName/appsettings.json

@ -1,2 +1,3 @@
{
}

Loading…
Cancel
Save