(feature): dependencies update
This commit is contained in:
parent
5327b19e04
commit
d07fc13b56
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Maksym Sadovnychyy (MAKS-IT)
|
||||
Copyright (c) 2024 - 2025 Maksym Sadovnychyy (MAKS-IT)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@ -1,14 +1,26 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MaksIT.Dapr.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
|
||||
namespace MaksIT.Dapr.Extensions;
|
||||
|
||||
public static class ServiceCollectionExtensions {
|
||||
private static bool _isDaprClientRegistered = false;
|
||||
|
||||
private static void AddDaprClientOnce(this IServiceCollection services) {
|
||||
if (!_isDaprClientRegistered) {
|
||||
services.AddDaprClient();
|
||||
_isDaprClientRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
public static void RegisterPublisher(this IServiceCollection services) {
|
||||
services.AddDaprClient();
|
||||
services.AddSingleton<IDaprPublisherService, DaprService>();
|
||||
services.AddDaprClientOnce();
|
||||
services.AddSingleton<IDaprPublisherService, DaprPublisherService>();
|
||||
}
|
||||
|
||||
public static void RegisterStateStore(this IServiceCollection services) {
|
||||
services.AddDaprClient();
|
||||
services.AddSingleton<IDaprStateStoreService, DaprService>();
|
||||
services.AddDaprClientOnce();
|
||||
services.AddSingleton<IDaprStateStoreService, DaprStateStoreService>();
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,7 @@
|
||||
|
||||
<!-- NuGet package metadata -->
|
||||
<PackageId>MaksIT.Dapr</PackageId>
|
||||
<Version>1.0.7</Version>
|
||||
<Version>1.0.8</Version>
|
||||
<Authors>Maksym Sadovnychyy</Authors>
|
||||
<Company>MAKS-IT</Company>
|
||||
<Product>MaksIT.Dapr</Product>
|
||||
@ -17,16 +17,20 @@
|
||||
<License>MIT</License>
|
||||
<RequireLicenseAcceptance>false</RequireLicenseAcceptance>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../../README.md" Pack="true" PackagePath="" />
|
||||
<None Include="../../LICENSE.md" Pack="true" PackagePath="" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapr.Actors.AspNetCore" Version="1.15.4" />
|
||||
<PackageReference Include="Dapr.AspNetCore" Version="1.15.4" />
|
||||
<PackageReference Include="MaksIT.Results" Version="1.0.6" />
|
||||
<PackageReference Include="Dapr.Actors.AspNetCore" Version="1.16.0" />
|
||||
<PackageReference Include="Dapr.AspNetCore" Version="1.16.0" />
|
||||
<PackageReference Include="Dapr.Workflow" Version="1.16.0" />
|
||||
<PackageReference Include="MaksIT.Core" Version="1.5.1" />
|
||||
<PackageReference Include="MaksIT.Results" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
37
src/MaksIT.Dapr/Services/DaprPublisherService.cs
Normal file
37
src/MaksIT.Dapr/Services/DaprPublisherService.cs
Normal file
@ -0,0 +1,37 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
using Dapr.Client;
|
||||
|
||||
using MaksIT.Results;
|
||||
using MaksIT.Core.Extensions;
|
||||
|
||||
namespace MaksIT.Dapr.Services;
|
||||
public interface IDaprPublisherService {
|
||||
Task<Result> PublishEventAsync(string pubsubName, string topicName, object payload);
|
||||
}
|
||||
|
||||
public class DaprPublisherService : IDaprPublisherService {
|
||||
private const string _errorMessage = "MaksIT.Dapr - Event publishing error";
|
||||
|
||||
private readonly DaprClient _client;
|
||||
private readonly ILogger<DaprPublisherService> _logger;
|
||||
|
||||
public DaprPublisherService(
|
||||
ILogger<DaprPublisherService> logger,
|
||||
DaprClient client
|
||||
) {
|
||||
_logger = logger;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
public async Task<Result> PublishEventAsync(string pubsubName, string topicName, object payload) {
|
||||
try {
|
||||
await _client.PublishEventAsync(pubsubName, topicName, payload);
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, _errorMessage);
|
||||
return Result.InternalServerError([_errorMessage, .. ex.ExtractMessages()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,61 +3,29 @@
|
||||
using Dapr.Client;
|
||||
|
||||
using MaksIT.Results;
|
||||
using MaksIT.Core.Extensions;
|
||||
|
||||
namespace MaksIT.Dapr;
|
||||
|
||||
public interface IDaprPublisherService {
|
||||
Task<Result> PublishEventAsync(string pubSubName, string topicName, string payload);
|
||||
}
|
||||
|
||||
namespace MaksIT.Dapr.Services;
|
||||
public interface IDaprStateStoreService {
|
||||
Task<Result> SetStateAsync<T>(string storeName, string key, T value);
|
||||
Task<Result<T?>> GetStateAsync<T>(string storeName, string key);
|
||||
Task<Result> DeleteStateAsync(string storeName, string key);
|
||||
}
|
||||
|
||||
public class DaprService : IDaprPublisherService, IDaprStateStoreService {
|
||||
|
||||
public class DaprStateStoreService : IDaprStateStoreService {
|
||||
private const string _errorMessage = "MaksIT.Dapr - Data provider error";
|
||||
|
||||
private readonly DaprClient _client;
|
||||
private readonly ILogger<DaprService> _logger;
|
||||
private readonly ILogger<DaprStateStoreService> _logger;
|
||||
|
||||
public DaprService(
|
||||
ILogger<DaprService> logger,
|
||||
public DaprStateStoreService(
|
||||
ILogger<DaprStateStoreService> logger,
|
||||
DaprClient client
|
||||
) {
|
||||
_logger = logger;
|
||||
_client = client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Publishes an event to a Dapr topic
|
||||
/// </summary>
|
||||
/// <param name="pubSubName"></param>
|
||||
/// <param name="topicName"></param>
|
||||
/// <param name="payload"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<Result> PublishEventAsync(string pubSubName, string topicName, string payload) {
|
||||
try {
|
||||
|
||||
var traceId = System.Diagnostics.Activity.Current?.TraceId.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(traceId)) {
|
||||
var metadata = new Dictionary<string, string> { ["traceid"] = traceId };
|
||||
await _client.PublishEventAsync(pubSubName, topicName, payload, metadata);
|
||||
}
|
||||
else {
|
||||
await _client.PublishEventAsync(pubSubName, topicName, payload);
|
||||
}
|
||||
|
||||
return Result.Ok();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, _errorMessage);
|
||||
return Result.InternalServerError(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves a state to a Dapr state store
|
||||
/// </summary>
|
||||
@ -73,7 +41,7 @@ public class DaprService : IDaprPublisherService, IDaprStateStoreService {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, _errorMessage);
|
||||
return Result.InternalServerError(ex.Message);
|
||||
return Result.InternalServerError(new[] {_errorMessage}.Concat(ex.ExtractMessages()).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +62,7 @@ public class DaprService : IDaprPublisherService, IDaprStateStoreService {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, _errorMessage);
|
||||
return Result<T?>.InternalServerError(default, ex.Message);
|
||||
return Result<T?>.InternalServerError(default, new[] {_errorMessage}.Concat(ex.ExtractMessages()).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
@ -111,7 +79,7 @@ public class DaprService : IDaprPublisherService, IDaprStateStoreService {
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, _errorMessage);
|
||||
return Result.InternalServerError(ex.Message);
|
||||
return Result.InternalServerError([_errorMessage, .. ex.ExtractMessages()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,3 +5,5 @@ cd /d %~dp0
|
||||
|
||||
REM Invoke the PowerShell script (Release-NuGetPackage.ps1) in the same directory
|
||||
powershell -ExecutionPolicy Bypass -File "%~dp0Release-NuGetPackage.ps1"
|
||||
|
||||
pause
|
||||
Loading…
Reference in New Issue
Block a user