Giter Club home page Giter Club logo

dapr-presentation's Introduction

Creating new Dapr subscriber

Create sample project

mkdir dapr-subscriber
cd dapr-subscriber
dotnet new webapi  
dotnet add package Dapr.AspNetCore
mkdir components

Add redis pub/sub component

Add components/pubsub.yaml

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: pubsub
spec:
  type: pubsub.redis
  version: v1
  metadata:
  - name: redisHost
    value: localhost:6379
  - name: redisPassword
    value: ""

Configure Dapr

Configure Dapr in startup

Add support the requests with Content-Type application/cloudevents+json in Startup.cs

app.UseCloudEvents();

Add Dapr subscribe handler in Startup.cs

endpoints.MapSubscribeHandler();

Remove https redirection

Sample Controller

Add sample controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapr;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace dapr_subscriber.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class SampleController : ControllerBase
    {

        private readonly ILogger<SampleController> _logger;

        public SampleController(ILogger<SampleController> logger)
        {
            _logger = logger;
        }

        [HttpPost]
        [Topic("pubsub", "hello-world")]
        public ActionResult HelloWorld()
        {
            _logger.LogWarning("Hello world");
            return Ok();
        }
    }
}

Run the app

Run the application

dapr run --app-id sample --app-port 5000 --components-path ./components -- dotnet run

Publish to Sample Controller

Publish a message to the application

dapr publish --pubsub pubsub --publish-app-id sample -t hello-world

dapr-presentation's People

Contributors

mlatoszek avatar

Watchers

 avatar

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.