https://github.com/nosratifarhad/publish_subscribe_with_channel_dotnet6
You could see in this repository how the channels work. Publish Subscribe With Channel In DotNet6.
https://github.com/nosratifarhad/publish_subscribe_with_channel_dotnet6
background-jobs background-service background-worker channel dotnet dotnet-core dotnetcore publish queue subscribe
Last synced: 3 months ago
JSON representation
You could see in this repository how the channels work. Publish Subscribe With Channel In DotNet6.
- Host: GitHub
- URL: https://github.com/nosratifarhad/publish_subscribe_with_channel_dotnet6
- Owner: nosratifarhad
- Created: 2023-05-09T09:10:18.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-30T11:52:03.000Z (almost 2 years ago)
- Last Synced: 2025-06-02T15:18:47.805Z (4 months ago)
- Topics: background-jobs, background-service, background-worker, channel, dotnet, dotnet-core, dotnetcore, publish, queue, subscribe
- Language: C#
- Homepage:
- Size: 50.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hello
## I have added two projects to this repository to help convey my point and provide more context for better understanding.
projct Number one :
### you need two BackgroundService for write item in channel And read item .
```csharp
// read item in channel
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
try
{
var result = await _channelReader.ReadAsync(stoppingToken);
Console.WriteLine("read item : {0}", result);
}
catch (ChannelClosedException)
{
Console.WriteLine("Channel Closed.");
}
}
}
// write item in channel
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
int count = 0;
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000);
Console.WriteLine("write item in channel");
await _channelWriter.WriteAsync(count);
count++;
}
}
```
### now you must add services
```csharp
builder.Services.AddSingleton(Channel.CreateUnbounded(new UnboundedChannelOptions() { SingleReader = true }));
builder.Services.AddSingleton(svc => svc.GetRequiredService>().Reader);
builder.Services.AddSingleton(svc => svc.GetRequiredService>().Writer);builder.Services.AddHostedService();
builder.Services.AddHostedService();
```
I Dont hal for type to how to worker :))))))