Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
aeb6e2aa
Commit
aeb6e2aa
authored
Aug 22, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add SignalR, test using server time notifier
parent
33ad80b5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
1 deletion
+113
-1
Program.cs
Clinics.Backend/API/Program.cs
+20
-1
ServerTimeNotifier.cs
...re/BackgroundServices/Notifications/ServerTimeNotifier.cs
+38
-0
Infrastructure.csproj
Clinics.Backend/Infrastructure/Infrastructure.csproj
+35
-0
NotificationHub.cs
...ackend/Infrastructure/NotificationsHub/NotificationHub.cs
+20
-0
No files found.
Clinics.Backend/API/Program.cs
View file @
aeb6e2aa
...
@@ -3,6 +3,8 @@ using API.Options.JWT;
...
@@ -3,6 +3,8 @@ using API.Options.JWT;
using
API.SeedDatabaseHelper
;
using
API.SeedDatabaseHelper
;
using
Application.Behaviors
;
using
Application.Behaviors
;
using
FluentValidation
;
using
FluentValidation
;
using
Infrastructure.BackgroundServices.Notifications
;
using
Infrastructure.NotificationsService
;
using
MediatR
;
using
MediatR
;
using
Microsoft.AspNetCore.Authentication.JwtBearer
;
using
Microsoft.AspNetCore.Authentication.JwtBearer
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore
;
...
@@ -12,6 +14,10 @@ using Persistence.Context;
...
@@ -12,6 +14,10 @@ using Persistence.Context;
var
builder
=
WebApplication
.
CreateBuilder
(
args
);
var
builder
=
WebApplication
.
CreateBuilder
(
args
);
// Add services to the container.
#region Add Database context
#region Add Database context
// First, get database options
// First, get database options
builder
.
Services
.
ConfigureOptions
<
DatabaseOptionsSetup
>();
builder
.
Services
.
ConfigureOptions
<
DatabaseOptionsSetup
>();
...
@@ -35,7 +41,16 @@ builder.Services.AddDbContext<ClinicsDbContext>(
...
@@ -35,7 +41,16 @@ builder.Services.AddDbContext<ClinicsDbContext>(
});
});
#endregion
#endregion
// Add services to the container.
#region Add SignalR
builder
.
Services
.
AddSignalR
();
// Background services:
builder
.
Services
.
AddHostedService
<
ServerTimeNotifier
>();
#endregion
#region Add CORS
#endregion
#region Link interfaces implemented in persistence
#region Link interfaces implemented in persistence
// Using Scrutor library
// Using Scrutor library
...
@@ -126,6 +141,10 @@ if (app.Environment.IsDevelopment())
...
@@ -126,6 +141,10 @@ if (app.Environment.IsDevelopment())
app
.
UseHttpsRedirection
();
app
.
UseHttpsRedirection
();
#region Map notification HUB
app
.
MapHub
<
NotificationHub
>(
"notifications"
);
#endregion
#region CORS
#region CORS
// TODO: Configure allows
// TODO: Configure allows
app
.
UseCors
(
policy
=>
policy
.
AllowAnyHeader
().
AllowAnyMethod
().
AllowAnyHeader
().
AllowAnyOrigin
());
app
.
UseCors
(
policy
=>
policy
.
AllowAnyHeader
().
AllowAnyMethod
().
AllowAnyHeader
().
AllowAnyOrigin
());
...
...
Clinics.Backend/Infrastructure/BackgroundServices/Notifications/ServerTimeNotifier.cs
0 → 100644
View file @
aeb6e2aa
using
Infrastructure.NotificationsService
;
using
Microsoft.AspNetCore.SignalR
;
using
Microsoft.Extensions.Hosting
;
using
Microsoft.Extensions.Logging
;
namespace
Infrastructure.BackgroundServices.Notifications
;
public
class
ServerTimeNotifier
:
BackgroundService
{
private
static
readonly
TimeSpan
Period
=
TimeSpan
.
FromSeconds
(
3
);
#
region
CTOR
DI
private
readonly
ILogger
<
ServerTimeNotifier
>
_logger
;
private
readonly
IHubContext
<
NotificationHub
,
INotificationClient
>
_context
;
public
ServerTimeNotifier
(
ILogger
<
ServerTimeNotifier
>
logger
,
IHubContext
<
NotificationHub
,
INotificationClient
>
context
)
{
_logger
=
logger
;
_context
=
context
;
}
#
endregion
protected
override
async
Task
ExecuteAsync
(
CancellationToken
stoppingToken
)
{
using
var
timer
=
new
PeriodicTimer
(
Period
);
while
(!
stoppingToken
.
IsCancellationRequested
&&
await
timer
.
WaitForNextTickAsync
(
stoppingToken
))
{
var
dateTime
=
DateTime
.
Now
;
_logger
.
LogInformation
(
"Executing {Service} {Time}"
,
nameof
(
ServerTimeNotifier
),
dateTime
);
await
_context
.
Clients
.
All
.
ReceiveNotification
(
$"Server time =
{
dateTime
}
"
);
}
}
}
Clinics.Backend/Infrastructure/Infrastructure.csproj
0 → 100644
View file @
aeb6e2aa
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.2" />
<PackageReference Include="MediatR" Version="12.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Application\Application.csproj" />
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="BackgroundServices\" />
</ItemGroup>
</Project>
Clinics.Backend/Infrastructure/NotificationsHub/NotificationHub.cs
0 → 100644
View file @
aeb6e2aa
using
Microsoft.AspNetCore.SignalR
;
namespace
Infrastructure.NotificationsService
;
public
class
NotificationHub
:
Hub
<
INotificationClient
>
{
public
override
async
Task
OnConnectedAsync
()
{
await
Clients
.
Client
(
Context
.
ConnectionId
).
ReceiveNotification
(
$"Connected successfully
{
Context
.
User
?.
Identity
?.
Name
}
"
);
await
base
.
OnConnectedAsync
();
}
}
public
interface
INotificationClient
{
Task
ReceiveNotification
(
string
message
);
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment