<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ashwin Gatadi</title>
    <description>The latest articles on DEV Community by Ashwin Gatadi (@ashwingatadi).</description>
    <link>https://dev.to/ashwingatadi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1198541%2F4b07b13f-a936-4769-b94b-aa7495262874.jpeg</url>
      <title>DEV Community: Ashwin Gatadi</title>
      <link>https://dev.to/ashwingatadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashwingatadi"/>
    <language>en</language>
    <item>
      <title>Dapr - Publish/Subscibe - Using Web APIs</title>
      <dc:creator>Ashwin Gatadi</dc:creator>
      <pubDate>Thu, 02 Nov 2023 19:10:39 +0000</pubDate>
      <link>https://dev.to/ashwingatadi/dapr-publishsubscibe-using-web-apis-1p3j</link>
      <guid>https://dev.to/ashwingatadi/dapr-publishsubscibe-using-web-apis-1p3j</guid>
      <description>&lt;p&gt;&lt;strong&gt;&lt;u&gt;Pre-requisites&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Dapr CLI &lt;/li&gt;
&lt;li&gt;.NET 6 SDK installed.&lt;/li&gt;
&lt;li&gt;Docker Desktop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Create a Web API Publish Service&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a ASP.Net Core Web API project with .Net 6.0 framework version. Say name of the project as &lt;em&gt;publisher&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Add nuget package &lt;em&gt;Dapr.AspNetCore&lt;/em&gt; (I have used version 1.12.0)&lt;/li&gt;
&lt;li&gt;Add a directory say &lt;em&gt;dapr-components&lt;/em&gt; into your project. Add file &lt;em&gt;pubsub.yaml&lt;/em&gt; into the folder, which should look as below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: myapp-pubsub
spec:
  type: pubsub.redis
  version: v1
  metadata:
    - name: redisHost
      value: localhost:6379
    - name: redisPassword
      value: ""
scopes:
  - publisher
  - subscriber
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Add below line of code into your &lt;em&gt;program.cs&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddDaprClient();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your &lt;em&gt;program.cs&lt;/em&gt; should look as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDaprClient();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a WebAPI controller, say it as &lt;em&gt;PublishController.cs&lt;/em&gt;. The code looks as below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System;

namespace Publisher.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PublishController : ControllerBase
    {
        private readonly DaprClient daprClient;
        private readonly ILogger&amp;lt;PublishController&amp;gt; logger;

        public PublishController(DaprClient daprClient, ILogger&amp;lt;PublishController&amp;gt; logger) 
        { 
            this.daprClient = daprClient;
            this.logger = logger;
        }

        [HttpPost]
        public async Task Post([FromBody] string value)
        {
            await daprClient.PublishEventAsync("myapp-pubsub", "test-dapr-topic", $"{DateTime.Now} - {value}");
            logger.LogInformation($"Published data at {DateTime.Now} - {value}");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Create a Web API Subscribe Service&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a ASP.Net Core Web API project with .Net 6.0 framework version. Say name of the project as &lt;em&gt;subscriber&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Add nuget package &lt;em&gt;Dapr.AspNetCore&lt;/em&gt; (I have used version 1.12.0)&lt;/li&gt;
&lt;li&gt;Add a directory say &lt;em&gt;dapr-components&lt;/em&gt; into your project. Add file &lt;em&gt;pubsub.yaml&lt;/em&gt; into the folder, which should look as below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: myapp-pubsub
spec:
  type: pubsub.redis
  version: v1
  metadata:
    - name: redisHost
      value: localhost:6379
    - name: redisPassword
      value: ""
scopes:
  - publisher
  - subscriber
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Add below two lines of code into your &lt;em&gt;program.cs&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* The call to UseCloudEvents adds CloudEvents middleware into to the 
 * ASP.NET Core middleware pipeline. 
 * This middleware will unwrap requests that use the CloudEvents structured 
 * format, so the receiving method can read the event payload directly. */
app.UseCloudEvents();

/* The call to MapSubscribeHandler in the endpoint routing configuration 
 * will add a Dapr subscribe endpoint to the application.
 * 
 * This endpoint will respond to requests on /dapr/subscribe.
 * When this endpoint is called, it will automatically find all WebAPI
 * action methods decorated with the Topic attribute and instruct
 * Dapr to create subscriptions for them. */
app.MapSubscribeHandler();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;your &lt;em&gt;program.cs&lt;/em&gt; should look as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCloudEvents();
app.MapSubscribeHandler();
app.MapControllers();
app.Run();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a WebAPI controller, say it as &lt;em&gt;SubscriberController.cs&lt;/em&gt;. The code looks as below
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using Dapr;
using Microsoft.AspNetCore.Mvc;

namespace Subscriber.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SubscriberController : ControllerBase
    {
        ILogger&amp;lt;SubscriberController&amp;gt; logger;

        public SubscriberController(ILogger&amp;lt;SubscriberController&amp;gt; logger)
        {
            this.logger = logger;
        }

        [Topic("myapp-pubsub", "test-dapr-topic")]
        [HttpPost("subscriber")]
        public IActionResult Post([FromBody] string value)
        {
            logger.LogInformation(value);
            return Ok(value);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Testing our web api's publish subscribe&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open docker desktop&lt;/li&gt;
&lt;li&gt;Use below command to run publisher&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;dapr run --app-id publisher --app-port &amp;lt;port used in launch settings for https&amp;gt; --app-protocol https --dapr-http-port 3608 --resources-path dapr-components -- dotnet run&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use below command to run subscriber&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;dapr run --app-id subscriber --app-port &amp;lt;port used in launch settings for https&amp;gt; --app-protocol https --dapr-http-port 3610 --resources-path dapr-components -- dotnet run&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Test using postman. Hit publisher endpoint. You can see in the logs that publisher published message and subscriber received message&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4so2zlev4gra283xip7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4so2zlev4gra283xip7.png" alt="Image that tested pubsub of dapr"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Source code&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/ashwingatadi/dapr-publisher" rel="noopener noreferrer"&gt;dapr-publisher&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ashwingatadi/dapr-subscriber" rel="noopener noreferrer"&gt;dapr-subscriber&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>dapr</category>
      <category>pubsub</category>
      <category>dotnetcore</category>
    </item>
  </channel>
</rss>
