diff --git a/docs/en/Dapr/Index.md b/docs/en/Dapr/Index.md index 20abaa3f31..5904b85508 100644 --- a/docs/en/Dapr/Index.md +++ b/docs/en/Dapr/Index.md @@ -281,7 +281,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync([FromBody] AbpDaprSubscriptionRequest model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { HttpContext.ValidateDaprAppApiToken(); @@ -411,7 +411,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync([FromBody] AbpDaprSubscriptionRequest 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 efeb6ca770..c4306f2c9f 100644 --- a/docs/zh-Hans/Dapr/Index.md +++ b/docs/zh-Hans/Dapr/Index.md @@ -281,7 +281,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync([FromBody] AbpDaprSubscriptionRequest model) + public async Task TestRouteAsync([FromBody] StockCountChangedEto model) { HttpContext.ValidateDaprAppApiToken(); @@ -411,7 +411,7 @@ public class MyController : AbpController { [HttpPost("/stock-changed")] [Topic("pubsub", "StockChanged")] - public async Task TestRouteAsync([FromBody] AbpDaprSubscriptionRequest 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/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 ef5594f2c5..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,14 +1,10 @@ -using System; -using System.Linq; +using System.Linq; using System.Threading.Tasks; using Dapr; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Json; -using Volo.Abp.Dapr; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus; using Volo.Abp.EventBus.Dapr; @@ -25,12 +21,6 @@ public class AbpAspNetCoreMvcDaprEventBusModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { - context.Services.AddOptions() - .Configure((options, serviceProvider) => - { - options.JsonSerializerOptions.Converters.Add(new AbpDaprSubscriptionRequestConverterFactory(serviceProvider.GetRequiredService())); - }); - var subscribeOptions = context.Services.ExecutePreConfiguredActions(); Configure(options => @@ -54,12 +44,19 @@ public class AbpAspNetCoreMvcDaprEventBusModule : AbpModule continue; } - subscriptions.Add(new AbpSubscription() + var subscription = new AbpSubscription { PubsubName = daprEventBusOptions.PubSubName, Topic = eventName, - Route = AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl - }); + Route = AbpAspNetCoreMvcDaprPubSubConsts.DaprEventCallbackUrl, + Metadata = new AbpMetadata + { + { + AbpMetadata.RawPayload, "true" + } + } + }; + subscriptions.Add(subscription); } } 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 index cb4461dd9c..92cf41db90 100644 --- 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 @@ -1,11 +1,9 @@ -using System.Collections.Concurrent; +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.AspNetCore.Mvc.Dapr.EventBus.Json; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; using Volo.Abp.Dapr; using Volo.Abp.EventBus.Dapr; @@ -21,31 +19,20 @@ public class AbpAspNetCoreMvcDaprEventsController : AbpController HttpContext.ValidateDaprAppApiToken(); var daprSerializer = HttpContext.RequestServices.GetRequiredService(); - var request = (await JsonDocument.ParseAsync(HttpContext.Request.Body)).Deserialize>(CreateJsonSerializerOptions(daprSerializer)); - if (request != null && request.Data is JsonElement jsonElement) + 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()) { - var distributedEventBus = HttpContext.RequestServices.GetRequiredService(); - var eventData = daprSerializer.Deserialize(jsonElement.GetRawText(), distributedEventBus.GetEventType(request.Topic)); - await distributedEventBus.TriggerHandlersAsync(distributedEventBus.GetEventType(request.Topic), eventData); - return Ok(); + Logger.LogError("Invalid Dapr event request."); + return BadRequest(); } - Logger.LogError("Invalid Dapr event request."); - return BadRequest(); - } - - private static readonly ConcurrentDictionary JsonSerializerOptionsCache = new ConcurrentDictionary(); - - protected virtual JsonSerializerOptions CreateJsonSerializerOptions(IDaprSerializer daprSerializer) - { - return JsonSerializerOptionsCache.GetOrAdd(nameof(AbpAspNetCoreMvcDaprEventsController), _ => - { - var settings = new JsonSerializerOptions(JsonSerializerDefaults.Web) - { - PropertyNamingPolicy = new AbpDaprSubscriptionRequestJsonNamingPolicy() - }; - settings.Converters.Add(new AbpDaprSubscriptionRequestConverterFactory(daprSerializer)); - return settings; - }); + 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/Json/AbpDaprSubscriptionRequestConverter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestConverter.cs deleted file mode 100644 index b0a4f72b12..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestConverter.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; -using Volo.Abp.Dapr; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Json; - -public class AbpDaprSubscriptionRequestConverter : JsonConverter> - where T : class -{ - private JsonSerializerOptions _readJsonSerializerOptions; - - private readonly IDaprSerializer _daprSerializer; - - public AbpDaprSubscriptionRequestConverter(IDaprSerializer daprSerializer) - { - _daprSerializer = daprSerializer; - } - - public override AbpDaprSubscriptionRequest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - _readJsonSerializerOptions ??= CreateJsonSerializerOptions(options); - - var rootElement = JsonDocument.ParseValue(ref reader).RootElement; - var obj = JsonSerializer.Deserialize>(rootElement.GetRawText(), _readJsonSerializerOptions); - obj.Data = _daprSerializer.Deserialize(rootElement.GetProperty("data").GetRawText(), typeof(T)).As(); - return obj; - } - - public override void Write(Utf8JsonWriter writer, AbpDaprSubscriptionRequest value, JsonSerializerOptions options) - { - throw new NotSupportedException(); - } - - private JsonSerializerOptions CreateJsonSerializerOptions(JsonSerializerOptions options) - { - var newOptions = new JsonSerializerOptions(options); - newOptions.Converters.RemoveAll(x => x == this || x.GetType() == typeof(AbpDaprSubscriptionRequestConverterFactory)); - newOptions.PropertyNamingPolicy = new AbpDaprSubscriptionRequestJsonNamingPolicy(); - return newOptions; - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestConverterFactory.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestConverterFactory.cs deleted file mode 100644 index ec809c23d2..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestConverterFactory.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Reflection; -using System.Text.Json; -using System.Text.Json.Serialization; -using Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; -using Volo.Abp.Dapr; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Json; - -public class AbpDaprSubscriptionRequestConverterFactory : JsonConverterFactory -{ - private readonly IDaprSerializer _daprSerializer; - - public AbpDaprSubscriptionRequestConverterFactory(IDaprSerializer daprSerializer) - { - _daprSerializer = daprSerializer; - } - - public override bool CanConvert(Type typeToConvert) - { - return typeToConvert.GetGenericTypeDefinition() == typeof(AbpDaprSubscriptionRequest<>); - } - - public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) - { - return (JsonConverter)Activator.CreateInstance( - typeof(AbpDaprSubscriptionRequestConverter<>).MakeGenericType(typeToConvert.GetGenericArguments()[0]), - BindingFlags.Instance | BindingFlags.Public, - binder: null, - new object[] { _daprSerializer }, - culture: null)!; - } -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestJsonNamingPolicy.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestJsonNamingPolicy.cs deleted file mode 100644 index 89437f5fa3..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Json/AbpDaprSubscriptionRequestJsonNamingPolicy.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Text.Json; - -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Json; - -public class AbpDaprSubscriptionRequestJsonNamingPolicy : 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/Models/AbpDaprSubscriptionRequest.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpDaprSubscriptionRequest.cs deleted file mode 100644 index ccf3724579..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.Dapr.EventBus/Volo/Abp/AspNetCore/Mvc/Dapr/EventBus/Models/AbpDaprSubscriptionRequest.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Volo.Abp.AspNetCore.Mvc.Dapr.EventBus.Models; - -public class AbpDaprSubscriptionRequest - where T : class -{ - public string PubSubName { get; set; } - - public string Topic { get; set; } - - public T Data { get; set; } -}