Skip to content

Commit

Permalink
Added HostedServices & UserSecrets
Browse files Browse the repository at this point in the history
  • Loading branch information
Aviuz committed Dec 20, 2024
1 parent f48019a commit bdf0ad4
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 23 deletions.
8 changes: 4 additions & 4 deletions source/ECF.AutoFac/.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>EasyConsoleFramework.AutoFac</id>
<version>1.0.3</version>
<version>1.0.4</version>
<description>Easy Console Framewok - Framework for building command based IoC console application. This package is using AutoFac library for IoC.</description>
<authors>Aviuz</authors>
<tags>console IoC ecf autofac</tags>
Expand All @@ -18,19 +18,19 @@

<dependencies>
<group targetFramework="net6">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Autofac" version="6.5.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="6.0.0" />
</group>

<group targetFramework="net7">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Autofac" version="6.5.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="7.0.0" />
</group>

<group targetFramework="net8">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Autofac" version="6.5.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="8.0.0" />
</group>
Expand Down
3 changes: 2 additions & 1 deletion source/ECF.AutoFac/Adapters/AutoFacProviderAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Autofac;
using Autofac;
using ECF.InverseOfControl;

namespace ECF.Autofac.Adapters;
Expand All @@ -9,6 +9,7 @@ public class AutoFacProviderAdapter : IIoCProviderAdapter

public AutoFacProviderAdapter(ILifetimeScope scope) => this.scope = scope;
public TService Resolve<TService>() where TService : notnull => scope.Resolve<TService>();
public IEnumerable<TService> ResolveMultiple<TService>() where TService : notnull => scope.Resolve<IEnumerable<TService>>();
public object Resolve(Type serviceType) => scope.Resolve(serviceType);
public IIoCScopeAdapter BeginNestedScope() => new AutoFacScopeAdapter(scope.BeginLifetimeScope());
}
2 changes: 1 addition & 1 deletion source/ECF.AutoFac/ECF.AutoFac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
52 changes: 52 additions & 0 deletions source/ECF.HostedServices/.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>ECF.HostedServices</id>
<version>1.0.4</version>
<description>Hosted Services plugin for Easy Console Framewok (ECF).</description>
<authors>Aviuz</authors>
<tags>console IoC ecf</tags>

<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<copyright>Copyright (c) Aviuz 2023</copyright>

<language>en-US</language>
<projectUrl>https://github.com/Aviuz/ECF</projectUrl>
<repository type="git" url="https://github.com/Aviuz/ECF.git" />
<readme>docs\README.md</readme>

<dependencies>
<group targetFramework="net6">
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.Hosting.Abstractions" version="6.0.0" />
</group>

<group targetFramework="net7">
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.Hosting.Abstractions" version="7.0.0" />
</group>

<group targetFramework="net8">
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.Hosting.Abstractions" version="8.0.0" />
</group>
</dependencies>
</metadata>

<files>
<file src="..\..\README.md" target="docs\" />

<file src="bin\Release\net6.0\ECF.HostedServices.dll" target="lib\net6.0" />
<file src="bin\Release\net6.0\ECF.HostedServices.pdb" target="lib\net6.0" />
<file src="bin\Release\net6.0\ECF.HostedServices.deps.json" target="lib\net6.0" />

<file src="bin\Release\net7.0\ECF.HostedServices.dll" target="lib\net7.0" />
<file src="bin\Release\net7.0\ECF.HostedServices.pdb" target="lib\net7.0" />
<file src="bin\Release\net7.0\ECF.HostedServices.deps.json" target="lib\net7.0" />

<file src="bin\Release\net8.0\ECF.HostedServices.dll" target="lib\net8.0" />
<file src="bin\Release\net8.0\ECF.HostedServices.pdb" target="lib\net8.0" />
<file src="bin\Release\net8.0\ECF.HostedServices.deps.json" target="lib\net8.0" />
</files>
</package>
28 changes: 28 additions & 0 deletions source/ECF.HostedServices/ECF.HostedServices.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.4</Version>
<RootNamespace>ECF.HostedServices</RootNamespace>
<AssemblyName>ECF.HostedServices</AssemblyName>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ECF\ECF.csproj" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions source/ECF.HostedServices/ECFHostedServicesExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using ECF.Engine;
using ECF.HostedServices;
using ECF.InverseOfControl;
using Microsoft.Extensions.Hosting;

namespace ECF;

