Created
March 12, 2021 15:19
-
-
Save EngRajabi/8a341aed4cc68728f812baff7961c3a4 to your computer and use it in GitHub Desktop.
ChannelService.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
public class ChannelService<TMessage> where TMessage : class, new() | |
{ | |
private readonly Channel<TMessage> _serviceChannel; | |
public ChannelService() | |
{ | |
_serviceChannel = Channel.CreateBounded<TMessage>(new BoundedChannelOptions(5000) | |
{ | |
SingleReader = false, | |
SingleWriter = false, | |
FullMode = BoundedChannelFullMode.DropWrite | |
}); | |
} | |
public async Task Add(TMessage model, CancellationToken cancellationToken) | |
{ | |
await _serviceChannel.Writer.WriteAsync(model, cancellationToken); | |
} | |
public IAsyncEnumerable<TMessage> Get(CancellationToken cancellationToken) | |
{ | |
return _serviceChannel.Reader.ReadAllAsync(cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment