Skip to content

Commit

Permalink
Move service additions in its own package.
Browse files Browse the repository at this point in the history
  • Loading branch information
TeraNovaLP committed May 30, 2021
1 parent e25cfd8 commit 7654629
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Cacao.Framework.Services
namespace Cacao.Services
{
public abstract class BaseService: BackgroundService
{
private readonly IServiceScopeFactory serviceScope;
private readonly ILogger logger;

protected BaseService(IServiceScopeFactory serviceScope)
protected BaseService(IServiceScopeFactory serviceScope, ILoggerFactory logger)
{
this.serviceScope = serviceScope;
this.logger = logger.CreateLogger("Cacao.Services");
}

protected abstract bool DelayAtStartup { get; }
Expand All @@ -30,11 +33,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

while (!stoppingToken.IsCancellationRequested)
{
logger.LogInformation($"Executing {GetType().Name} ...");

using (var scope = serviceScope.CreateScope())
{
await Execute(scope);
}

logger.LogInformation("Finished!");

await Task.Delay(GetDelayUntilNextRun(), stoppingToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<PackageId>Cacao.Framework</PackageId>
<PackageId>Cacao.Services</PackageId>
<Version>1.0.0</Version>
<RepositoryUrl>https://github.com/TeraNovaLP/Cacao</RepositoryUrl>
<Authors>TeraNovaLP</Authors>
Expand All @@ -13,11 +13,7 @@
</PropertyGroup>

<ItemGroup>
<Folder Include="Services" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.14" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.15" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Cacao.Framework.Services
namespace Cacao.Services
{
public abstract class CronService: BaseService
{
protected CronService(IServiceScopeFactory serviceScope) : base(serviceScope)
protected CronService(IServiceScopeFactory serviceScope, ILoggerFactory logger) : base(serviceScope, logger)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Cacao.Framework.Services
namespace Cacao.Services
{
public abstract class IntervalService: BaseService
{
protected IntervalService(IServiceScopeFactory serviceScope) : base(serviceScope)
protected IntervalService(IServiceScopeFactory serviceScope, ILoggerFactory logger) : base(serviceScope, logger)
{
}

Expand Down
33 changes: 33 additions & 0 deletions Cacao.Services/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;

namespace Cacao.Services
{
public static class ServiceCollectionExtension
{
/// <summary>
/// Initializes all services from the given assembly.
/// </summary>
/// <param name="services"></param>
/// <param name="sourceAssembly"></param>
public static void InitServices(this IServiceCollection services, Assembly sourceAssembly)
{
var hostedServices = sourceAssembly
.GetTypes()
.Where(x => x.IsClass && !x.IsAbstract && x.IsSubclassOf(typeof(BaseService)))
.ToList();

hostedServices.ForEach(x =>
{
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), x));
});
}
}
}
12 changes: 6 additions & 6 deletions Cacao.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cacao", "Cacao\Cacao.csproj", "{6737BF54-B7F7-4916-9BE6-DE7704C676F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cacao.Framework", "Cacao.Framework\Cacao.Framework.csproj", "{DA04E916-77FF-4249-9125-8A3BAE62F5C9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cacao.Tests", "Cacao.Tests\Cacao.Tests.csproj", "{060F3EF6-3A4E-4B7C-8CFD-722E0351A00C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cacao.Services", "Cacao.Services\Cacao.Services.csproj", "{5E0B181F-7995-4278-BD54-438F30219701}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -16,13 +16,13 @@ Global
{6737BF54-B7F7-4916-9BE6-DE7704C676F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6737BF54-B7F7-4916-9BE6-DE7704C676F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6737BF54-B7F7-4916-9BE6-DE7704C676F1}.Release|Any CPU.Build.0 = Release|Any CPU
{DA04E916-77FF-4249-9125-8A3BAE62F5C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DA04E916-77FF-4249-9125-8A3BAE62F5C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DA04E916-77FF-4249-9125-8A3BAE62F5C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DA04E916-77FF-4249-9125-8A3BAE62F5C9}.Release|Any CPU.Build.0 = Release|Any CPU
{060F3EF6-3A4E-4B7C-8CFD-722E0351A00C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{060F3EF6-3A4E-4B7C-8CFD-722E0351A00C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{060F3EF6-3A4E-4B7C-8CFD-722E0351A00C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{060F3EF6-3A4E-4B7C-8CFD-722E0351A00C}.Release|Any CPU.Build.0 = Release|Any CPU
{5E0B181F-7995-4278-BD54-438F30219701}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E0B181F-7995-4278-BD54-438F30219701}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E0B181F-7995-4278-BD54-438F30219701}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E0B181F-7995-4278-BD54-438F30219701}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Set of additions and utilities for .NET Core
| Package | Compatibility | Description |
| --- | --- | --- |
| [Cacao](https://www.nuget.org/packages/Cacao/) | .NET Standard 2.0 | Core package with extensions and utilities |
| [Cacao.Framework](https://www.nuget.org/packages/Cacao.Framework/) | .NET Core 3.1 | Additions for .NET Core and ASP Net Core |
| [Cacao.Services](https://www.nuget.org/packages/Cacao.Services/) | .NET Core 3.1 | Additions for ASP Net Core hosted services |

## API Documentation
## API Reference
* [Cacao](https://www.fuget.org/packages/Cacao)
* [Cacao.Framework](https://www.fuget.org/packages/Cacao.Framework)
* [Cacao.Services](https://www.fuget.org/packages/Cacao.Services)

0 comments on commit 7654629

Please sign in to comment.