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; |
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
namespace MyCompanyName.MyProjectName; |
namespace MyCompanyName.MyProjectName; |
||||
|
|
||||
public class HelloWorldService : ITransientDependency |
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 System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.DependencyInjection.Extensions; |
||||
using Microsoft.Extensions.Hosting; |
using Microsoft.Extensions.Hosting; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using Serilog; |
||||
|
using Serilog.Extensions.Hosting; |
||||
|
using Serilog.Extensions.Logging; |
||||
using Volo.Abp; |
using Volo.Abp; |
||||
|
|
||||
namespace MyCompanyName.MyProjectName; |
namespace MyCompanyName.MyProjectName; |
||||
|
|
||||
public class MyProjectNameHostedService : IHostedService |
public class MyProjectNameHostedService : IHostedService |
||||
{ |
{ |
||||
private readonly IAbpApplicationWithExternalServiceProvider _application; |
private IAbpApplicationWithInternalServiceProvider _abpApplication; |
||||
private readonly IServiceProvider _serviceProvider; |
|
||||
private readonly HelloWorldService _helloWorldService; |
private readonly IConfiguration _configuration; |
||||
|
private readonly IHostEnvironment _hostEnvironment; |
||||
public MyProjectNameHostedService( |
|
||||
IAbpApplicationWithExternalServiceProvider application, |
public MyProjectNameHostedService(IConfiguration configuration, IHostEnvironment hostEnvironment) |
||||
IServiceProvider serviceProvider, |
|
||||
HelloWorldService helloWorldService) |
|
||||
{ |
{ |
||||
_application = application; |
_configuration = configuration; |
||||
_serviceProvider = serviceProvider; |
_hostEnvironment = hostEnvironment; |
||||
_helloWorldService = helloWorldService; |
|
||||
} |
} |
||||
|
|
||||
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(); |
await _abpApplication.ShutdownAsync(); |
||||
|
|
||||
return Task.CompletedTask; |
|
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -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; |
namespace MyCompanyName.MyProjectName; |
||||
|
|
||||
public class HelloWorldService : ITransientDependency |
public class HelloWorldService : ITransientDependency |
||||
{ |
{ |
||||
|
public ILogger<HelloWorldService> Logger { get; set; } |
||||
|
|
||||
|
public HelloWorldService() |
||||
|
{ |
||||
|
Logger = NullLogger<HelloWorldService>.Instance; |
||||
|
} |
||||
public string SayHello() |
public string SayHello() |
||||
{ |
{ |
||||
return "\tHello world!"; |
Logger.LogInformation("Call SayHello"); |
||||
|
return "Hello world!"; |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue