diff --git a/docs/en/Dapr/Index.md b/docs/en/Dapr/Index.md index 2596c449d1..060b31aac3 100644 --- a/docs/en/Dapr/Index.md +++ b/docs/en/Dapr/Index.md @@ -209,7 +209,7 @@ ABP provides the following endpoints to receive events from Dapr: * `dapr/subscribe`: Dapr uses this endpoint to get a list of subscriptions from the application. ABP automatically returns all the subscriptions for your distributed event handler classes and custom controller actions with the `Topic` attribute. * `api/abp/dapr/event`: The unified endpoint to receive all the events from Dapr. ABP dispatches the events to your event handlers based on the topic name. -> **Since ABP provides the standard `dapr/subscribe` endpoint, you should not manually call the `app.MapSubscribeHandler()` method of Dapr.** You can use the `app.UseCloudEvents()` middleware in your ASP.NET Core pipeline if you want to support the [CloudEvents](https://cloudevents.io/) standard. +> **Since ABP will call `MapSubscribeHandler` internally, you should not manually call it anymore.** You can use the `app.UseCloudEvents()` middleware in your ASP.NET Core pipeline if you want to support the [CloudEvents](https://cloudevents.io/) standard. ### Usage @@ -299,8 +299,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync( - [FromBody] StockCountChangedEto model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { HttpContext.ValidateDaprAppApiToken(); @@ -430,8 +429,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync( - [FromBody] StockCountChangedEto model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { // Validate the App API token! HttpContext.ValidateDaprAppApiToken(); diff --git a/docs/zh-Hans/Dapr/Index.md b/docs/zh-Hans/Dapr/Index.md index 30936eb6da..64a9451737 100644 --- a/docs/zh-Hans/Dapr/Index.md +++ b/docs/zh-Hans/Dapr/Index.md @@ -209,7 +209,7 @@ ABP提供了以下端点来接收来自Dapr的事件: * `dapr/subscribe`: Dapr使用此端点从应用程序获取订阅列表.ABP会自动返回所有分布式事件处理程序类和具有`Topic`属性的自定义控制器操作的订阅. * `api/abp/dapr/event`: 用于接收来自Dapr的所有事件的统一端点.ABP根据主题名称将事件分派给您的事件处理程序. -> **由于ABP提供了标准的`dapr/subscribe`端点,所以你不应该手动调用Dapr的`app.MapSubscribeHandler()`方法.** 如果你想支持[CloudEvents](https://cloudevents.io/)标准,你可以在你的ASP.NET Core管道中使用`app.UseCloudEvents()`中间件. +> **由于ABP会在内部调用`MapSubscribeHandler` 方法,所以你不应该手动调用了.** 如果你想支持[CloudEvents](https://cloudevents.io/)标准,你可以在你的ASP.NET Core管道中使用`app.UseCloudEvents()`中间件. ### 用法 @@ -299,8 +299,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync( - [FromBody] StockCountChangedEto model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { HttpContext.ValidateDaprAppApiToken(); @@ -430,8 +429,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync( - [FromBody] StockCountChangedEto model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { // Validate the App API token! HttpContext.ValidateDaprAppApiToken(); diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/DaprAspNetCore/AbpDaprEndpointRouteBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/DaprAspNetCore/AbpDaprEndpointRouteBuilderExtensions.cs new file mode 100644 index 0000000000..b5866c5a68 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/DaprAspNetCore/AbpDaprEndpointRouteBuilderExtensions.cs @@ -0,0 +1,296 @@ +// ------------------------------------------------------------------------ +// Copyright 2021 The Dapr Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ------------------------------------------------------------------------ + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Dapr +{ + /// + /// This class defines configurations for the subscribe endpoint. + /// + public class AbpSubscribeOptions + { + /// + /// Gets or Sets a value which indicates whether to enable or disable processing raw messages. + /// + public bool EnableRawPayload { get; set; } + + /// + /// An optional delegate used to configure the subscriptions. + /// + public Func, Task> SubscriptionsCallback { get; set; } + } + + /// + /// This class defines subscribe endpoint response + /// + public class AbpSubscription + { + /// + /// Gets or sets the topic name. + /// + public string Topic { get; set; } + + /// + /// Gets or sets the pubsub name + /// + public string PubsubName { get; set; } + + /// + /// Gets or sets the route + /// + public string Route { get; set; } + + /// + /// Gets or sets the routes + /// + public AbpRoutes Routes { get; set; } + + /// + /// Gets or sets the metadata. + /// + public AbpMetadata Metadata { get; set; } + + /// + /// Gets or sets the deadletter topic. + /// + public string DeadLetterTopic { get; set; } + } + + /// + /// This class defines the metadata for subscribe endpoint. + /// + public class AbpMetadata : Dictionary + { + /// + /// Initializes a new instance of the Metadata class. + /// + public AbpMetadata() { } + + /// + /// Initializes a new instance of the Metadata class. + /// + /// + public AbpMetadata(IDictionary dictionary) : base(dictionary) { } + + /// + /// RawPayload key + /// + internal const string RawPayload = "rawPayload"; + } + + /// + /// This class defines the routes for subscribe endpoint. + /// + public class AbpRoutes + { + /// + /// Gets or sets the default route + /// + public string Default { get; set; } + + /// + /// Gets or sets the routing rules + /// + public List Rules { get; set; } + } + + /// + /// This class defines the rule for subscribe endpoint. + /// + public class AbpRule + { + /// + /// Gets or sets the CEL expression to match this route. + /// + public string Match { get; set; } + + /// + /// Gets or sets the path of the route. + /// + public string Path { get; set; } + } +} + +namespace Microsoft.AspNetCore.Builder +{ + using System.Collections.Generic; + using System.Linq; + using System.Text.Json; + using System.Text.Json.Serialization; + using Dapr; + using Microsoft.AspNetCore.Http; + using Microsoft.AspNetCore.Routing; + using Microsoft.AspNetCore.Routing.Patterns; + using Microsoft.Extensions.DependencyInjection; + using Microsoft.Extensions.Logging; + + /// + /// Contains extension methods for . + /// + public static class AbpDaprEndpointRouteBuilderExtensions + { + /// + /// Maps an endpoint that will respond to requests to /dapr/subscribe from the + /// Dapr runtime. + /// + /// The . + /// The . + public static IEndpointConventionBuilder MapAbpSubscribeHandler(this IEndpointRouteBuilder endpoints) + { + return CreateSubscribeEndPoint(endpoints); + } + + /// + /// Maps an endpoint that will respond to requests to /dapr/subscribe from the + /// Dapr runtime. + /// + /// The . + /// Configuration options + /// The . + /// + public static IEndpointConventionBuilder MapAbpSubscribeHandler(this IEndpointRouteBuilder endpoints, AbpSubscribeOptions options) + { + return CreateSubscribeEndPoint(endpoints, options); + } + + private static IEndpointConventionBuilder CreateSubscribeEndPoint(IEndpointRouteBuilder endpoints, AbpSubscribeOptions options = null) + { + if (endpoints is null) + { + throw new System.ArgumentNullException(nameof(endpoints)); + } + + return endpoints.MapGet("dapr/subscribe", async context => + { + var logger = context.RequestServices.GetRequiredService().CreateLogger("DaprTopicSubscription"); + var dataSource = context.RequestServices.GetRequiredService(); + var subscriptions = dataSource.Endpoints + .OfType() + .Where(e => e.Metadata.GetOrderedMetadata().Any(t => t.Name != null)) // only endpoints which have TopicAttribute with not null Name. + .SelectMany(e => + { + var topicMetadata = e.Metadata.GetOrderedMetadata(); + var originalTopicMetadata = e.Metadata.GetOrderedMetadata(); + + var subs = new List<(string PubsubName, string Name, string DeadLetterTopic, bool? EnableRawPayload, string Match, int Priority, Dictionary OriginalTopicMetadata, string MetadataSeparator, RoutePattern RoutePattern)>(); + + for (int i = 0; i < topicMetadata.Count(); i++) + { + subs.Add((topicMetadata[i].PubsubName, + topicMetadata[i].Name, + (topicMetadata[i] as IDeadLetterTopicMetadata)?.DeadLetterTopic, + (topicMetadata[i] as IRawTopicMetadata)?.EnableRawPayload, + topicMetadata[i].Match, + topicMetadata[i].Priority, + originalTopicMetadata.Where(m => (topicMetadata[i] as IOwnedOriginalTopicMetadata)?.OwnedMetadatas?.Any(o => o.Equals(m.Id)) == true || string.IsNullOrEmpty(m.Id)) + .GroupBy(c => c.Name) + .ToDictionary(m => m.Key, m => m.Select(c => c.Value).Distinct().ToArray()), + (topicMetadata[i] as IOwnedOriginalTopicMetadata)?.MetadataSeparator, + e.RoutePattern)); + } + + return subs; + }) + .Distinct() + .GroupBy(e => new { e.PubsubName, e.Name }) + .Select(e => e.OrderBy(e => e.Priority)) + .Select(e => + { + var first = e.First(); + var rawPayload = e.Any(e => e.EnableRawPayload.GetValueOrDefault()); + var metadataSeparator = e.FirstOrDefault(e => !string.IsNullOrEmpty(e.MetadataSeparator)).MetadataSeparator ?? ","; + var rules = e.Where(e => !string.IsNullOrEmpty(e.Match)).ToList(); + var defaultRoutes = e.Where(e => string.IsNullOrEmpty(e.Match)).Select(e => RoutePatternToString(e.RoutePattern)).ToList(); + var defaultRoute = defaultRoutes.FirstOrDefault(); + + //multiple identical names. use comma separation. + var metadata = new AbpMetadata(e.SelectMany(c => c.OriginalTopicMetadata).GroupBy(c => c.Key).ToDictionary(c => c.Key, c => string.Join(metadataSeparator, c.SelectMany(c => c.Value).Distinct()))); + if (rawPayload || options?.EnableRawPayload is true) + { + metadata.Add(AbpMetadata.RawPayload, "true"); + } + + if (logger != null) + { + if (defaultRoutes.Count > 1) + { + logger.LogError("A default subscription to topic {name} on pubsub {pubsub} already exists.", first.Name, first.PubsubName); + } + + var duplicatePriorities = rules.GroupBy(e => e.Priority) + .Where(g => g.Count() > 1) + .ToDictionary(x => x.Key, y => y.Count()); + + foreach (var entry in duplicatePriorities) + { + logger.LogError("A subscription to topic {name} on pubsub {pubsub} has duplicate priorities for {priority}: found {count} occurrences.", first.Name, first.PubsubName, entry.Key, entry.Value); + } + } + + var subscription = new AbpSubscription + { + Topic = first.Name, + PubsubName = first.PubsubName, + Metadata = metadata.Count > 0 ? metadata : null, + }; + + if (first.DeadLetterTopic != null) + { + subscription.DeadLetterTopic = first.DeadLetterTopic; + } + + // Use the V2 routing rules structure + if (rules.Count > 0) + { + subscription.Routes = new AbpRoutes + { + Rules = rules.Select(e => new AbpRule + { + Match = e.Match, + Path = RoutePatternToString(e.RoutePattern), + }).ToList(), + Default = defaultRoute, + }; + } + // Use the V1 structure for backward compatibility. + else + { + subscription.Route = defaultRoute; + } + + return subscription; + }) + .OrderBy(e => (e.PubsubName, e.Topic)) + .ToList(); + + await options?.SubscriptionsCallback(subscriptions); + await context.Response.WriteAsync(JsonSerializer.Serialize(subscriptions, + new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + })); + }); + } + + private static string RoutePatternToString(RoutePattern routePattern) + { + return string.Join("/", routePattern.PathSegments + .Select(segment => string.Concat(segment.Parts.Cast() + .Select(part => part.Content)))); + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.csproj b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.csproj index e1ede9fe69..f31ded0e06 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.csproj @@ -5,8 +5,6 @@ net7.0 - enable - enable diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs index 333a5aae9a..a291c4e769 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusModule.cs @@ -1,7 +1,14 @@ -using Microsoft.AspNetCore.Http.Json; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.SystemTextJson; +using System.Linq; +using System.Threading.Tasks; +using Dapr; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Routing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus; using Volo.Abp.EventBus.Dapr; -using Volo.Abp.Json.SystemTextJson; +using Volo.Abp.EventBus.Distributed; using Volo.Abp.Modularity; namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus; @@ -14,16 +21,50 @@ public class AbpAspNetCoreMvcDaprEventBusModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - // TODO: Add NewtonsoftJson json converter. + var subscribeOptions = context.Services.ExecutePreConfiguredActions(); - Configure(options => + Configure(options => { - options.SerializerOptions.Converters.Add(new AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter()); - }); + options.EndpointConfigureActions.Add(endpointContext => + { + var rootServiceProvider = endpointContext.ScopeServiceProvider.GetRequiredService(); + subscribeOptions.SubscriptionsCallback = subscriptions => + { + var daprEventBusOptions = rootServiceProvider.GetRequiredService>().Value; + foreach (var handler in rootServiceProvider.GetRequiredService>().Value.Handlers) + { + foreach (var @interface in handler.GetInterfaces().Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDistributedEventHandler<>))) + { + var eventType = @interface.GetGenericArguments()[0]; + var eventName = EventNameAttribute.GetNameOrDefault(eventType); - Configure(options => - { - options.JsonSerializerOptions.Converters.Add(new AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter()); + if (subscriptions.Any(x => x.PubsubName == daprEventBusOptions.PubSubName && x.Topic == eventName)) + { + // Controllers with a [Topic] attribute can replace built-in event handlers. + continue; + } + + var subscription = new AbpSubscription + { + PubsubName = daprEventBusOptions.PubSubName, + Topic = eventName, + Route = AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl, + Metadata = new AbpMetadata + { + { + AbpMetadata.RawPayload, "true" + } + } + }; + subscriptions.Add(subscription); + } + } + + return Task.CompletedTask; + }; + + endpointContext.Endpoints.MapAbpSubscribeHandler(subscribeOptions); + }); }); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusOptions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusOptions.cs deleted file mode 100644 index 02ca4c8e22..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprEventBusOptions.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus; - -public class AbpAspNetCoreMvcDaprEventBusOptions -{ - public List Contributors { get; } - - public AbpAspNetCoreMvcDaprEventBusOptions() - { - Contributors = new List(); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubConsts.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubConsts.cs index e785f2e737..5f224abc7e 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubConsts.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubConsts.cs @@ -2,7 +2,5 @@ public class AbpAspNetCoreMvcDaprPubSubConsts { - public const string DaprSubscribeUrl = "dapr/subscribe"; - public const string DaprEventCallbackUrl = "api/abp/dapr/event"; } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProvider.cs deleted file mode 100644 index e797bd2fc0..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProvider.cs +++ /dev/null @@ -1,63 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; -using Volo.Abp.DependencyInjection; -using Volo.Abp.EventBus; -using Volo.Abp.EventBus.Dapr; -using Volo.Abp.EventBus.Distributed; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus; - -public class AbpAspNetCoreMvcDaprPubSubProvider : ITransientDependency -{ - protected IServiceProvider ServiceProvider { get; } - protected AbpAspNetCoreMvcDaprEventBusOptions AspNetCoreMvcDaprEventBusOptions { get; } - protected AbpDaprEventBusOptions DaprEventBusOptions { get; } - protected AbpDistributedEventBusOptions DistributedEventBusOptions { get; } - - public AbpAspNetCoreMvcDaprPubSubProvider( - IServiceProvider serviceProvider, - IOptions aspNetCoreDaprEventBusOptions, - IOptions daprEventBusOptions, - IOptions distributedEventBusOptions) - { - ServiceProvider = serviceProvider; - AspNetCoreMvcDaprEventBusOptions = aspNetCoreDaprEventBusOptions.Value; - DaprEventBusOptions = daprEventBusOptions.Value; - DistributedEventBusOptions = distributedEventBusOptions.Value; - } - - public virtual async Task> GetSubscriptionsAsync() - { - var subscriptions = new List(); - foreach (var handler in DistributedEventBusOptions.Handlers) - { - foreach (var @interface in handler.GetInterfaces().Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDistributedEventHandler<>))) - { - var eventType = @interface.GetGenericArguments()[0]; - var eventName = EventNameAttribute.GetNameOrDefault(eventType); - - subscriptions.Add(new AbpAspNetCoreMvcDaprSubscriptionDefinition() - { - PubSubName = DaprEventBusOptions.PubSubName, - Topic = eventName, - Route = AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl - }); - } - } - - if (AspNetCoreMvcDaprEventBusOptions.Contributors.Any()) - { - using (var scope = ServiceProvider.CreateScope()) - { - var context = new AbpAspNetCoreMvcDaprPubSubProviderContributorContext(scope.ServiceProvider, subscriptions); - foreach (var contributor in AspNetCoreMvcDaprEventBusOptions.Contributors) - { - await contributor.ContributeAsync(context); - } - } - } - - return subscriptions; - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProviderContributorContext.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProviderContributorContext.cs deleted file mode 100644 index 564b541dec..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/AbpAspNetCoreMvcDaprPubSubProviderContributorContext.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus; - -public class AbpAspNetCoreMvcDaprPubSubProviderContributorContext -{ - public IServiceProvider ServiceProvider { get; } - - public List Subscriptions { get; } - - public AbpAspNetCoreMvcDaprPubSubProviderContributorContext(IServiceProvider serviceProvider, List daprSubscriptionModels) - { - ServiceProvider = serviceProvider; - Subscriptions = daprSubscriptionModels; - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprEventsController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprEventsController.cs new file mode 100644 index 0000000000..92cf41db90 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprEventsController.cs @@ -0,0 +1,38 @@ +using System; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Volo.Abp.Dapr; +using Volo.Abp.EventBus.Dapr; + +namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Controllers; + +[Area("abp")] +[RemoteService(Name = "abp")] +public class AbpAspNetCoreMvcDaprEventsController : AbpController +{ + [HttpPost(AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl)] + public virtual async Task EventAsync() + { + HttpContext.ValidateDaprAppApiToken(); + + var daprSerializer = HttpContext.RequestServices.GetRequiredService(); + var body = (await JsonDocument.ParseAsync(HttpContext.Request.Body)); + + var pubSubName = body.RootElement.GetProperty("pubsubname").GetString(); + var topic = body.RootElement.GetProperty("topic").GetString(); + var data = body.RootElement.GetProperty("data").GetRawText(); + if (pubSubName.IsNullOrWhiteSpace() || topic.IsNullOrWhiteSpace() || data.IsNullOrWhiteSpace()) + { + Logger.LogError("Invalid Dapr event request."); + return BadRequest(); + } + + var distributedEventBus = HttpContext.RequestServices.GetRequiredService(); + var eventData = daprSerializer.Deserialize(data, distributedEventBus.GetEventType(topic)); + await distributedEventBus.TriggerHandlersAsync(distributedEventBus.GetEventType(topic), eventData); + return Ok(); + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprPubSubController.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprPubSubController.cs deleted file mode 100644 index 674e227cd0..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Controllers/AbpAspNetCoreMvcDaprPubSubController.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Text.Json; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; -using Volo.Abp.Dapr; -using Volo.Abp.EventBus.Dapr; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Controllers; - -[Area("abp")] -[RemoteService(Name = "abp")] -public class AbpAspNetCoreMvcDaprPubSubController : AbpController -{ - [HttpGet(AbpAspNetCoreMvcDaprPubSubConsts.DaprSubscribeUrl)] - public virtual async Task> SubscribeAsync() - { - return await HttpContext.RequestServices.GetRequiredService().GetSubscriptionsAsync(); - } - - [HttpPost(AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl)] - public virtual async Task EventsAsync() - { - this.HttpContext.ValidateDaprAppApiToken(); - - var bodyJsonDocument = await JsonDocument.ParseAsync(HttpContext.Request.Body); - var request = JsonSerializer.Deserialize(bodyJsonDocument.RootElement.GetRawText(), - HttpContext.RequestServices.GetRequiredService>().Value.JsonSerializerOptions); - - var distributedEventBus = HttpContext.RequestServices.GetRequiredService(); - var daprSerializer = HttpContext.RequestServices.GetRequiredService(); - - var eventData = daprSerializer.Deserialize(bodyJsonDocument.RootElement.GetProperty("data").GetRawText(), distributedEventBus.GetEventType(request.Topic)); - await distributedEventBus.TriggerHandlersAsync(distributedEventBus.GetEventType(request.Topic), eventData); - - return Ok(); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/IAbpAspNetCoreMvcDaprPubSubProviderContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/IAbpAspNetCoreMvcDaprPubSubProviderContributor.cs deleted file mode 100644 index 87047cd7ec..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/IAbpAspNetCoreMvcDaprPubSubProviderContributor.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus; - -public interface IAbpAspNetCoreMvcDaprPubSubProviderContributor -{ - Task ContributeAsync(AbpAspNetCoreMvcDaprPubSubProviderContributorContext context); -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionDefinition.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionDefinition.cs deleted file mode 100644 index 287e78e01a..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionDefinition.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; - -public class AbpAspNetCoreMvcDaprSubscriptionDefinition -{ - public string PubSubName { get; set; } - - public string Topic { get; set; } - - public string Route { get; set; } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionRequest.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionRequest.cs deleted file mode 100644 index 46c04b5a44..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpAspNetCoreMvcDaprSubscriptionRequest.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; - -public class AbpAspNetCoreMvcDaprSubscriptionRequest -{ - public string PubSubName { get; set; } - - public string Topic { get; set; } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprPubSubJsonNamingPolicy.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprPubSubJsonNamingPolicy.cs deleted file mode 100644 index 0aa6bd4f7c..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprPubSubJsonNamingPolicy.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Text.Json; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.SystemTextJson; - -public class AbpAspNetCoreMvcDaprPubSubJsonNamingPolicy : JsonNamingPolicy -{ - public override string ConvertName(string name) - { - return name.ToLower(); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter.cs deleted file mode 100644 index bcd2a0e1be..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/SystemTextJson/AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.SystemTextJson; - -public class AbpAspNetCoreMvcDaprSubscriptionDefinitionConverter : JsonConverter -{ - private JsonSerializerOptions? _writeJsonSerializerOptions; - - public override AbpAspNetCoreMvcDaprSubscriptionDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - throw new NotSupportedException(); - } - - public override void Write(Utf8JsonWriter writer, AbpAspNetCoreMvcDaprSubscriptionDefinition value, JsonSerializerOptions options) - { - _writeJsonSerializerOptions ??= JsonSerializerOptionsHelper.Create(new JsonSerializerOptions(options) - { - PropertyNamingPolicy = new AbpAspNetCoreMvcDaprPubSubJsonNamingPolicy() - }, x => x == this); - - JsonSerializer.Serialize(writer, value, _writeJsonSerializerOptions); - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo.Abp.AspNetCore.Mvc.Dapr.csproj b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo.Abp.AspNetCore.Mvc.Dapr.csproj index 0c72d5acc5..03bb453ba2 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo.Abp.AspNetCore.Mvc.Dapr.csproj +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo.Abp.AspNetCore.Mvc.Dapr.csproj @@ -5,8 +5,6 @@ net7.0 - enable - enable @@ -16,7 +14,7 @@ - + diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprAppApiTokenValidator.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprAppApiTokenValidator.cs index 817eb9824d..dd0c6a5300 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprAppApiTokenValidator.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprAppApiTokenValidator.cs @@ -1,5 +1,6 @@ -using Microsoft.AspNetCore.Http; -using Microsoft.AspNetCore.SignalR; +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Authorization; using Volo.Abp.Dapr; @@ -10,13 +11,13 @@ namespace Volo.Abp.AspNetCore.Mvc.Dapr; public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDependency { protected IHttpContextAccessor HttpContextAccessor { get; } - protected HttpContext HttpContext => GetHttpContext(); + protected HttpContext HttpContext => GetHttpContext(); public DaprAppApiTokenValidator(IHttpContextAccessor httpContextAccessor) { HttpContextAccessor = httpContextAccessor; } - + public virtual void CheckDaprAppApiToken() { var expectedAppApiToken = GetConfiguredAppApiTokenOrNull(); @@ -24,7 +25,7 @@ public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDep { return; } - + var headerAppApiToken = GetDaprAppApiTokenOrNull(); if (headerAppApiToken.IsNullOrWhiteSpace()) { @@ -44,12 +45,12 @@ public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDep { return true; } - + var headerAppApiToken = GetDaprAppApiTokenOrNull(); return expectedAppApiToken == headerAppApiToken; } - public virtual string? GetDaprAppApiTokenOrNull() + public virtual string GetDaprAppApiTokenOrNull() { string apiTokenHeader = HttpContext.Request.Headers["dapr-api-token"]; if (string.IsNullOrEmpty(apiTokenHeader) || apiTokenHeader.Length < 1) @@ -59,8 +60,8 @@ public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDep return apiTokenHeader; } - - protected virtual string? GetConfiguredAppApiTokenOrNull() + + protected virtual string GetConfiguredAppApiTokenOrNull() { return HttpContext .RequestServices @@ -72,4 +73,4 @@ public class DaprAppApiTokenValidator : IDaprAppApiTokenValidator, ISingletonDep { return HttpContextAccessor.HttpContext ?? throw new AbpException("HttpContext is not available!"); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprHttpContextExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprHttpContextExtensions.cs index 15663d0c3c..aa81765b12 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprHttpContextExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/DaprHttpContextExtensions.cs @@ -1,4 +1,5 @@ -using Microsoft.AspNetCore.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Dapr; @@ -12,7 +13,7 @@ public static class DaprHttpContextExtensions .GetRequiredService() .CheckDaprAppApiToken(); } - + public static bool IsValidDaprAppApiToken(this HttpContext httpContext) { return httpContext @@ -20,12 +21,12 @@ public static class DaprHttpContextExtensions .GetRequiredService() .IsValidDaprAppApiToken(); } - - public static string? GetDaprAppApiTokenOrNull(HttpContext httpContext) + + public static string GetDaprAppApiTokenOrNull(HttpContext httpContext) { return httpContext .RequestServices .GetRequiredService() .GetDaprAppApiTokenOrNull(); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/IDaprAppApiTokenValidator.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/IDaprAppApiTokenValidator.cs index ed5f281ea8..66fd833b77 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/IDaprAppApiTokenValidator.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr/Volo/Abp/AspNetCore/Mvc/Dapr/IDaprAppApiTokenValidator.cs @@ -3,6 +3,8 @@ namespace Volo.Abp.AspNetCore.Mvc.Dapr; public interface IDaprAppApiTokenValidator { void CheckDaprAppApiToken(); + bool IsValidDaprAppApiToken(); - string? GetDaprAppApiTokenOrNull(); -} \ No newline at end of file + + string GetDaprAppApiTokenOrNull(); +} diff --git a/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj b/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj index ab079747e9..f21af371bb 100644 --- a/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj +++ b/framework/src/Volo.Abp.Dapr/Volo.Abp.Dapr.csproj @@ -5,8 +5,6 @@ net7.0 - enable - enable @@ -15,7 +13,7 @@ - + diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs index ad284ce156..aa14bbe7f2 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprClientFactory.cs @@ -1,4 +1,6 @@ -using System.Text.Json; +using System; +using System.Net.Http; +using System.Text.Json; using Dapr.Client; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; @@ -22,7 +24,7 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency JsonSerializerOptions = CreateJsonSerializerOptions(systemTextJsonSerializerOptions.Value); } - public virtual DaprClient Create(Action? builderAction = null) + public virtual DaprClient Create(Action builderAction = null) { var builder = new DaprClientBuilder() .UseJsonSerializationOptions(JsonSerializerOptions); @@ -49,9 +51,9 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency } public virtual HttpClient CreateHttpClient( - string? appId = null, - string? daprEndpoint = null, - string? daprApiToken = null) + string appId = null, + string daprEndpoint = null, + string daprApiToken = null) { if(daprEndpoint.IsNullOrWhiteSpace() && !DaprOptions.HttpEndpoint.IsNullOrWhiteSpace()) @@ -65,9 +67,9 @@ public class AbpDaprClientFactory : IAbpDaprClientFactory, ISingletonDependency daprApiToken ?? DaprApiTokenProvider.GetDaprApiToken() ); } - + protected virtual JsonSerializerOptions CreateJsonSerializerOptions(AbpSystemTextJsonSerializerOptions systemTextJsonSerializerOptions) { return new JsonSerializerOptions(systemTextJsonSerializerOptions.JsonSerializerOptions); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs index 276e96895d..4153ae2ec1 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/AbpDaprModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Configuration; +using System; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Volo.Abp.Json; @@ -12,7 +13,7 @@ public class AbpDaprModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { var configuration = context.Services.GetConfiguration(); - + ConfigureDaprOptions(configuration); context.Services.TryAddSingleton( diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/DaprApiTokenProvider.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/DaprApiTokenProvider.cs index 9fbed78204..10dd3913ed 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/DaprApiTokenProvider.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/DaprApiTokenProvider.cs @@ -11,14 +11,14 @@ public class DaprApiTokenProvider : IDaprApiTokenProvider, ISingletonDependency { Options = options.Value; } - - public virtual string? GetDaprApiToken() + + public virtual string GetDaprApiToken() { return Options.DaprApiToken; } - public virtual string? GetAppApiToken() + public virtual string GetAppApiToken() { return Options.AppApiToken; } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs index fc50a07d41..65c4e76b7e 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IAbpDaprClientFactory.cs @@ -1,14 +1,16 @@ +using System; +using System.Net.Http; using Dapr.Client; namespace Volo.Abp.Dapr; public interface IAbpDaprClientFactory { - DaprClient Create(Action? builderAction = null); + DaprClient Create(Action builderAction = null); HttpClient CreateHttpClient( - string? appId = null, - string? daprEndpoint = null, - string? daprApiToken = null + string appId = null, + string daprEndpoint = null, + string daprApiToken = null ); -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprApiTokenProvider.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprApiTokenProvider.cs index 89ea4d516d..60f2195596 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprApiTokenProvider.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprApiTokenProvider.cs @@ -2,7 +2,7 @@ namespace Volo.Abp.Dapr; public interface IDaprApiTokenProvider { - string? GetDaprApiToken(); - - string? GetAppApiToken(); -} \ No newline at end of file + string GetDaprApiToken(); + + string GetAppApiToken(); +} diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprSerializer.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprSerializer.cs index 7eec2c5c1c..9a8b4c9520 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprSerializer.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/IDaprSerializer.cs @@ -1,4 +1,6 @@ -namespace Volo.Abp.Dapr; +using System; + +namespace Volo.Abp.Dapr; public interface IDaprSerializer { @@ -6,11 +8,5 @@ public interface IDaprSerializer object Deserialize(byte[] value, Type type); - T Deserialize(byte[] value); - - string SerializeToString(object obj); - object Deserialize(string value, Type type); - - T Deserialize(string value); } diff --git a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/Utf8JsonDaprSerializer.cs b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/Utf8JsonDaprSerializer.cs index ce9b4a8523..c0924f775b 100644 --- a/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/Utf8JsonDaprSerializer.cs +++ b/framework/src/Volo.Abp.Dapr/Volo/Abp/Dapr/Utf8JsonDaprSerializer.cs @@ -1,4 +1,5 @@ -using System.Text; +using System; +using System.Text; using Volo.Abp.DependencyInjection; using Volo.Abp.Json; @@ -23,23 +24,8 @@ public class Utf8JsonDaprSerializer : IDaprSerializer, ITransientDependency return _jsonSerializer.Deserialize(type, Encoding.UTF8.GetString(value)); } - public T Deserialize(byte[] value) - { - return _jsonSerializer.Deserialize(Encoding.UTF8.GetString(value)); - } - - public string SerializeToString(object obj) - { - return _jsonSerializer.Serialize(obj); - } - public object Deserialize(string value, Type type) { return _jsonSerializer.Deserialize(type, value); } - - public T Deserialize(string value) - { - return _jsonSerializer.Deserialize(value); - } } diff --git a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo.Abp.DistributedLocking.Dapr.csproj b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo.Abp.DistributedLocking.Dapr.csproj index 4adc11cef7..79d39eedf0 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo.Abp.DistributedLocking.Dapr.csproj +++ b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo.Abp.DistributedLocking.Dapr.csproj @@ -2,11 +2,9 @@ - + net7.0 - enable - enable @@ -14,5 +12,5 @@ - + diff --git a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/AbpDistributedLockDaprOptions.cs b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/AbpDistributedLockDaprOptions.cs index 7579796871..a51ed8ede7 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/AbpDistributedLockDaprOptions.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/AbpDistributedLockDaprOptions.cs @@ -1,15 +1,17 @@ -namespace Volo.Abp.DistributedLocking.Dapr; +using System; + +namespace Volo.Abp.DistributedLocking.Dapr; public class AbpDistributedLockDaprOptions { public string StoreName { get; set; } - + public string? Owner { get; set; } - + public TimeSpan DefaultExpirationTimeout { get; set; } public AbpDistributedLockDaprOptions() { DefaultExpirationTimeout = TimeSpan.FromMinutes(2); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs index ef99b52227..974c6461a6 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLock.cs @@ -1,4 +1,7 @@ -using Microsoft.Extensions.Options; +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Options; using Volo.Abp.Dapr; using Volo.Abp.DependencyInjection; @@ -10,7 +13,7 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency protected IAbpDaprClientFactory DaprClientFactory { get; } protected AbpDistributedLockDaprOptions DistributedLockDaprOptions { get; } protected IDistributedLockKeyNormalizer DistributedLockKeyNormalizer { get; } - + public DaprAbpDistributedLock( IAbpDaprClientFactory daprClientFactory, IOptions distributedLockDaprOptions, @@ -20,8 +23,8 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency DistributedLockKeyNormalizer = distributedLockKeyNormalizer; DistributedLockDaprOptions = distributedLockDaprOptions.Value; } - - public async Task TryAcquireAsync( + + public async Task TryAcquireAsync( string name, TimeSpan timeout = default, CancellationToken cancellationToken = default) @@ -30,8 +33,8 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency var daprClient = DaprClientFactory.Create(); var lockResponse = await daprClient.Lock( - DistributedLockDaprOptions.StoreName, - name, + DistributedLockDaprOptions.StoreName, + name, DistributedLockDaprOptions.Owner ?? Guid.NewGuid().ToString(), (int)DistributedLockDaprOptions.DefaultExpirationTimeout.TotalSeconds, cancellationToken); @@ -43,4 +46,4 @@ public class DaprAbpDistributedLock : IAbpDistributedLock, ITransientDependency return new DaprAbpDistributedLockHandle(lockResponse); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLockHandle.cs b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLockHandle.cs index b10f0f3672..ccb82fc995 100644 --- a/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLockHandle.cs +++ b/framework/src/Volo.Abp.DistributedLocking.Dapr/Volo/Abp/DistributedLocking/Dapr/DaprAbpDistributedLockHandle.cs @@ -1,18 +1,19 @@ -using Dapr.Client; +using System.Threading.Tasks; +using Dapr.Client; namespace Volo.Abp.DistributedLocking.Dapr; public class DaprAbpDistributedLockHandle : IAbpDistributedLockHandle { protected TryLockResponse LockResponse { get; } - + public DaprAbpDistributedLockHandle(TryLockResponse lockResponse) { LockResponse = lockResponse; } - + public async ValueTask DisposeAsync() { await LockResponse.DisposeAsync(); } -} \ No newline at end of file +} diff --git a/framework/src/Volo.Abp.EventBus.Dapr/Volo.Abp.EventBus.Dapr.csproj b/framework/src/Volo.Abp.EventBus.Dapr/Volo.Abp.EventBus.Dapr.csproj index 95cd73dbfd..61f398fecf 100644 --- a/framework/src/Volo.Abp.EventBus.Dapr/Volo.Abp.EventBus.Dapr.csproj +++ b/framework/src/Volo.Abp.EventBus.Dapr/Volo.Abp.EventBus.Dapr.csproj @@ -2,11 +2,9 @@ - + net7.0 - enable - enable diff --git a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs index c8d47f6887..a00127ac8c 100644 --- a/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Dapr/Volo/Abp/EventBus/Dapr/DaprDistributedEventBus.cs @@ -1,4 +1,8 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Dapr; @@ -113,7 +117,7 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend GetOrCreateHandlerFactories(eventType).Locking(factories => factories.Clear()); } - protected async override Task PublishToEventBusAsync(Type eventType, object eventData) + protected override async Task PublishToEventBusAsync(Type eventType, object eventData) { await PublishToDaprAsync(eventType, eventData); } @@ -135,12 +139,12 @@ public class DaprDistributedEventBus : DistributedEventBusBase, ISingletonDepend return handlerFactoryList.ToArray(); } - public async override Task PublishFromOutboxAsync(OutgoingEventInfo outgoingEvent, OutboxConfig outboxConfig) + public override async Task PublishFromOutboxAsync(OutgoingEventInfo outgoingEvent, OutboxConfig outboxConfig) { await PublishToDaprAsync(outgoingEvent.EventName, Serializer.Deserialize(outgoingEvent.EventData, GetEventType(outgoingEvent.EventName))); } - public async override Task PublishManyFromOutboxAsync(IEnumerable outgoingEvents, OutboxConfig outboxConfig) + public override async Task PublishManyFromOutboxAsync(IEnumerable outgoingEvents, OutboxConfig outboxConfig) { var outgoingEventArray = outgoingEvents.ToArray(); diff --git a/framework/src/Volo.Abp.Http.Client.Dapr/Volo.Abp.Http.Client.Dapr.csproj b/framework/src/Volo.Abp.Http.Client.Dapr/Volo.Abp.Http.Client.Dapr.csproj index 610203dae5..0d22e68b97 100644 --- a/framework/src/Volo.Abp.Http.Client.Dapr/Volo.Abp.Http.Client.Dapr.csproj +++ b/framework/src/Volo.Abp.Http.Client.Dapr/Volo.Abp.Http.Client.Dapr.csproj @@ -2,11 +2,9 @@ - + net7.0 - enable - enable diff --git a/framework/src/Volo.Abp.Http.Client.Dapr/Volo/Abp/Http/Client/Dapr/AbpInvocationHandler.cs b/framework/src/Volo.Abp.Http.Client.Dapr/Volo/Abp/Http/Client/Dapr/AbpInvocationHandler.cs index e13c6dbb04..9ebfb5665d 100644 --- a/framework/src/Volo.Abp.Http.Client.Dapr/Volo/Abp/Http/Client/Dapr/AbpInvocationHandler.cs +++ b/framework/src/Volo.Abp.Http.Client.Dapr/Volo/Abp/Http/Client/Dapr/AbpInvocationHandler.cs @@ -1,4 +1,5 @@ -using Dapr.Client; +using System; +using Dapr.Client; using Microsoft.Extensions.Options; using Volo.Abp.Dapr; using Volo.Abp.DependencyInjection; @@ -14,4 +15,4 @@ public class AbpInvocationHandler : InvocationHandler, ITransientDependency DaprEndpoint = daprOptions.Value.HttpEndpoint; } } -} \ No newline at end of file +}