Skip to content

Commit

Permalink
Including .NET sample for subscriber, fixing parameter name on declar…
Browse files Browse the repository at this point in the history
…ative subscription

Signed-off-by: Fernando Rocha <fernando@diagrid.io>
  • Loading branch information
rochabr committed Jan 27, 2025
1 parent efcd9fb commit 828c96b
Showing 1 changed file with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
{
{ "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 %}}

Expand Down Expand Up @@ -151,7 +196,7 @@ spec:
default: /dsstatus
pubsubname: pubsub
metadata:
rawPayload: "true"
isRawPayload: "true"
scopes:
- app1
- app2
Expand All @@ -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 >}})
- 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)

0 comments on commit 828c96b

Please sign in to comment.