From 828c96b1274db069d4d6cdebfde0beb012bb5146 Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Mon, 27 Jan 2025 10:32:51 -0800 Subject: [PATCH 1/5] Including .NET sample for subscriber, fixing parameter name on declarative subscription Signed-off-by: Fernando Rocha --- .../building-blocks/pubsub/pubsub-raw.md | 54 +++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md index 6e518fa963a..84475065068 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md @@ -74,9 +74,54 @@ Dapr apps are also able to subscribe to raw events coming from existing pub/sub ### Programmatically subscribe to raw events -When subscribing programmatically, add the additional metadata entry for `rawPayload` so the Dapr sidecar automatically wraps the payloads into a CloudEvent that is compatible with current Dapr SDKs. +When subscribing programmatically, add the additional metadata entry for `rawPayload` - `isRawPayload` on .NET - so the Dapr sidecar automatically wraps the payloads into a CloudEvent that is compatible with current Dapr SDKs. -{{< tabs "Python" "PHP SDK" >}} +{{< tabs ".NET" "Python" "PHP SDK" >}} + +{{% codetab %}} + +```csharp +using System.Text.Json; +using System.Text.Json.Serialization; + + +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Subscriber API"); + +app.MapGet("/dapr/subscribe", () => +{ + var subscriptions = new[] + { + new + { + pubsubname = "pubsub", + topic = "messages", + route = "/messages", + metadata = new Dictionary + { + { "isRawPayload", "true" } + } + } + }; + return Results.Ok(subscriptions); +}); + +app.MapPost("/messages", async (HttpContext context) => +{ + using var reader = new StreamReader(context.Request.Body); + var json = await reader.ReadToEndAsync(); + + Console.WriteLine($"Raw message received: {json}"); + + return Results.Ok(); +}); + +app.Run(); +``` + +{{% /codetab %}} {{% codetab %}} @@ -151,7 +196,7 @@ spec: default: /dsstatus pubsubname: pubsub metadata: - rawPayload: "true" + isRawPayload: "true" scopes: - app1 - app2 @@ -161,4 +206,5 @@ scopes: - Learn more about [publishing and subscribing messages]({{< ref pubsub-overview.md >}}) - List of [pub/sub components]({{< ref supported-pubsub >}}) -- Read the [API reference]({{< ref pubsub_api.md >}}) \ No newline at end of file +- Read the [API reference]({{< ref pubsub_api.md >}}) +- Read the .NET sample on how to [consume Kafka messages without CloudEvents](https://github.com/dapr/samples/pubsub-raw-payload) \ No newline at end of file From bd9eb23110f43db5fa7f4304b0e4e945dd67f1dd Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Thu, 30 Jan 2025 18:13:44 -0800 Subject: [PATCH 2/5] including .NET publisher example Signed-off-by: Fernando Rocha --- .../building-blocks/pubsub/pubsub-raw.md | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md index 84475065068..6431b97cc5d 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md @@ -20,7 +20,7 @@ Not using CloudEvents disables support for tracing, event deduplication per mess To disable CloudEvent wrapping, set the `rawPayload` metadata to `true` as part of the publishing request. This allows subscribers to receive these messages without having to parse the CloudEvent schema. -{{< tabs curl "Python SDK" "PHP SDK">}} +{{< tabs curl ".NET" "Python SDK" "PHP SDK">}} {{% codetab %}} ```bash @@ -28,6 +28,46 @@ curl -X "POST" http://localhost:3500/v1.0/publish/pubsub/TOPIC_A?metadata.rawPay ``` {{% /codetab %}} +{{% codetab %}} + +```csharp +using Dapr.Client; +using Shared; + +var builder = WebApplication.CreateBuilder(args); +builder.Services.AddControllers().AddDapr(); + +var app = builder.Build(); + +app.MapGet("/", () => "Publisher API"); + +app.MapPost("/publish", async (DaprClient daprClient) => +{ + var message = new Message( + Guid.NewGuid().ToString(), + $"Hello at {DateTime.UtcNow}", + DateTime.UtcNow + ); + + await daprClient.PublishEventAsync( + "pubsub", // pubsub name + "messages", // topic name + message, // message data + new Dictionary + { + { "rawPayload", "true" }, + { "content-type", "application/json" } + } + ); + + return Results.Ok(message); +}); + +app.Run(); +``` + +{{% /codetab %}} + {{% codetab %}} ```python from dapr.clients import DaprClient From f6bd09836513208e7752065b8c835e68088c64c2 Mon Sep 17 00:00:00 2001 From: Mark Fussell Date: Tue, 4 Feb 2025 11:54:13 -0800 Subject: [PATCH 3/5] Update daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md Co-authored-by: Marc Duiker Signed-off-by: Mark Fussell --- .../building-blocks/pubsub/pubsub-raw.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md index 6431b97cc5d..806ae2cee0f 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md @@ -20,7 +20,7 @@ Not using CloudEvents disables support for tracing, event deduplication per mess To disable CloudEvent wrapping, set the `rawPayload` metadata to `true` as part of the publishing request. This allows subscribers to receive these messages without having to parse the CloudEvent schema. -{{< tabs curl ".NET" "Python SDK" "PHP SDK">}} +{{< tabs curl ".NET" "Python" "PHP">}} {{% codetab %}} ```bash From e98d40fd23807b71169d9588f662b17856308fd4 Mon Sep 17 00:00:00 2001 From: Mark Fussell Date: Tue, 4 Feb 2025 11:57:17 -0800 Subject: [PATCH 4/5] Update daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md Co-authored-by: Marc Duiker Signed-off-by: Mark Fussell --- .../building-blocks/pubsub/pubsub-raw.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md index 806ae2cee0f..9c9b3da4ae3 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md @@ -116,7 +116,7 @@ Dapr apps are also able to subscribe to raw events coming from existing pub/sub When subscribing programmatically, add the additional metadata entry for `rawPayload` - `isRawPayload` on .NET - so the Dapr sidecar automatically wraps the payloads into a CloudEvent that is compatible with current Dapr SDKs. -{{< tabs ".NET" "Python" "PHP SDK" >}} +{{< tabs ".NET" "Python" "PHP" >}} {{% codetab %}} From 9ad76c3c058f58725e0936546823305f8caaf8b6 Mon Sep 17 00:00:00 2001 From: Fernando Rocha Date: Wed, 5 Feb 2025 12:31:19 -0800 Subject: [PATCH 5/5] removing unecessary lines of code and moddifying verbiage around raw message subscriptions Signed-off-by: Fernando Rocha --- .../building-blocks/pubsub/pubsub-raw.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md index 9c9b3da4ae3..5b4fe2c1b2b 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md +++ b/daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-raw.md @@ -32,15 +32,12 @@ curl -X "POST" http://localhost:3500/v1.0/publish/pubsub/TOPIC_A?metadata.rawPay ```csharp using Dapr.Client; -using Shared; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers().AddDapr(); var app = builder.Build(); -app.MapGet("/", () => "Publisher API"); - app.MapPost("/publish", async (DaprClient daprClient) => { var message = new Message( @@ -114,7 +111,7 @@ Dapr apps are also able to subscribe to raw events coming from existing pub/sub ### Programmatically subscribe to raw events -When subscribing programmatically, add the additional metadata entry for `rawPayload` - `isRawPayload` on .NET - so the Dapr sidecar automatically wraps the payloads into a CloudEvent that is compatible with current Dapr SDKs. +When subscribing programmatically, add the additional metadata entry for `rawPayload` to allow the subscriber to receive a message that is not wrapped by a CloudEvent. For .NET, this metadata entry is called `isRawPayload`. {{< tabs ".NET" "Python" "PHP" >}} @@ -124,12 +121,9 @@ When subscribing programmatically, add the additional metadata entry for `rawPay using System.Text.Json; using System.Text.Json.Serialization; - var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); -app.MapGet("/", () => "Subscriber API"); - app.MapGet("/dapr/subscribe", () => { var subscriptions = new[] @@ -141,7 +135,8 @@ app.MapGet("/dapr/subscribe", () => route = "/messages", metadata = new Dictionary { - { "isRawPayload", "true" } + { "isRawPayload", "true" }, + { "content-type", "application/json" } } } };