Coder Social home page Coder Social logo

Comments (3)

olitomlinson avatar olitomlinson commented on June 3, 2024

@JasonRodman

Given your example here :

var createdEvent = new SessionCreated();
new CloudEvent<SessionCreated>(createdEvent) { Type = "session-created" };

metadata = new Dictionary<string, string);
metadata.Add("cloudevent.id", createdEvent.Id);

await client.PublishEventAsync("messagebus", "Session", cloudEvent, metadata);

You could create your own CloudEvent (based on this ) and add an Id field, then you could do something like this :

new CloudEvent<SessionCreated>(createdEvent) { Type = "session-created", Id = createdEvent.Id };

My understanding (which could be wrong) is that when you specify a CloudEvent object, dapr will treat the model as-is, and won't attempt to interfere/augment it with other CE fields, (due to the content being set like this) So I think your metadata overrides are being ignored in this instance.


FWIW I can successfully override ce properties with this code, using redis :

request is just a normal serializable POCO (not a CloudEvent)

 
    var metadata = new Dictionary<string, string>();
    metadata.Add("cloudevent.id", "123");
    metadata.Add("cloudevent.type", "123");
    metadata.Add("cloudevent.source", "123");

    await daprClient.PublishEventAsync<StartWorkflowRequest>("kafka-pubsub", "workflowTopic", request, metadata, cts.Token);
      

this results in the following message body :

{
    "data": {
        ...
    },
    "datacontenttype": "application/json",
    "id": "123",
    "pubsubname": "redis-pubsub",
    "source": "123",
    "specversion": "1.0",
    "time": "2023-10-09T20:28:49Z",
    "topic": "workflowTopic",
    "traceid": "00-1c99e29b6f293df1836e945a1af06ffb-f4e20d12e31faa67-00",
    "traceparent": "00-1c99e29b6f293df1836e945a1af06ffb-f4e20d12e31faa67-00",
    "tracestate": "",
    "type": "123"
}

from dotnet-sdk.

olitomlinson avatar olitomlinson commented on June 3, 2024

@JasonRodman

A better answer is just to derive a new class which extends CloudEvent. Add any properties you require, CE or otherwise.

I.e. to include the ce field id you can just do this :

public class CloudEvent2<TData> : Dapr.CloudEvent<TData>
{
    public CloudEvent2(TData data) : base(data)
    {
        
    }

    [JsonPropertyName("id")]
    public string Id { get; init; }
}
var ce = new CloudEvent2<StartWorkflowRequest>(request) { 
    Id = "wf-" + Guid.NewGuid().ToString(),
    Source = new Uri("/cloudevents/spec/pull/123"), 
    Type = "my-type" };
    
await daprClient.PublishEventAsync<CloudEvent2<StartWorkflowRequest>>("redis-pubsub", "workflowTopic", ce, cts.Token);

which generates the following message body in pubsub

{
  "data": {
    "failOnTimeout": false,
    "id": "0-30"
  },
  "datacontenttype": "application/json",
  "id": "wf-6d632d55-5ddd-4c8e-be01-fb3c98e7abc5",
  "pubsubname": "redis-pubsub",
  "source": "/cloudevents/spec/pull/123",
  "specversion": "1.0",
  "subject": null,
  "time": "2023-10-11T12:01:58Z",
  "topic": "workflowTopic",
  "traceid": "00-d38ec30dbe1a3e7ad13430344cf62416-7996fe9086ffa58d-00",
  "traceparent": "00-d38ec30dbe1a3e7ad13430344cf62416-7996fe9086ffa58d-00",
  "tracestate": "",
  "type": "my-type"
}

from dotnet-sdk.

JasonRodman avatar JasonRodman commented on June 3, 2024

I will give that a try. So the crux of the problem is when you pass a CloudEvent or derived class to PublishEventAsync the metadata you pass it will be ignored. This behavior should probably be documented somewhere. The documentation leads me to think otherwise the way it currently reads.

from dotnet-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.