mirror of https://github.com/abpframework/abp.git
7 changed files with 110 additions and 86 deletions
@ -1,12 +1,22 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class HelloWorldService : ITransientDependency |
|||
{ |
|||
public void SayHello() |
|||
public ILogger<HelloWorldService> Logger { get; set; } |
|||
|
|||
public HelloWorldService() |
|||
{ |
|||
Logger = NullLogger<HelloWorldService>.Instance; |
|||
} |
|||
|
|||
public Task SayHelloAsync() |
|||
{ |
|||
Console.WriteLine("\tHello World!"); |
|||
Logger.LogInformation("Hello World!"); |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
|
|||
@ -1,40 +1,56 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.DependencyInjection.Extensions; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
using Serilog; |
|||
using Serilog.Extensions.Hosting; |
|||
using Serilog.Extensions.Logging; |
|||
using Volo.Abp; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class MyProjectNameHostedService : IHostedService |
|||
{ |
|||
private readonly IAbpApplicationWithExternalServiceProvider _application; |
|||
private readonly IServiceProvider _serviceProvider; |
|||
private readonly HelloWorldService _helloWorldService; |
|||
|
|||
public MyProjectNameHostedService( |
|||
IAbpApplicationWithExternalServiceProvider application, |
|||
IServiceProvider serviceProvider, |
|||
HelloWorldService helloWorldService) |
|||
private IAbpApplicationWithInternalServiceProvider _abpApplication; |
|||
|
|||
private readonly IConfiguration _configuration; |
|||
private readonly IHostEnvironment _hostEnvironment; |
|||
|
|||
public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment) |
|||
{ |
|||
_application = application; |
|||
_serviceProvider = serviceProvider; |
|||
_helloWorldService = helloWorldService; |
|||
_configuration = configuration; |
|||
_hostEnvironment = hostEnvironment; |
|||
} |
|||
|
|||
public Task StartAsync(CancellationToken cancellationToken) |
|||
public async Task StartAsync(CancellationToken cancellationToken) |
|||
{ |
|||
_application.Initialize(_serviceProvider); |
|||
_abpApplication = await AbpApplicationFactory.CreateAsync<MyProjectNameModule>(options => |
|||
{ |
|||
options.Services.ReplaceConfiguration(_configuration); |
|||
options.Services.AddSingleton(_hostEnvironment); |
|||
|
|||
options.UseAutofac(); |
|||
|
|||
_helloWorldService.SayHello(); |
|||
// UseSerilog()
|
|||
options.Services.AddLogging(); |
|||
options.Services.Replace(ServiceDescriptor.Singleton<ILoggerFactory, SerilogLoggerFactory>()); |
|||
var implementationInstance = new DiagnosticContext(null); |
|||
options.Services.AddSingleton(implementationInstance); |
|||
options.Services.AddSingleton((IDiagnosticContext) implementationInstance); |
|||
}); |
|||
|
|||
return Task.CompletedTask; |
|||
await _abpApplication.InitializeAsync(); |
|||
|
|||
var helloWorldService = _abpApplication.ServiceProvider.GetRequiredService<HelloWorldService>(); |
|||
|
|||
await helloWorldService.SayHelloAsync(); |
|||
} |
|||
|
|||
public Task StopAsync(CancellationToken cancellationToken) |
|||
public async Task StopAsync(CancellationToken cancellationToken) |
|||
{ |
|||
_application.Shutdown(); |
|||
|
|||
return Task.CompletedTask; |
|||
await _abpApplication.ShutdownAsync(); |
|||
} |
|||
} |
|||
|
|||
@ -1,3 +1,3 @@ |
|||
{ |
|||
|
|||
"MySettingName": "MySettingValue" |
|||
} |
|||
|
|||
@ -1,11 +1,20 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace MyCompanyName.MyProjectName; |
|||
|
|||
public class HelloWorldService : ITransientDependency |
|||
{ |
|||
public ILogger<HelloWorldService> Logger { get; set; } |
|||
|
|||
public HelloWorldService() |
|||
{ |
|||
Logger = NullLogger<HelloWorldService>.Instance; |
|||
} |
|||
public string SayHello() |
|||
{ |
|||
return "\tHello world!"; |
|||
Logger.LogInformation("Call SayHello"); |
|||
return "Hello world!"; |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue