-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: creates KafkaFlow.Extensions.Hosting to run as Hosted Service
- Loading branch information
1 parent
2fc7985
commit 4051104
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/KafkaFlow.Extensions.Hosting/KafkaFlow.Extensions.Hosting.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>latest</LangVersion> | ||
<RootNamespace>KafkaFlow</RootNamespace> | ||
<PackageId>KafkaFlow.Extensions.Hosting</PackageId> | ||
<Description>Helper to run KafkaFlow as a Hosted Service</Description> | ||
<PackageIconUrl>https://raw.githubusercontent.com/Farfetch/.github/master/images/fuse-logo-128.png</PackageIconUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\KafkaFlow.Microsoft.DependencyInjection\KafkaFlow.Microsoft.DependencyInjection.csproj" /> | ||
<ProjectReference Include="..\KafkaFlow\KafkaFlow.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
18 changes: 18 additions & 0 deletions
18
src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace KafkaFlow | ||
{ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using global::Microsoft.Extensions.Hosting; | ||
|
||
internal class KafkaFlowHostedService : IHostedService | ||
{ | ||
private readonly IKafkaBus kafkaBus; | ||
|
||
public KafkaFlowHostedService(IServiceProvider serviceProvider) => this.kafkaBus = serviceProvider.CreateKafkaBus(); | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) => this.kafkaBus.StartAsync(cancellationToken); | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => this.kafkaBus.StopAsync(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace KafkaFlow | ||
{ | ||
using System; | ||
using global::Microsoft.Extensions.DependencyInjection; | ||
using KafkaFlow.Configuration; | ||
|
||
/// <summary> | ||
/// </summary> | ||
public static class ServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Configures KafkaFlow to run as a Hosted Service | ||
/// </summary> | ||
/// <param name="services">Instance of <see cref="IServiceCollection"/></param> | ||
/// <param name="kafka">A handler to configure KafkaFlow</param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddKafkaFlowHostedService( | ||
this IServiceCollection services, | ||
Action<IKafkaConfigurationBuilder> kafka) | ||
{ | ||
return services | ||
.AddHostedService<KafkaFlowHostedService>() | ||
.AddKafka(kafka); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters