initial commit
This commit is contained in:
9
.github/config/gitversion.yml
vendored
Normal file
9
.github/config/gitversion.yml
vendored
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
next-version: 1.0.0
|
||||||
|
mode: ContinuousDeployment
|
||||||
|
assembly-versioning-scheme: MajorMinorPatch
|
||||||
|
assembly-file-versioning-scheme: MajorMinorPatch
|
||||||
|
|
||||||
|
branches:
|
||||||
|
main:
|
||||||
|
regex: ^main$
|
||||||
|
increment: Patch
|
||||||
24
.github/workflows/pull_request_dev.yml
vendored
Normal file
24
.github/workflows/pull_request_dev.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: Build NuGet package on dev branch pull request
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- "dev"
|
||||||
|
paths:
|
||||||
|
- "TimetableDesigner.Backend.Events.Providers.RabbitMQ**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v6.0.2
|
||||||
|
- name: Setup .NET
|
||||||
|
uses: actions/setup-dotnet@v3
|
||||||
|
with:
|
||||||
|
dotnet-version: 10.0.x
|
||||||
|
- name: Add source
|
||||||
|
run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json"
|
||||||
|
- name: Build
|
||||||
|
run: dotnet build
|
||||||
24
.github/workflows/pull_request_main.yml
vendored
Normal file
24
.github/workflows/pull_request_main.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: Build NuGet package on main branch pull request
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
paths:
|
||||||
|
- "TimetableDesigner.Backend.Events.Providers.RabbitMQ**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v6.0.2
|
||||||
|
- name: Setup .NET
|
||||||
|
uses: actions/setup-dotnet@v3
|
||||||
|
with:
|
||||||
|
dotnet-version: 10.0.x
|
||||||
|
- name: Add source
|
||||||
|
run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json"
|
||||||
|
- name: Build
|
||||||
|
run: dotnet build
|
||||||
24
.github/workflows/push_dev.yml
vendored
Normal file
24
.github/workflows/push_dev.yml
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
name: Build NuGet package on dev branch push
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "dev"
|
||||||
|
paths:
|
||||||
|
- "TimetableDesigner.Backend.Events.Providers.RabbitMQ**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v6.0.2
|
||||||
|
- name: Setup .NET
|
||||||
|
uses: actions/setup-dotnet@v3
|
||||||
|
with:
|
||||||
|
dotnet-version: 10.0.x
|
||||||
|
- name: Add source
|
||||||
|
run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json"
|
||||||
|
- name: Build
|
||||||
|
run: dotnet build
|
||||||
74
.github/workflows/push_main.yml
vendored
Normal file
74
.github/workflows/push_main.yml
vendored
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
name: Build NuGet package on main branch push
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "main"
|
||||||
|
paths:
|
||||||
|
- "TimetableDesigner.Backend.Events.Providers.RabbitMQ**"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: Build and pack
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v6.0.2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Setup .NET
|
||||||
|
uses: actions/setup-dotnet@v3
|
||||||
|
with:
|
||||||
|
dotnet-version: 10.0.x
|
||||||
|
- name: Add source
|
||||||
|
run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json"
|
||||||
|
- name: Build
|
||||||
|
run: dotnet build
|
||||||
|
- name: Setup GitVersion
|
||||||
|
uses: gittools/actions/gitversion/setup@v4.2.0
|
||||||
|
with:
|
||||||
|
versionSpec: 6.4.x
|
||||||
|
- name: Determine version
|
||||||
|
uses: gittools/actions/gitversion/execute@v4.2.0
|
||||||
|
id: gitversion
|
||||||
|
with:
|
||||||
|
configFilePath: ./.github/config/gitversion.yml
|
||||||
|
- name: Create NuGet package
|
||||||
|
run: dotnet pack -c Release -p:Version=${{steps.gitversion.outputs.SemVer}} -p:PackageVersion=${{steps.gitversion.outputs.SemVer}} --output ${{ github.workspace}}/nuget
|
||||||
|
- name: Upload artifact
|
||||||
|
uses: actions/upload-artifact@v6
|
||||||
|
with:
|
||||||
|
name: nuget
|
||||||
|
path: ${{ github.workspace}}/nuget/*.nupkg
|
||||||
|
outputs:
|
||||||
|
version: ${{ steps.gitversion.outputs.SemVer }}
|
||||||
|
publish:
|
||||||
|
name: Publish
|
||||||
|
permissions: write-all
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
needs: build
|
||||||
|
steps:
|
||||||
|
- name: Download artifact
|
||||||
|
uses: actions/download-artifact@v6
|
||||||
|
with:
|
||||||
|
name: nuget
|
||||||
|
path: ${{ github.workspace}}/nuget
|
||||||
|
- name: Setup .NET
|
||||||
|
uses: actions/setup-dotnet@v3
|
||||||
|
with:
|
||||||
|
dotnet-version: 10.0.x
|
||||||
|
- name: Add source
|
||||||
|
run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json"
|
||||||
|
- name: Publish
|
||||||
|
run: dotnet nuget push ${{ github.workspace}}/nuget/*.nupkg --source "github" --api-key ${{ secrets.NUGET_REGISTRY_API_KEY }} --skip-duplicate
|
||||||
|
- name: Create Release
|
||||||
|
uses: actions/create-release@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
tag_name: ${{needs.build.outputs.version}}
|
||||||
|
release_name: ${{needs.build.outputs.version}}
|
||||||
|
draft: false
|
||||||
|
prerelease: false
|
||||||
|
|
||||||
|
|
||||||
289
.gitignore
vendored
Normal file
289
.gitignore
vendored
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
|
||||||
|
# Visual Studio 2015 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUNIT
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# .NET Core
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
**/Properties/launchSettings.json
|
||||||
|
|
||||||
|
# VS Code
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_i.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*.log
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# JustCode is a .NET coding add-in
|
||||||
|
.JustCode
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/packages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/packages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/packages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Typescript v1 declaration files
|
||||||
|
typings/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# JetBrains Rider
|
||||||
|
.idea/
|
||||||
|
*.sln.iml
|
||||||
|
|
||||||
|
# CodeRush
|
||||||
|
.cr/
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
3
TimetableDesigner.Backend.Events.Providers.RabbitMQ.slnx
Normal file
3
TimetableDesigner.Backend.Events.Providers.RabbitMQ.slnx
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="TimetableDesigner.Backend.Events.Providers.RabbitMQ/TimetableDesigner.Backend.Events.Providers.RabbitMQ.csproj" />
|
||||||
|
</Solution>
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
|
||||||
|
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
|
||||||
|
|
||||||
|
public class RabbitMQEventQueue : EventQueue
|
||||||
|
{
|
||||||
|
public string Hostname { get; set; } = null!;
|
||||||
|
public int Port { get; set; }
|
||||||
|
public string Username { get; set; } = null!;
|
||||||
|
public string Password { get; set; } = null!;
|
||||||
|
public string ExchangeName { get; set; } = null!;
|
||||||
|
public string QueuePrefix { get; set; } = null!;
|
||||||
|
|
||||||
|
public override void Setup(IServiceCollection services)
|
||||||
|
{
|
||||||
|
ConnectionFactory factory = new ConnectionFactory
|
||||||
|
{
|
||||||
|
HostName = Hostname,
|
||||||
|
Port = Port,
|
||||||
|
UserName = Username,
|
||||||
|
Password = Password,
|
||||||
|
};
|
||||||
|
|
||||||
|
Task<IConnection> createConnectionTask = factory.CreateConnectionAsync();
|
||||||
|
createConnectionTask.Wait();
|
||||||
|
services.AddSingleton(createConnectionTask.Result);
|
||||||
|
services.AddSingleton<IEventQueuePublisher, RabbitMQEventQueuePublisher>(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService<IConnection>(), ExchangeName));
|
||||||
|
services.AddSingleton<IEventQueueSubscriber, RabbitMQEventQueueSubscriber>(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService<IConnection>(), ExchangeName, QueuePrefix));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
|
||||||
|
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
|
||||||
|
|
||||||
|
public class RabbitMQEventQueuePublisher : IEventQueuePublisher
|
||||||
|
{
|
||||||
|
private readonly IConnection _connection;
|
||||||
|
private readonly string _exchangeName;
|
||||||
|
|
||||||
|
public RabbitMQEventQueuePublisher(IConnection connection, string exchangeName)
|
||||||
|
{
|
||||||
|
_connection = connection;
|
||||||
|
_exchangeName = exchangeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task PublishAsync<T>(T eventData) where T : class
|
||||||
|
{
|
||||||
|
string routingKey = typeof(T).FullName!;
|
||||||
|
BasicProperties properties = new BasicProperties
|
||||||
|
{
|
||||||
|
ContentType = "application/json",
|
||||||
|
DeliveryMode = DeliveryModes.Persistent,
|
||||||
|
Type = typeof(T).FullName,
|
||||||
|
|
||||||
|
};
|
||||||
|
ReadOnlyMemory<byte> body = JsonSerializer.SerializeToUtf8Bytes(eventData);
|
||||||
|
await using (IChannel channel = await _connection.CreateChannelAsync())
|
||||||
|
{
|
||||||
|
await channel.ExchangeDeclareAsync(_exchangeName, ExchangeType.Direct);
|
||||||
|
await channel.BasicPublishAsync(_exchangeName, routingKey, true, properties, body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using RabbitMQ.Client;
|
||||||
|
using RabbitMQ.Client.Events;
|
||||||
|
|
||||||
|
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
|
||||||
|
|
||||||
|
public class RabbitMQEventQueueSubscriber : IEventQueueSubscriber
|
||||||
|
{
|
||||||
|
private readonly IConnection _connection;
|
||||||
|
private readonly string _exchangeName;
|
||||||
|
private readonly string _queuePrefix;
|
||||||
|
|
||||||
|
public RabbitMQEventQueueSubscriber(IConnection connection, string exchangeName, string queuePrefix)
|
||||||
|
{
|
||||||
|
_connection = connection;
|
||||||
|
_exchangeName = exchangeName;
|
||||||
|
_queuePrefix = queuePrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Subscribe<T>(Func<T, Task> handler) where T : class
|
||||||
|
{
|
||||||
|
Task<IChannel> createChannelTask = _connection.CreateChannelAsync();
|
||||||
|
createChannelTask.Wait();
|
||||||
|
IChannel channel = createChannelTask.Result;
|
||||||
|
|
||||||
|
string queueName = $"{_queuePrefix}-{typeof(T).FullName!}";
|
||||||
|
Task exchangeDeclareTask = channel.ExchangeDeclareAsync(_exchangeName, ExchangeType.Direct);
|
||||||
|
Task queueDeclareTask = channel.QueueDeclareAsync(queueName, true, false, false);
|
||||||
|
Task.WaitAll(exchangeDeclareTask, queueDeclareTask);
|
||||||
|
channel.QueueBindAsync(queueName, _exchangeName, typeof(T).FullName!).Wait();
|
||||||
|
|
||||||
|
AsyncEventingBasicConsumer consumer = new AsyncEventingBasicConsumer(channel);
|
||||||
|
consumer.ReceivedAsync += async (_, args) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
T? eventData = JsonSerializer.Deserialize<T>(args.Body.Span);
|
||||||
|
if (eventData is null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Received null event");
|
||||||
|
}
|
||||||
|
|
||||||
|
await handler(eventData);
|
||||||
|
|
||||||
|
await channel.BasicAckAsync(args.DeliveryTag, false);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await channel.BasicNackAsync(args.DeliveryTag, false, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
channel.BasicConsumeAsync(queueName, false, consumer).Wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="RabbitMQ.Client" Version="7.2.0" />
|
||||||
|
<PackageReference Include="timetabledesigner.backend.events" Version="1.1.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
BIN
TimetableDesigner.Backend.Events.Providers.RabbitMQ/icon.png
Normal file
BIN
TimetableDesigner.Backend.Events.Providers.RabbitMQ/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 30 KiB |
Reference in New Issue
Block a user