public static class ECFHostedServicesExtensions
{
public static ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> AddHostedServices<TDPBuilderAdapter, TDPBuilder>(this ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> builder) where TDPBuilderAdapter : class, IIoCBuilderAdapter<TDPBuilder> where TDPBuilder : class
{
HostedServicesRunner runner = new();

builder.RegisterStartHandler(runner.StartAsync);
builder.RegisterStopHandler(runner.StopAsync);
return builder;
}
}
32 changes: 32 additions & 0 deletions source/ECF.HostedServices/HostedServicesRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using ECF.InverseOfControl;
using Microsoft.Extensions.Hosting;

namespace ECF.HostedServices;

internal class HostedServicesRunner
{
private readonly List<IHostedService> startedServices = new();
private bool hasStarted = false;

public async Task StartAsync(IIoCProviderAdapter iocProvider, CancellationToken cancellationToken)
{
if (hasStarted)
return;

hasStarted = true;

foreach (var service in iocProvider.ResolveMultiple<IHostedService>())
{
await service.StartAsync(cancellationToken);
startedServices.Add(service);
}
}

public async Task StopAsync(IIoCProviderAdapter _)
{
foreach (var service in startedServices)
{
await service.StopAsync(default);
}
}
}
8 changes: 4 additions & 4 deletions source/ECF.Microsoft.DependencyInjection/.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>ECF</id>
<version>1.0.3</version>
<version>1.0.4</version>
<description>Easy Console Framewok - Framework for building command based IoC console application. This package is using Microsoft.Extensions.DependencyInjection library for IoC</description>
<authors>Aviuz</authors>
<tags>console IoC ecf DependencyInjection</tags>
Expand All @@ -18,19 +18,19 @@

<dependencies>
<group targetFramework="net6">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.DependencyInjection" version="6.0.1" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="6.0.0" />
</group>

<group targetFramework="net7">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.DependencyInjection" version="7.0.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="7.0.0" />
</group>

<group targetFramework="net8">
<dependency id="EasyConsoleFramework.Base" version="1.0.3" />
<dependency id="EasyConsoleFramework.Base" version="1.0.4" />
<dependency id="Microsoft.Extensions.DependencyInjection" version="8.0.0" />
<dependency id="Microsoft.Extensions.Configuration.Json" version="8.0.0" />
</group>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MicrosoftServiceProviderAdapter : IIoCProviderAdapter
public MicrosoftServiceProviderAdapter(IServiceProvider serviceProvider) => this.serviceProvider = serviceProvider;

public TService Resolve<TService>() where TService : notnull => serviceProvider.GetRequiredService<TService>();
public IEnumerable<TService> ResolveMultiple<TService>() where TService : notnull => serviceProvider.GetServices<TService>();
public object Resolve(Type serviceType) => serviceProvider.GetRequiredService(serviceType);
public IIoCScopeAdapter BeginNestedScope() => new MicrosoftServiceScopeAdapter(serviceProvider.CreateScope());
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<AssemblyName>ECF</AssemblyName>
<RootNamespace>ECF</RootNamespace>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion source/ECF.Templates/ECFTemplates.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageType>Template</PackageType>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<PackageId>ECFTemplates</PackageId>
<Title>ECF Templates</Title>
<Authors>Aviuz</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyConsoleFramework.Autofac" Version="1.0.3" />
<PackageReference Include="EasyConsoleFramework.Autofac" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion source/ECF.Templates/templates/ECF/NewProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ECF" Version="1.0.3" />
<PackageReference Include="ECF" Version="1.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task help_into_should_start_with_provided_text()
consoleStreams.Reset();

var commandline = PrepareDefaultCommandLineUsingCustomConfiguration(ctx => ctx.HelpIntro = "5284749012");
await commandline.StartAsync(["help"]);
await commandline.RunAsync(["help"]);

Assert.StartsWith("5284749012", consoleStreams.GetConsoleOutput());
}
Expand Down
8 changes: 7 additions & 1 deletion source/ECF.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\README.md = ..\README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECF.Tests", "ECF.Tests\ECF.Tests.csproj", "{71DADA34-E165-402A-8A85-22904D776A97}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ECF.Tests", "ECF.Tests\ECF.Tests.csproj", "{71DADA34-E165-402A-8A85-22904D776A97}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ECF.HostedServices", "ECF.HostedServices\ECF.HostedServices.csproj", "{45D6C5EA-52F3-40E7-A923-D7EDC1612402}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -50,6 +52,10 @@ Global
{71DADA34-E165-402A-8A85-22904D776A97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71DADA34-E165-402A-8A85-22904D776A97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71DADA34-E165-402A-8A85-22904D776A97}.Release|Any CPU.Build.0 = Release|Any CPU
{45D6C5EA-52F3-40E7-A923-D7EDC1612402}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45D6C5EA-52F3-40E7-A923-D7EDC1612402}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45D6C5EA-52F3-40E7-A923-D7EDC1612402}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45D6C5EA-52F3-40E7-A923-D7EDC1612402}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion source/ECF/.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>EasyConsoleFramework.Base</id>
<version>1.0.3</version>
<version>1.0.4</version>
<description>Easy Console Framewok - Framework for building command based IoC console application. This package is base library without any IoC dependency.</description>
<authors>Aviuz</authors>
<tags>console IoC ecf</tags>
Expand Down
8 changes: 4 additions & 4 deletions source/ECF/ECF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<RootNamespace>ECF</RootNamespace>
<AssemblyName>ECF.Base</AssemblyName>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.1" />
</ItemGroup>

</Project>
35 changes: 33 additions & 2 deletions source/ECF/Engine/Building/ECFHostBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> where TDPBuilderA
{
private bool throwWhenCommandRegistryNotEmpty = false;

public delegate Task OnStartEventHandler(IIoCProviderAdapter services, CancellationToken cancellationToken);
public delegate Task OnStopEventHandler(IIoCProviderAdapter services);
private event OnStartEventHandler onStart;
private event OnStopEventHandler onStop;

public InterfaceContext InterfaceContext { get; }
public TDPBuilderAdapter IoCBuilderAdapter { get; }
public CommandRegistryBuilder RegistryBuilder { get; }
Expand Down Expand Up @@ -80,6 +85,7 @@ public ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> AddConfiguration(string
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(configurationFileName, optional, reloadOnChange)
.AddUserSecrets(Assembly.GetEntryAssembly())

Check warning on line 88 in source/ECF/Engine/Building/ECFHostBuilderBase.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'assembly' in 'IConfigurationBuilder UserSecretsConfigurationExtensions.AddUserSecrets(IConfigurationBuilder configuration, Assembly assembly)'.

Check warning on line 88 in source/ECF/Engine/Building/ECFHostBuilderBase.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'assembly' in 'IConfigurationBuilder UserSecretsConfigurationExtensions.AddUserSecrets(IConfigurationBuilder configuration, Assembly assembly)'.
.Build();

IoCBuilderAdapter.RegisterSingleton<IConfiguration>(configuration);
Expand All @@ -95,18 +101,43 @@ public ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> RegisterDefaultCommand(
return this;
}

public ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> RegisterStartHandler(OnStartEventHandler eventHandler)
{
onStart += eventHandler;
return this;
}

public ECFHostBuilderBase<TDPBuilderAdapter, TDPBuilder> RegisterStopHandler(OnStopEventHandler eventHandler)
{
onStop += eventHandler;
return this;
}

public void Run(string[] args) => RunAsync(args, default).Wait();

public async Task RunAsync(string[] args, CancellationToken cancellationToken = default)
{
using CancellationTokenSource customBackgroundWorkersCts = new();

if (throwWhenCommandRegistryNotEmpty && RegistryBuilder.IsEmpty == false)
throw new ECFCommandRegistryNotEmptyWhenUsingSingleMode();

IIoCProviderAdapter ioc = IoCBuilderAdapter.Build();

if (InterfaceContext.CommandProcessor == null)
InterfaceContext.CommandProcessor = new CommandProcessor(IoCBuilderAdapter.Build());
InterfaceContext.CommandProcessor = new CommandProcessor(ioc);

if (onStart != null)
foreach (OnStartEventHandler start in onStart.GetInvocationList())
await start(ioc, customBackgroundWorkersCts.Token);

CommandLineInterface commandInterface = new(InterfaceContext);
await commandInterface.RunAsync(args, cancellationToken);

if (onStop != null)
foreach (OnStopEventHandler stop in onStop.GetInvocationList())
await stop(ioc);

await commandInterface.StartAsync(args, cancellationToken);
customBackgroundWorkersCts.Cancel();
}
}
Loading

0 comments on commit bdf0ad4

Please sign in to comment.