Giter VIP home page Giter VIP logo

masstransit-in-rabbimq's Introduction

MassTransit

A functioning installation of the dotnet runtime and sdk (at least 6.0).


RabbitMQ:

A high performance transport that allows both cloud based and local development.


Add Packages

Add the MassTransit and MassTransit.RabbitMQ packages to the project.

$ dotnet add package MassTransit

$ dotnet add package MassTransit.RabbitMQ


Install MassTransit Templates

MassTransit includes project and item templates simplifying the creation of new projects. Install the templates by executing dotnet new -i MassTransit.Templates at the console.

$ dotnet new --install MassTransit.Templates


Initial Project Creation

Create the worker project

To create a service using MassTransit, create a worker via the Command Prompt.

$ dotnet new mtworker -n MassTransitSample
$ cd MassTransitMassSample
$ dotnet new mtconsumer

Overview of the code

When you open the project you will see that you have 3 class files.

  • Program.cs is the standard entry point and here we configure the host builder.
  • Consumers/GettingStartedConsumer.cs is the MassTransit Consumer
  • Contracts/MassTransitSample.cs is an example messag

Add A BackgroundService

In the root of the project add Worker.cs

using MassTransit;

namespace MassTransitSample
{

    public class Worker : BackgroundService
    {
        readonly IBus _bus;

        public Worker(IBus bus)
        {
            _bus = bus;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await _bus.Publish(new Contracts.MassTransitSample { Value = $"The time is {DateTimeOffset.Now}" }, stoppingToken);
                await Task.Delay(1000, stoppingToken);
            }
        }
    }
}

Configure Services

In Program.cs at CreateHostBuilder method.

using MassTransit;
using MassTransitSample;
using Company.Consumers;

namespace MassTransitSample
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            await CreateHostBuilder(args).Build().RunAsync();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddMassTransit(x =>
                    {
                        x.AddConsumer<MassTransitSampleConsumer>();
                        x.UsingRabbitMq((context, cfg) =>
                        {

                            cfg.Host("localhost", "/", h =>
                            {
                                h.Username("admin");
                                h.Password("admin");
                            });
                            cfg.ConfigureEndpoints(context);

                        });
                    });

                     services.AddHostedService<Worker>();
                });
    }
}

localhost is where the docker image is running. We are inferring the default port of 5672 and are using \ as the virtual host (opens new window). guest and guest are the default username and password to talk to the cluster and management dashboard (opens new window).


Run the Project

$ dotnet run

masstransit-in-rabbimq's People

Contributors

mostafataheri avatar

Watchers

James Cloos avatar  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.