Dapr component

SriRaghavendran Prabhakaran 300 Reputation points
2023-11-13T11:34:15.1966667+00:00

Hello,

I am using dapr components to publish and subscribe events between services of single connection string (event hub). is that possible to have different connection string (eventhubs) in the same dapr ? so that instead of creating multiple container services based on connection string, i can use same dapr component.

Azure Container Instances
Azure Container Instances
An Azure service that provides customers with a serverless container experience.
687 questions
{count} votes

Accepted answer
  1. vipullag-MSFT 26,211 Reputation points
    2023-11-15T05:57:03.3966667+00:00

    Hello SriRaghavendran Prabhakaran

    To configure Dapr in Azure Container Apps, you can create a dapr.yaml file and include it in your container image. Here's an example of a dapr.yaml file that configures two Event Hubs components:

    apiVersion: dapr.io/v1alpha1
    kind: Configuration
    metadata:
      name: pubsub-config
    spec:
      pubsub:
        - name: eventhub1
          pubsubType: azure.eventhubs
          metadata:
          - name: connectionString
            value: 
          - name: consumerID
            value: 
        - name: eventhub2
          pubsubType: azure.eventhubs
          metadata:
          - name: connectionString
            value: 
          - name: consumerID
            value: 
    

    You can then reference the pubsub name in your .NET Core web API's by using the Dapr SDK. Here's an example of how you can publish an event to a specific topic using the Dapr SDK:

    using Dapr.Client;
    
    public class MyController : ControllerBase
    {
        private readonly DaprClient daprClient;
    
        public MyController(DaprClient daprClient)
        {
            this.daprClient = daprClient;
        }
    
        [HttpPost("publish/{pubsubName}/{topic}")]
        public async Task PublishEvent(string pubsubName, string topic, [FromBody] object data)
        {
            await daprClient.PublishEventAsync(pubsubName, topic, data);
            return Ok();
        }
    }
    

    In this example, we're using the DaprClient to publish an event to a specific topic. The pubsubName parameter specifies the name of the Event Hubs component, and the topic parameter specifies the name of the topic to publish to.

    Similarly, you can use the DaprClient to subscribe to events from a specific topic.

    Hope this helps.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.