-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventService.cs
141 lines (119 loc) · 3.61 KB
/
EventService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reactive.Subjects;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
// Observable service for event-driven communication
public class EventService : IObservable<string>
{
private readonly Subject<string> _subject = new();
public IDisposable Subscribe(IObserver<string> observer)
{
return _subject.Subscribe(observer);
}
public void Publish(string message)
{
_subject.OnNext(message);
}
}
// Singleton holding a ConcurrentDictionary
public class DataStore
{
private readonly ConcurrentDictionary<string, string> _dictionary = new();
public void AddOrUpdate(string key, string value)
{
_dictionary.AddOrUpdate(key, value, (_, _) => value);
Console.WriteLine($"DataStore updated: [{key}] = {value}");
}
}
// Observer for the EventService
public class EventObserver : IObserver<string>
{
private readonly DataStore _dataStore;
public EventObserver(DataStore dataStore)
{
_dataStore = dataStore;
}
public void OnNext(string value)
{
Console.WriteLine($"EventObserver received: {value}");
_dataStore.AddOrUpdate(value, DateTime.UtcNow.ToString());
}
public void OnError(Exception error)
{
Console.WriteLine($"Error: {error.Message}");
}
public void OnCompleted()
{
Console.WriteLine("EventObserver: Completed");
}
}
// HostedService publishing events
public class PublisherHostedService : IHostedService
{
private readonly EventService _eventService;
public PublisherHostedService(EventService eventService)
{
_eventService = eventService;
}
public Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("PublisherHostedService started.");
Task.Run(async () =>
{
for (int i = 0; i < 5; i++)
{
_eventService.Publish($"Message {i}");
await Task.Delay(1000, cancellationToken);
}
}, cancellationToken);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("PublisherHostedService stopped.");
return Task.CompletedTask;
}
}
// BackgroundService consuming the events
public class ConsumerBackgroundService : BackgroundService
{
private readonly EventService _eventService;
private IDisposable _subscription;
public ConsumerBackgroundService(EventService eventService, DataStore dataStore)
{
_eventService = eventService;
var observer = new EventObserver(dataStore);
_subscription = _eventService.Subscribe(observer);
}
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine("ConsumerBackgroundService is running.");
return Task.CompletedTask;
}
public override void Dispose()
{
_subscription.Dispose();
base.Dispose();
}
}
// Program
public class Program
{
public static async Task Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((_, services) =>
{
services.AddSingleton<EventService>();
services.AddSingleton<DataStore>();
services.AddHostedService<PublisherHostedService>();
services.AddHostedService<ConsumerBackgroundService>();
})
.Build();
await host.RunAsync();
}
}