Skip to content

Commit 39d4b97

Browse files
committed
Enforcing options based constructor for derived ServerSentEventService. Resolves #33
1 parent 0b94dc1 commit 39d4b97

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

Benchmark.AspNetCore.ServerSentEvents/Benchmarks/ServerSentEventsServiceBenchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using System.Security.Claims;
55
using System.Collections.Generic;
6+
using Microsoft.Extensions.Options;
67
using BenchmarkDotNet.Attributes;
78
using Lib.AspNetCore.ServerSentEvents;
89
using Lib.AspNetCore.ServerSentEvents.Internals;
@@ -34,7 +35,7 @@ public ServerSentEventsServiceBenchmarks()
3435
{
3536
_serverSentEventsClient = new ServerSentEventsClient(Guid.NewGuid(), new ClaimsPrincipal(), new NoOpHttpResponse());
3637

37-
_serverSentEventsService = new ServerSentEventsService();
38+
_serverSentEventsService = new ServerSentEventsService(Options.Create<ServerSentEventsServiceOptions<ServerSentEventsService>>(null));
3839
for (int i = 0; i < MULTIPLE_CLIENTS_COUNT; i++)
3940
{
4041
_serverSentEventsService.AddClient(new ServerSentEventsClient(Guid.NewGuid(), new ClaimsPrincipal(), new NoOpHttpResponse()));

DocFx.AspNetCore.ServerSentEvents/articles/getting-started.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ public class Startup
2727
}
2828
```
2929

30-
The `AddServerSendEvents()` method registers default service which provides operations over Server-Sent Events protocol, while `MapServerSentEvents()` maps this service to a specific endpoint. The `MapServerSentEvents()` method can be called multiple times for different endpoints but all of them will be mapped to same service which will make them indistinguishable from application perspective. In order to create distinguishable endpoints a delivered version of [`Server​Sent​Events​Service`](../api/Lib.AspNetCore.ServerSentEvents.ServerSentEventsService.html) should be registered (it doesn't need to override any of the default service functionality). Below is an example of such configuration.
30+
The `AddServerSendEvents()` method registers default service which provides operations over Server-Sent Events protocol, while `MapServerSentEvents()` maps this service to a specific endpoint. The `MapServerSentEvents()` method can be called multiple times for different endpoints but all of them will be mapped to same service which will make them indistinguishable from application perspective. In order to create distinguishable endpoints a delivered version of [`Server​Sent​Events​Service`](../api/Lib.AspNetCore.ServerSentEvents.ServerSentEventsService.html) should be registered. It doesn't need to override any of the default service functionality, just provide a constructor which can take a service specific options. Below is an example of such configuration.
3131

3232
```cs
3333
internal interface INotificationsServerSentEventsService : IServerSentEventsService
3434
{ }
3535

3636
internal class NotificationsServerSentEventsService : ServerSentEventsService, INotificationsServerSentEventsService
37-
{ }
37+
{
38+
public NotificationsServerSentEventsService(IOptions<ServerSentEventsServiceOptions<NotificationsServerSentEventsService>> options)
39+
: base(options.ToBaseServerSentEventsServiceOptions())
40+
{ }
41+
}
3842

3943
public class Startup
4044
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace Lib.AspNetCore.ServerSentEvents
4+
{
5+
/// <summary>
6+
/// The <see cref="ServerSentEventsServiceOptions{TServerSentEventsService}"/> options related extensions.
7+
/// </summary>
8+
public static class OptionsExtensions
9+
{
10+
/// <summary>
11+
/// Converts an instace of <see cref="ServerSentEventsServiceOptions{TServerSentEventsService}"/> to <see cref="ServerSentEventsServiceOptions{ServerSentEventsService}"/>.
12+
/// </summary>
13+
/// <typeparam name="TServerSentEventsService">The type of <see cref="ServerSentEventsService"/> for the options instance.</typeparam>
14+
/// <param name="options">The options instance.</param>
15+
/// <returns>The option instance</returns>
16+
public static IOptions<ServerSentEventsServiceOptions<ServerSentEventsService>> ToBaseServerSentEventsServiceOptions<TServerSentEventsService>(this IOptions<ServerSentEventsServiceOptions<TServerSentEventsService>> options) where TServerSentEventsService : ServerSentEventsService
17+
{
18+
if (options is null)
19+
{
20+
return null;
21+
}
22+
23+
if (options.Value is null)
24+
{
25+
return Options.Create<ServerSentEventsServiceOptions<ServerSentEventsService>>(null);
26+
}
27+
28+
return Options.Create(new ServerSentEventsServiceOptions<ServerSentEventsService>
29+
{
30+
KeepaliveInterval = options.Value.KeepaliveInterval,
31+
KeepaliveMode = options.Value.KeepaliveMode,
32+
OnClientConnected = options.Value.OnClientConnected,
33+
OnClientDisconnected = options.Value.OnClientDisconnected
34+
});
35+
}
36+
}
37+
}

Lib.AspNetCore.ServerSentEvents/Lib.AspNetCore.ServerSentEvents.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<PropertyGroup>
33
<Description>Lib.AspNetCore.ServerSentEvents is a library which provides Server-Sent Events (SSE) support for ASP.NET Core</Description>
44
<Copyright>Copyright © 2017 - 2020 Tomasz Pęczek</Copyright>
5-
<VersionPrefix>4.1.0</VersionPrefix>
6-
<VersionSuffix></VersionSuffix>
5+
<VersionPrefix>5.0.0</VersionPrefix>
6+
<VersionSuffix>alpha1</VersionSuffix>
77
<Authors>Tomasz Pęczek</Authors>
88
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0;net461</TargetFrameworks>
99
<AssemblyTitle>Lib.AspNetCore.ServerSentEvents</AssemblyTitle>

Lib.AspNetCore.ServerSentEvents/ServerSentEventsService.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ public class ServerSentEventsService : IServerSentEventsService
3636
#endregion
3737

3838
#region Constructors
39-
/// <summary>
40-
/// Initializes new instance of <see cref="ServerSentEventsService"/>.
41-
/// </summary>
42-
public ServerSentEventsService()
43-
{ }
44-
4539
/// <summary>
4640
/// Initializes new instance of <see cref="ServerSentEventsService"/>.
4741
/// </summary>

0 commit comments

Comments
 (0)