|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title> | AzureServiceBusFlow </title> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <meta name="title" content=" | AzureServiceBusFlow "> |
| 8 | + |
| 9 | + |
| 10 | + <link rel="icon" href="../favicon.ico"> |
| 11 | + <link rel="stylesheet" href="../public/docfx.min.css"> |
| 12 | + <link rel="stylesheet" href="../public/main.css"> |
| 13 | + <meta name="docfx:navrel" content="../toc.html"> |
| 14 | + <meta name="docfx:tocrel" content="toc.html"> |
| 15 | + |
| 16 | + <meta name="docfx:rel" content="../"> |
| 17 | + |
| 18 | + |
| 19 | + <meta name="docfx:docurl" content="https://github.com/migueltotti/AzureServiceBusFlow/blob/documentation/docs/docs/creatingConsumers.md/#L1"> |
| 20 | + <meta name="loc:inThisArticle" content="In this article"> |
| 21 | + <meta name="loc:searchResultsCount" content="{count} results for "{query}""> |
| 22 | + <meta name="loc:searchNoResults" content="No results for "{query}""> |
| 23 | + <meta name="loc:tocFilter" content="Filter by title"> |
| 24 | + <meta name="loc:nextArticle" content="Next"> |
| 25 | + <meta name="loc:prevArticle" content="Previous"> |
| 26 | + <meta name="loc:themeLight" content="Light"> |
| 27 | + <meta name="loc:themeDark" content="Dark"> |
| 28 | + <meta name="loc:themeAuto" content="Auto"> |
| 29 | + <meta name="loc:changeTheme" content="Change theme"> |
| 30 | + <meta name="loc:copy" content="Copy"> |
| 31 | + <meta name="loc:downloadPdf" content="Download PDF"> |
| 32 | + |
| 33 | + <script type="module" src="./../public/docfx.min.js"></script> |
| 34 | + |
| 35 | + <script> |
| 36 | + const theme = localStorage.getItem('theme') || 'auto' |
| 37 | + document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme) |
| 38 | + </script> |
| 39 | + |
| 40 | + </head> |
| 41 | + |
| 42 | + <body class="tex2jax_ignore" data-layout="" data-yaml-mime=""> |
| 43 | + <header class="bg-body border-bottom"> |
| 44 | + <nav id="autocollapse" class="navbar navbar-expand-md" role="navigation"> |
| 45 | + <div class="container-xxl flex-nowrap"> |
| 46 | + <a class="navbar-brand" href="../index.html"> |
| 47 | + <img id="logo" class="svg" src="../logo.svg" alt="AzureServiceBusFlow"> |
| 48 | + AzureServiceBusFlow |
| 49 | + </a> |
| 50 | + <button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation"> |
| 51 | + <i class="bi bi-three-dots"></i> |
| 52 | + </button> |
| 53 | + <div class="collapse navbar-collapse" id="navpanel"> |
| 54 | + <div id="navbar"> |
| 55 | + <form class="search" role="search" id="search"> |
| 56 | + <i class="bi bi-search"></i> |
| 57 | + <input class="form-control" id="search-query" type="search" disabled placeholder="Search" autocomplete="off" aria-label="Search"> |
| 58 | + </form> |
| 59 | + </div> |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + </nav> |
| 63 | + </header> |
| 64 | + |
| 65 | + <main class="container-xxl"> |
| 66 | + <div class="toc-offcanvas"> |
| 67 | + <div class="offcanvas-md offcanvas-start" tabindex="-1" id="tocOffcanvas" aria-labelledby="tocOffcanvasLabel"> |
| 68 | + <div class="offcanvas-header"> |
| 69 | + <h5 class="offcanvas-title" id="tocOffcanvasLabel">Table of Contents</h5> |
| 70 | + <button type="button" class="btn-close" data-bs-dismiss="offcanvas" data-bs-target="#tocOffcanvas" aria-label="Close"></button> |
| 71 | + </div> |
| 72 | + <div class="offcanvas-body"> |
| 73 | + <nav class="toc" id="toc"></nav> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + |
| 78 | + <div class="content"> |
| 79 | + <div class="actionbar"> |
| 80 | + <button class="btn btn-lg border-0 d-md-none" type="button" data-bs-toggle="offcanvas" data-bs-target="#tocOffcanvas" aria-controls="tocOffcanvas" aria-expanded="false" aria-label="Show table of contents"> |
| 81 | + <i class="bi bi-list"></i> |
| 82 | + </button> |
| 83 | + |
| 84 | + <nav id="breadcrumb"></nav> |
| 85 | + </div> |
| 86 | + |
| 87 | + <article data-uid=""> |
| 88 | + |
| 89 | +<h2 id="consumers">Consumers</h2> |
| 90 | +<p><strong>AzureServiceBusFlow</strong> also simplify the creation of <strong>Consumers</strong> or <strong>MessageHandlers</strong> by implementing the <strong><code>IMessageHandler<></code></strong> interface and passing the <strong>Message</strong> that will be consumed as the Type of the <strong>Handler</strong>.</p> |
| 91 | +<blockquote> |
| 92 | +<p>This interface provides the <code>HandleAsync()</code> method that is used to process the received message.</p> |
| 93 | +</blockquote> |
| 94 | +<hr> |
| 95 | +<h3 id="-creating-a-consumer">🛠️ Creating a Consumer</h3> |
| 96 | +<pre><code class="lang-csharp">public class CommandExample1Handler : IMessageHandler<ExampleCommand1> |
| 97 | +{ |
| 98 | + public Task HandleAsync(ExampleCommand1 message, ServiceBusReceivedMessage rawMessage, CancellationToken cancellationToken) |
| 99 | + { |
| 100 | + return Task.CompletedTask; |
| 101 | + } |
| 102 | +} |
| 103 | +</code></pre> |
| 104 | +<p>The MessageHandler above consume a Message of type ExampleCommand1, created earlier in this documentation (<a href="/docs/creatingMessages.html">ExampleCommand1</a>).</p> |
| 105 | +<hr> |
| 106 | +<h3 id="-registering-consumer">⚙️ Registering Consumer</h3> |
| 107 | +<p>To register the <strong>Consumer</strong>, aka <strong>MessageHandler</strong>, we need to use the <strong><code>AddConsumer()</code></strong> extension method in <strong><code>AddAzureServiceBus()</code></strong> configuration methon in <strong><code>Program.cs</code></strong>.</p> |
| 108 | +<blockquote> |
| 109 | +<p>the <strong><code>AddConsummer()</code></strong> method requires a Action of type ServiceBusConsumerConfigurationBuilder to configure the Consumer using this methods:</p> |
| 110 | +<ul> |
| 111 | +<li><code>FromQueue()</code>: Defines the Queue this Consumer will be receiving messages.</li> |
| 112 | +<li><code>FromTopic()</code>: Defines the Topic this Consumer will be receiving messages.</li> |
| 113 | +<li><code>EnsureSubscriptionExists()</code>: Ensure that the subscription passed exists in AzureServiceBus, creates it if doesn`t exists.</li> |
| 114 | +<li><code>AddHandler<TMessage, TMessageHandler>()</code>: Defines the <strong>Consumer</strong> / <strong>MessageHandler</strong> that is goint to consume a specific Message from a Queue or a Topic. More than one Consumer to the same <strong>Message</strong>, <strong>Queue</strong> or <strong>Topic</strong> can be defined in the same <code>AddConsumer()</code></li> |
| 115 | +</ul> |
| 116 | +</blockquote> |
| 117 | +<p>This example shows the full configuration for:</p> |
| 118 | +<ul> |
| 119 | +<li>Registering a <strong>Message</strong></li> |
| 120 | +<li>Creating a <strong>Producer</strong> for it</li> |
| 121 | +<li>Publishing in a specifi <strong>Queue</strong></li> |
| 122 | +<li>Configuring 2 <strong>Consumers</strong> / <strong>MessageHandlers</strong> for the same <strong>Message</strong></li> |
| 123 | +<li>Consuming this <strong>Message</strong> from the same <strong>Queue</strong>.</li> |
| 124 | +</ul> |
| 125 | +<pre><code class="lang-csharp">builder.Services.AddAzureServiceBus(cfg => cfg |
| 126 | + .ConfigureAzureServiceBus(azureServiceBusConfig) |
| 127 | + .AddProducer<ExampleCommand1>(p => p |
| 128 | + .EnsureQueueExists("command-queue-one") |
| 129 | + .WithCommandProducer() |
| 130 | + .ToQueue("command-queue-one")) |
| 131 | + .AddConsumer(c => c |
| 132 | + .FromQueue("command-queue-one") |
| 133 | + .AddHandler<ExampleCommand1, CommandExemple1Handler>() |
| 134 | + .AddHandler<ExampleCommand1, CommandExampleTwoHandlersPerOneMessageHandler>()) |
| 135 | + ); |
| 136 | +</code></pre> |
| 137 | + |
| 138 | +</article> |
| 139 | + |
| 140 | + <div class="contribution d-print-none"> |
| 141 | + <a href="https://github.com/migueltotti/AzureServiceBusFlow/blob/documentation/docs/docs/creatingConsumers.md/#L1" class="edit-link">Edit this page</a> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div class="next-article d-print-none border-top" id="nextArticle"></div> |
| 145 | + |
| 146 | + </div> |
| 147 | + |
| 148 | + <div class="affix"> |
| 149 | + <nav id="affix"></nav> |
| 150 | + </div> |
| 151 | + </main> |
| 152 | + |
| 153 | + <div class="container-xxl search-results" id="search-results"></div> |
| 154 | + |
| 155 | + <footer class="border-top text-secondary"> |
| 156 | + <div class="container-xxl"> |
| 157 | + <div class="flex-fill"> |
| 158 | + <span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + </footer> |
| 162 | + </body> |
| 163 | +</html> |
0 commit comments