{"id":7093,"date":"2023-09-28T08:42:31","date_gmt":"2023-09-28T00:42:31","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=7093"},"modified":"2023-09-28T08:42:31","modified_gmt":"2023-09-28T00:42:31","slug":"quickstart-send-events-to-and-receive-events-from-azure-event-hubs-using-net","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/7093","title":{"rendered":"Quickstart: Send events to and receive events from Azure Event Hubs using .NET"},"content":{"rendered":"<p><main id=\"main\" class=\"\" dir=\"ltr\" lang=\"en-us\" role=\"main\" data-bi-name=\"content\"><\/p>\n<div class=\"content \">\n<p><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal\">cc from<\/a><\/p>\n<div class=\"display-flex justify-content-space-between align-items-center flex-wrap-wrap page-metadata-container\">\n<div class=\"margin-right-xxs\">\n<ul class=\"metadata page-metadata\" dir=\"ltr\" lang=\"en-us\" data-bi-name=\"page info\">\n<li>Article<\/li>\n<li class=\"visibility-hidden-visual-diff\"><time class=\"\" datetime=\"2023-08-08T17:00:00.000Z\" data-article-date=\"\" aria-label=\"Article review date\" data-article-date-source=\"calculated\">08\/09\/2023<\/time><\/li>\n<li class=\"contributors-holder display-none-print\"><button class=\"contributors-button link-button\" title=\"View all contributors\" aria-label=\"View all contributors\" data-bi-name=\"contributors\">23 contributors<\/button><\/li>\n<\/ul>\n<\/div>\n<div id=\"user-feedback\" class=\"margin-block-xxs display-none-print\" data-hide-on-archived=\"\"><button id=\"user-feedback-button\" class=\"button button-sm button-clear button-primary\" type=\"button\" data-test-id=\"conceptual-feedback-button\" data-bi-name=\"user-feedback-button\" data-user-feedback-button=\"\">Feedback<\/button><\/div>\n<\/div>\n<div id=\"ms--content-well-notifications\" data-bi-name=\"content-well-notification\"><\/div>\n<nav id=\"center-doc-outline\" class=\"doc-outline display-none-print margin-bottom-sm\" role=\"navigation\" data-bi-name=\"intopic toc\" aria-label=\"In this article\">\n<h2 id=\"ms--in-this-article\" class=\"title is-6 margin-block-xs\">In this article<\/h2>\n<ol id=\"content-well-in-this-article-list\" class=\"border-left padding-left-xxs\" aria-live=\"polite\" aria-relevant=\"additions\">\n<li class=\"\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#prerequisites\">Prerequisites<\/a><\/li>\n<li class=\"\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#send-events-to-the-event-hub\">Send events to the event hub<\/a><\/li>\n<li class=\"\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#receive-events-from-the-event-hub\">Receive events from the event hub<\/a><\/li>\n<li class=\"\"><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#schema-validation-for-event-hubs-sdk-based-applications\">Schema validation for Event Hubs SDK based applications<\/a><\/li>\n<\/ol>\n<p><button class=\"link-button font-weight-semibold font-size-sm margin-top-xxs margin-left-xs\" type=\"button\" aria-expanded=\"false\" data-show-more=\"\" aria-controls=\"content-well-in-this-article-list\" data-title=\"Show 3 more\" data-bi-name=\"show-more-btn\"><span class=\"show-more-text \">Show 3 more<\/span><\/button><\/nav>\n<p>In this quickstart, you learn how to send events to an event hub and then receive those events from the event hub using the\u00a0<strong>Azure.Messaging.EventHubs<\/strong>\u00a0.NET library.<\/p>\n<div class=\"alert is-info\">\n<p class=\"alert-title\">\u00a0Note<\/p>\n<p>Quickstarts are for you to quickly ramp up on the service. If you are already familiar with the service, you may want to see .NET samples for Event Hubs in our .NET SDK repository on GitHub:\u00a0<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/tree\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs\/samples\" data-linktype=\"external\">Event Hubs samples on GitHub<\/a>,\u00a0<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/tree\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs.Processor\/samples\" data-linktype=\"external\">Event processor samples on GitHub<\/a>.<\/p>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"prerequisites\" class=\"heading-anchor\">Prerequisites<\/h2>\n<\/div>\n<p>If you&#8217;re new to Azure Event Hubs, see\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-about\" data-linktype=\"relative-path\">Event Hubs overview<\/a>\u00a0before you go through this quickstart.<\/p>\n<p>To complete this quickstart, you need the following prerequisites:<\/p>\n<ul>\n<li><strong>Microsoft Azure subscription<\/strong>. To use Azure services, including Azure Event Hubs, you need a subscription. If you don&#8217;t have an existing Azure account, you can sign up for a\u00a0<a href=\"https:\/\/azure.microsoft.com\/free\/?WT.mc_id=A261C142F\" data-linktype=\"external\">free trial<\/a>\u00a0or use your MSDN subscriber benefits when you\u00a0<a href=\"https:\/\/azure.microsoft.com\/\" data-linktype=\"external\">create an account<\/a>.<\/li>\n<li><strong>Microsoft Visual Studio 2022<\/strong>. The Azure Event Hubs client library makes use of new features that were introduced in C# 8.0. You can still use the library with previous C# language versions, but the new syntax isn&#8217;t available. To make use of the full syntax, we recommend that you compile with the\u00a0<a href=\"https:\/\/dotnet.microsoft.com\/download\" data-linktype=\"external\">.NET Core SDK<\/a>\u00a03.0 or higher and\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/configure-language-version#override-a-default\" data-linktype=\"absolute-path\">language version<\/a>\u00a0set to\u00a0<code>latest<\/code>. If you&#8217;re using Visual Studio, versions before Visual Studio 2022 aren&#8217;t compatible with the tools needed to build C# 8.0 projects. Visual Studio 2022, including the free Community edition, can be downloaded\u00a0<a href=\"https:\/\/visualstudio.microsoft.com\/vs\/\" data-linktype=\"external\">here<\/a>.<\/li>\n<li><strong>Create an Event Hubs namespace and an event hub<\/strong>. The first step is to use the Azure portal to create an Event Hubs namespace and an event hub in the namespace. Then, obtain the management credentials that your application needs to communicate with the event hub. To create a namespace and an event hub, see\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-create\" data-linktype=\"relative-path\">Quickstart: Create an event hub using Azure portal<\/a>.<\/li>\n<\/ul>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"authenticate-the-app-to-azure\" class=\"heading-anchor\">Authenticate the app to Azure<\/h3>\n<\/div>\n<p>This quick start shows you two ways of connecting to Azure Event Hubs:<\/p>\n<ul>\n<li>Passwordless (Azure Active Directory authentication)<\/li>\n<li>Connection string<\/li>\n<\/ul>\n<p>The first option shows you how to use your security principal in Azure\u00a0<strong>Active Directory and role-based access control (RBAC)<\/strong>\u00a0to connect to an Event Hubs namespace. You don&#8217;t need to worry about having hard-coded connection strings in your code or in a configuration file or in a secure storage like Azure Key Vault.<\/p>\n<p>The second option shows you how to use a\u00a0<strong>connection string<\/strong>\u00a0to connect to an Event Hubs namespace. If you&#8217;re new to Azure, you may find the connection string option easier to follow. We recommend using the passwordless option in real-world applications and production environments. For more information, see\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/service-bus-messaging\/service-bus-authentication-and-authorization\" data-linktype=\"relative-path\">Authentication and authorization<\/a>. You can also read more about passwordless authentication on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/azure\/sdk\/authentication?tabs=command-line\" data-linktype=\"absolute-path\">overview page<\/a>.<\/p>\n<div id=\"tabgroup_1\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_passwordless\" aria-controls=\"tabpanel_1_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_connection-string\" aria-controls=\"tabpanel_1_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_1_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"assign-roles-to-your-azure-ad-user\" class=\"heading-anchor\">Assign roles to your Azure AD user<\/h3>\n<\/div>\n<p>When developing locally, make sure that the user account that connects to Azure Event Hubs has the correct permissions. You&#8217;ll need the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/built-in-roles#azure-event-hubs-data-owner\" data-linktype=\"relative-path\">Azure Event Hubs Data Owner<\/a>\u00a0role in order to send and receive messages. To assign yourself this role, you&#8217;ll need the User Access Administrator role, or another role that includes the\u00a0<code>Microsoft.Authorization\/roleAssignments\/write<\/code>\u00a0action. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. Learn more about the available scopes for role assignments on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/scope-overview\" data-linktype=\"relative-path\">scope overview<\/a>\u00a0page.<\/p>\n<p>The following example assigns the\u00a0<code>Azure Event Hubs Data Owner<\/code>\u00a0role to your user account, which provides full access to Azure Event Hubs resources. In a real scenario, follow the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/active-directory\/develop\/secure-least-privileged-access\" data-linktype=\"relative-path\">Principle of Least Privilege<\/a>\u00a0to give users only the minimum permissions needed for a more secure production environment.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"azure-built-in-roles-for-azure-event-hubs\" class=\"heading-anchor\">Azure built-in roles for Azure Event Hubs<\/h3>\n<\/div>\n<p>For Azure Event Hubs, the management of namespaces and all related resources through the Azure portal and the Azure resource management API is already protected using the Azure RBAC model. Azure provides the below Azure built-in roles for authorizing access to an Event Hubs namespace:<\/p>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/built-in-roles#azure-event-hubs-data-owner\" data-linktype=\"relative-path\">Azure Event Hubs Data Owner<\/a>: Enables data access to Event Hubs namespace and its entities (queues, topics, subscriptions, and filters)<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/built-in-roles#azure-event-hubs-data-sender\" data-linktype=\"relative-path\">Azure Event Hubs Data Sender<\/a>: Use this role to give the sender access to Event Hubs namespace and its entities.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/built-in-roles#azure-event-hubs-data-receiver\" data-linktype=\"relative-path\">Azure Event Hubs Data Receiver<\/a>: Use this role to give the receiver access to Event Hubs namespace and its entities.<\/li>\n<\/ul>\n<p>If you want to create a custom role, see\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/service-bus-messaging\/service-bus-sas#rights-required-for-service-bus-operations\" data-linktype=\"relative-path\">Rights required for Event Hubs operations<\/a>.<\/p>\n<div class=\"alert is-primary\">\n<p class=\"alert-title\">\u00a0Important<\/p>\n<p>In most cases, it will take a minute or two for the role assignment to propagate in Azure. In rare cases, it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.<\/p>\n<\/div>\n<div id=\"tabgroup_1\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-azure-portal\" aria-controls=\"tabpanel_1_roles-azure-portal\" data-tab=\"roles-azure-portal\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Azure portal<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-azure-cli\" aria-controls=\"tabpanel_1_roles-azure-cli\" data-tab=\"roles-azure-cli\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Azure CLI<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-powershell\" aria-controls=\"tabpanel_1_roles-powershell\" data-tab=\"roles-powershell\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">PowerShell<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_1_roles-azure-portal\" role=\"tabpanel\" data-tab=\"roles-azure-portal\">\n<ol>\n<li>In the Azure portal, locate your Event Hubs namespace using the main search bar or left navigation.<\/li>\n<li>On the overview page, select\u00a0<strong>Access control (IAM)<\/strong>\u00a0from the left-hand menu.<\/li>\n<li>On the\u00a0<strong>Access control (IAM)<\/strong>\u00a0page, select the\u00a0<strong>Role assignments<\/strong>\u00a0tab.<\/li>\n<li>Select\u00a0<strong>+ Add<\/strong>\u00a0from the top menu and then\u00a0<strong>Add role assignment<\/strong>\u00a0from the resulting drop-down menu.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/includes\/passwordless\/event-hub\/media\/event-hub-assign-roles\/add-role.png\" alt=\"A screenshot showing how to assign a role.\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<li>Use the search box to filter the results to the desired role. For this example, search for\u00a0<code>Azure Event Hubs Data Owner<\/code>\u00a0and select the matching result. Then choose\u00a0<strong>Next<\/strong>.<\/li>\n<li>Under\u00a0<strong>Assign access to<\/strong>, select\u00a0<strong>User, group, or service principal<\/strong>, and then choose\u00a0<strong>+ Select members<\/strong>.<\/li>\n<li>In the dialog, search for your Azure AD username (usually your\u00a0<em>user@domain<\/em>\u00a0email address) and then choose\u00a0<strong>Select<\/strong>\u00a0at the bottom of the dialog.<\/li>\n<li>Select\u00a0<strong>Review + assign<\/strong>\u00a0to go to the final page, and then\u00a0<strong>Review + assign<\/strong>\u00a0again to complete the process.<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"launch-visual-studio-and-sign-in-to-azure\" class=\"heading-anchor\">Launch Visual Studio and sign-in to Azure<\/h3>\n<\/div>\n<p>You can authorize access to the service bus namespace using the following steps:<\/p>\n<ol>\n<li>Launch Visual Studio. If you see the\u00a0<strong>Get started<\/strong>\u00a0window, select the\u00a0<strong>Continue without code<\/strong>\u00a0link in the right pane.<\/li>\n<li>Select the\u00a0<strong>Sign in<\/strong>\u00a0button in the top right of Visual Studio.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/service-bus-messaging\/media\/service-bus-dotnet-get-started-with-queues\/azure-sign-button-visual-studio.png\" alt=\"Screenshot showing a button to sign in to Azure using Visual Studio.\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<li>Sign-in using the Azure AD account you assigned a role to previously.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/storage\/blobs\/media\/storage-quickstart-blobs-dotnet\/sign-in-visual-studio-account-small.png\" alt=\"Screenshot showing the account selection.\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<\/ol>\n<\/section>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"send-events-to-the-event-hub\" class=\"heading-anchor\">Send events to the event hub<\/h2>\n<\/div>\n<p>This section shows you how to create a .NET Core console application to send events to the event hub you created.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"create-a-console-application\" class=\"heading-anchor\">Create a console application<\/h3>\n<\/div>\n<ol>\n<li>If you have Visual Studio 2022 open already, select\u00a0<strong>File<\/strong>\u00a0on the menu, select\u00a0<strong>New<\/strong>, and then select\u00a0<strong>Project<\/strong>. Otherwise, launch Visual Studio 2022 and select\u00a0<strong>Create a new project<\/strong>\u00a0if you see a popup window.<\/li>\n<li>On the\u00a0<strong>Create a new project<\/strong>\u00a0dialog box, do the following steps: If you don&#8217;t see this dialog box, select\u00a0<strong>File<\/strong>\u00a0on the menu, select\u00a0<strong>New<\/strong>, and then select\u00a0<strong>Project<\/strong>.\n<ol>\n<li>Select\u00a0<strong>C#<\/strong>\u00a0for the programming language.<\/li>\n<li>Select\u00a0<strong>Console<\/strong>\u00a0for the type of the application.<\/li>\n<li>Select\u00a0<strong>Console Application<\/strong>\u00a0from the results list.<\/li>\n<li>Then, select\u00a0<strong>Next<\/strong>.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/new-send-project.png\" alt=\"Image showing the New Project dialog box\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<\/ol>\n<\/li>\n<li>Enter\u00a0<strong>EventHubsSender<\/strong>\u00a0for the project name,\u00a0<strong>EventHubsQuickStart<\/strong>\u00a0for the solution name, and then select\u00a0<strong>Next<\/strong>.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/project-solution-names.png\" alt=\"Image showing the page where you enter solution and project names\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<li>On the\u00a0<strong>Additional information<\/strong>\u00a0page, select\u00a0<strong>Create<\/strong>.<\/li>\n<\/ol>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"add-the-nuget-packages-to-the-project\" class=\"heading-anchor\">Add the NuGet packages to the project<\/h3>\n<\/div>\n<div id=\"tabgroup_1\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_passwordless\" aria-controls=\"tabpanel_1_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless (Recommended)<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_connection-string\" aria-controls=\"tabpanel_1_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_1_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">\n<ol>\n<li>Select\u00a0<strong>Tools<\/strong>\u00a0&gt;\u00a0<strong>NuGet Package Manager<\/strong>\u00a0&gt;\u00a0<strong>Package Manager Console<\/strong>\u00a0from the menu.<\/li>\n<li>Run the following commands to install\u00a0<strong>Azure.Messaging.EventHubs<\/strong>\u00a0and\u00a0<strong>Azure.Identity<\/strong>\u00a0NuGet packages. Press\u00a0<strong>ENTER<\/strong>\u00a0to run the second command.\n<div id=\"code-try-4\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">PowerShell<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\"><code class=\"lang-powershell\" data-author-content=\"Install-Package Azure.Messaging.EventHubs\r\nInstall-Package Azure.Identity\r\n\"><span class=\"hljs-pscommand\">Install-Package<\/span> Azure.Messaging.EventHubs\r\n<span class=\"hljs-pscommand\">Install-Package<\/span> Azure.Identity\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"write-code-to-send-events-to-the-event-hub\" class=\"heading-anchor\">Write code to send events to the event hub<\/h3>\n<\/div>\n<div id=\"tabgroup_2\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_2_passwordless\" aria-controls=\"tabpanel_2_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless (Recommended)<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_2_connection-string\" aria-controls=\"tabpanel_2_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_2_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">\n<ol>\n<li>Replace the existing code in the\u00a0<code>Program.cs<\/code>\u00a0file with the following sample code. Then, replace\u00a0<code>&lt;EVENT_HUB_NAMESPACE&gt;<\/code>\u00a0and\u00a0<code>&lt;HUB_NAME&gt;<\/code>\u00a0placeholder values for the\u00a0<code>EventHubProducerClient<\/code>\u00a0parameters with the names of your Event Hubs namespace and the event hub. For example:\u00a0<code>\"spehubns0309.servicebus.windows.net\"<\/code>\u00a0and\u00a0<code>\"spehub\"<\/code>.\n<p>Here are the important steps from the code:<\/p>\n<ol>\n<li>Creates an\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventhubproducerclient\" data-linktype=\"absolute-path\">EventHubProducerClient<\/a>\u00a0object using the namespace and the event hub name.<\/li>\n<li>Invokes the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventhubproducerclient.createbatchasync\" data-linktype=\"absolute-path\">CreateBatchAsync<\/a>\u00a0method on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventhubproducerclient\" data-linktype=\"absolute-path\">EventHubProducerClient<\/a>\u00a0object to create an\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventdatabatch\" data-linktype=\"absolute-path\">EventDataBatch<\/a>\u00a0object.<\/li>\n<li>Add events to the batch using the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventdatabatch.tryadd\" data-linktype=\"absolute-path\">EventDataBatch.TryAdd<\/a>\u00a0method.<\/li>\n<li>Sends the batch of messages to the event hub using the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.producer.eventhubproducerclient.sendasync\" data-linktype=\"absolute-path\">EventHubProducerClient.SendAsync<\/a>\u00a0method.<\/li>\n<\/ol>\n<div id=\"code-try-6\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">C#<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\"><code class=\"lang-csharp\" data-author-content=\"using Azure.Identity;\r\nusing Azure.Messaging.EventHubs;\r\nusing Azure.Messaging.EventHubs.Producer;\r\nusing System.Text;\r\n\r\n\/\/ number of events to be sent to the event hub\r\nint numOfEvents = 3;\r\n\r\n\/\/ The Event Hubs client types are safe to cache and use as a singleton for the lifetime\r\n\/\/ of the application, which is best practice when events are being published or read regularly.\r\n\/\/ TODO: Replace the &lt;EVENT_HUB_NAMESPACE&gt; and &lt;HUB_NAME&gt; placeholder values\r\nEventHubProducerClient producerClient = new EventHubProducerClient(\r\n    &quot;&lt;EVENT_HUB_NAMESPACE&gt;.servicebus.windows.net&quot;,\r\n    &quot;&lt;HUB_NAME&gt;&quot;,\r\n    new DefaultAzureCredential());\r\n\r\n\/\/ Create a batch of events \r\nusing EventDataBatch eventBatch = await producerClient.CreateBatchAsync();\r\n\r\nfor (int i = 1; i &lt;= numOfEvents; i++)\r\n{\r\n    if (!eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes($&quot;Event {i}&quot;))))\r\n    {\r\n        \/\/ if it is too large for the batch\r\n        throw new Exception($&quot;Event {i} is too large for the batch and cannot be sent.&quot;);\r\n    }\r\n}\r\n\r\ntry\r\n{\r\n    \/\/ Use the producer client to send the batch of events to the event hub\r\n    await producerClient.SendAsync(eventBatch);\r\n    Console.WriteLine($&quot;A batch of {numOfEvents} events has been published.&quot;);\r\n    Console.ReadLine();\r\n}\r\nfinally\r\n{\r\n    await producerClient.DisposeAsync();\r\n}\r\n\"><span class=\"hljs-keyword\">using<\/span> Azure.Identity;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Messaging.EventHubs;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Messaging.EventHubs.Producer;\r\n<span class=\"hljs-keyword\">using<\/span> System.Text;\r\n\r\n<span class=\"hljs-comment\">\/\/ number of events to be sent to the event hub<\/span>\r\n<span class=\"hljs-built_in\">int<\/span> numOfEvents = <span class=\"hljs-number\">3<\/span>;\r\n\r\n<span class=\"hljs-comment\">\/\/ The Event Hubs client types are safe to cache and use as a singleton for the lifetime<\/span>\r\n<span class=\"hljs-comment\">\/\/ of the application, which is best practice when events are being published or read regularly.<\/span>\r\n<span class=\"hljs-comment\">\/\/ <span class=\"hljs-doctag\">TODO:<\/span> Replace the &lt;EVENT_HUB_NAMESPACE&gt; and &lt;HUB_NAME&gt; placeholder values<\/span>\r\nEventHubProducerClient producerClient = <span class=\"hljs-keyword\">new<\/span> EventHubProducerClient(\r\n    <span class=\"hljs-string\">\"&lt;EVENT_HUB_NAMESPACE&gt;.servicebus.windows.net\"<\/span>,\r\n    <span class=\"hljs-string\">\"&lt;HUB_NAME&gt;\"<\/span>,\r\n    <span class=\"hljs-keyword\">new<\/span> DefaultAzureCredential());\r\n\r\n<span class=\"hljs-comment\">\/\/ Create a batch of events <\/span>\r\n<span class=\"hljs-keyword\">using<\/span> EventDataBatch eventBatch = <span class=\"hljs-keyword\">await<\/span> producerClient.CreateBatchAsync();\r\n\r\n<span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-built_in\">int<\/span> i = <span class=\"hljs-number\">1<\/span>; i &lt;= numOfEvents; i++)\r\n{\r\n    <span class=\"hljs-keyword\">if<\/span> (!eventBatch.TryAdd(<span class=\"hljs-keyword\">new<\/span> EventData(Encoding.UTF8.GetBytes(<span class=\"hljs-string\">$\"Event <span class=\"hljs-subst\">{i}<\/span>\"<\/span>))))\r\n    {\r\n        <span class=\"hljs-comment\">\/\/ if it is too large for the batch<\/span>\r\n        <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> Exception(<span class=\"hljs-string\">$\"Event <span class=\"hljs-subst\">{i}<\/span> is too large for the batch and cannot be sent.\"<\/span>);\r\n    }\r\n}\r\n\r\n<span class=\"hljs-keyword\">try<\/span>\r\n{\r\n    <span class=\"hljs-comment\">\/\/ Use the producer client to send the batch of events to the event hub<\/span>\r\n    <span class=\"hljs-keyword\">await<\/span> producerClient.SendAsync(eventBatch);\r\n    Console.WriteLine(<span class=\"hljs-string\">$\"A batch of <span class=\"hljs-subst\">{numOfEvents}<\/span> events has been published.\"<\/span>);\r\n    Console.ReadLine();\r\n}\r\n<span class=\"hljs-keyword\">finally<\/span>\r\n{\r\n    <span class=\"hljs-keyword\">await<\/span> producerClient.DisposeAsync();\r\n}\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<ol start=\"2\">\n<li>Build the project, and ensure that there are no errors.<\/li>\n<li>Run the program and wait for the confirmation message.\n<div id=\"code-try-8\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">C#<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\"><code class=\"lang-csharp\" data-author-content=\"A batch of 3 events has been published.\r\n\">A batch of <span class=\"hljs-number\">3<\/span> events has been published.\r\n<\/code><\/pre>\n<\/li>\n<li>On the\u00a0<strong>Event Hubs Namespace<\/strong>\u00a0page in the Azure portal, you see three incoming messages in the\u00a0<strong>Messages<\/strong>\u00a0chart. Refresh the page to update the chart if needed. It may take a few seconds for it to show that the messages have been received.\n<p><span class=\"mx-imgBorder\"><a class=\"lightbox-enabled\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/verify-messages-portal.png#lightbox\" data-linktype=\"relative-path\" data-bi-name=\"lightbox\" aria-haspopup=\"dialog\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/verify-messages-portal.png\" alt=\"Image of the Azure portal page to verify that the event hub received the events\" data-linktype=\"relative-path\" \/><\/a><\/span><\/p>\n<div class=\"alert is-info\">\n<p class=\"alert-title\">\u00a0Note<\/p>\n<p>For the complete source code with more informational comments, see\u00a0<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/blob\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs\/samples\/Sample04_PublishingEvents.md\" data-linktype=\"external\">this file on the GitHub<\/a><\/p>\n<\/div>\n<\/li>\n<\/ol>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"receive-events-from-the-event-hub\" class=\"heading-anchor\">Receive events from the event hub<\/h2>\n<\/div>\n<p>This section shows how to write a .NET Core console application that receives events from an event hub using an event processor. The event processor simplifies receiving events from event hubs.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"create-an-azure-storage-account-and-a-blob-container\" class=\"heading-anchor\">Create an Azure Storage Account and a blob container<\/h3>\n<\/div>\n<p>In this quickstart, you use Azure Storage as the checkpoint store. Follow these steps to create an Azure Storage account.<\/p>\n<ol>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/storage\/common\/storage-account-create?tabs=azure-portal\" data-linktype=\"relative-path\">Create an Azure Storage account<\/a><\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/storage\/blobs\/storage-quickstart-blobs-portal#create-a-container\" data-linktype=\"relative-path\">Create a blob container<\/a><\/li>\n<li>Authenticate to the blob container using either Azure AD (passwordless) authentication or a connection string to the namespace.<\/li>\n<\/ol>\n<p>Follow these recommendations when using Azure Blob Storage as a checkpoint store:<\/p>\n<ul>\n<li>Use a separate container for each processor group. You can use the same storage account, but use one container per each group.<\/li>\n<li>Don&#8217;t use the container for anything else, and don&#8217;t use the storage account for anything else.<\/li>\n<li>Storage account should be in the same region as the deployed application is located in. If the application is on-premises, try to choose the closest region possible.<\/li>\n<\/ul>\n<p>On the\u00a0<strong>Storage account<\/strong>\u00a0page in the Azure portal, in the\u00a0<strong>Blob service<\/strong>\u00a0section, ensure that the following settings are disabled.<\/p>\n<ul>\n<li>Hierarchical namespace<\/li>\n<li>Blob soft delete<\/li>\n<li>Versioning<\/li>\n<\/ul>\n<div id=\"tabgroup_3\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_3_passwordless\" aria-controls=\"tabpanel_3_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless (Recommended)<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_3_connection-string\" aria-controls=\"tabpanel_3_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_3_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">When developing locally, make sure that the user account that is accessing blob data has the correct permissions. You&#8217;ll need\u00a0<strong>Storage Blob Data Contributor<\/strong>\u00a0to read and write blob data. To assign yourself this role, you&#8217;ll need to be assigned the\u00a0<strong>User Access Administrator<\/strong>\u00a0role, or another role that includes the\u00a0<strong>Microsoft.Authorization\/roleAssignments\/write<\/strong>\u00a0action. You can assign Azure RBAC roles to a user using the Azure portal, Azure CLI, or Azure PowerShell. You can learn more about the available scopes for role assignments on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/role-based-access-control\/scope-overview\" data-linktype=\"relative-path\">scope overview<\/a>\u00a0page.<\/p>\n<p>In this scenario, you&#8217;ll assign permissions to your user account, scoped to the storage account, to follow the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/active-directory\/develop\/secure-least-privileged-access\" data-linktype=\"relative-path\">Principle of Least Privilege<\/a>. This practice gives users only the minimum permissions needed and creates more secure production environments.<\/p>\n<p>The following example will assign the\u00a0<strong>Storage Blob Data Contributor<\/strong>\u00a0role to your user account, which provides both read and write access to blob data in your storage account.<\/p>\n<div class=\"alert is-primary\">\n<p class=\"alert-title\">\u00a0Important<\/p>\n<p>In most cases it will take a minute or two for the role assignment to propagate in Azure, but in rare cases it may take up to eight minutes. If you receive authentication errors when you first run your code, wait a few moments and try again.<\/p>\n<\/div>\n<div id=\"tabgroup_1\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-azure-portal\" aria-controls=\"tabpanel_1_roles-azure-portal\" data-tab=\"roles-azure-portal\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Azure portal<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-azure-cli\" aria-controls=\"tabpanel_1_roles-azure-cli\" data-tab=\"roles-azure-cli\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Azure CLI<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_1_roles-powershell\" aria-controls=\"tabpanel_1_roles-powershell\" data-tab=\"roles-powershell\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">PowerShell<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_1_roles-azure-portal\" role=\"tabpanel\" data-tab=\"roles-azure-portal\">\n<ol>\n<li>In the Azure portal, locate your storage account using the main search bar or left navigation.<\/li>\n<li>On the storage account overview page, select\u00a0<strong>Access control (IAM)<\/strong>\u00a0from the left-hand menu.<\/li>\n<li>On the\u00a0<strong>Access control (IAM)<\/strong>\u00a0page, select the\u00a0<strong>Role assignments<\/strong>\u00a0tab.<\/li>\n<li>Select\u00a0<strong>+ Add<\/strong>\u00a0from the top menu and then\u00a0<strong>Add role assignment<\/strong>\u00a0from the resulting drop-down menu.\n<p><span class=\"mx-imgBorder\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/storage\/common\/media\/assign-role-system-identity.png\" alt=\"A screenshot showing how to assign a storage account role.\" data-linktype=\"relative-path\" \/><\/span><\/li>\n<li>Use the search box to filter the results to the desired role. For this example, search for\u00a0<em>Storage Blob Data Contributor<\/em>\u00a0and select the matching result and then choose\u00a0<strong>Next<\/strong>.<\/li>\n<li>Under\u00a0<strong>Assign access to<\/strong>, select\u00a0<strong>User, group, or service principal<\/strong>, and then choose\u00a0<strong>+ Select members<\/strong>.<\/li>\n<li>In the dialog, search for your Azure AD username (usually your\u00a0<em>user@domain<\/em>\u00a0email address) and then choose\u00a0<strong>Select<\/strong>\u00a0at the bottom of the dialog.<\/li>\n<li>Select\u00a0<strong>Review + assign<\/strong>\u00a0to go to the final page, and then\u00a0<strong>Review + assign<\/strong>\u00a0again to complete the process.<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<\/section>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"create-a-project-for-the-receiver\" class=\"heading-anchor\">Create a project for the receiver<\/h3>\n<\/div>\n<ol>\n<li>In the Solution Explorer window, right-click the\u00a0<strong>EventHubQuickStart<\/strong>\u00a0solution, point to\u00a0<strong>Add<\/strong>, and select\u00a0<strong>New Project<\/strong>.<\/li>\n<li>Select\u00a0<strong>Console application<\/strong>, and select\u00a0<strong>Next<\/strong>.<\/li>\n<li>Enter\u00a0<strong>EventHubsReceiver<\/strong>\u00a0for the\u00a0<strong>Project name<\/strong>, and select\u00a0<strong>Create<\/strong>.<\/li>\n<li>In the\u00a0<strong>Solution Explorer<\/strong>\u00a0window, right-click\u00a0<strong>EventHubsReceiver<\/strong>, and select\u00a0<strong>Set as a Startup Project<\/strong>.<\/li>\n<\/ol>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"add-the-nuget-packages-to-the-project-1\" class=\"heading-anchor\">Add the NuGet packages to the project<\/h3>\n<\/div>\n<div id=\"tabgroup_4\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_4_passwordless\" aria-controls=\"tabpanel_4_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless (Recommended)<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_4_connection-string\" aria-controls=\"tabpanel_4_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_4_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">\n<ol>\n<li>Select\u00a0<strong>Tools<\/strong>\u00a0&gt;\u00a0<strong>NuGet Package Manager<\/strong>\u00a0&gt;\u00a0<strong>Package Manager Console<\/strong>\u00a0from the menu.<\/li>\n<li>In the\u00a0<strong>Package Manager Console<\/strong>\u00a0window, confirm that\u00a0<strong>EventHubsReceiver<\/strong>\u00a0is selected for the\u00a0<strong>Default project<\/strong>. If not, use the drop-down list to select\u00a0<strong>EventHubsReceiver<\/strong>.<\/li>\n<li>Run the following command to install the\u00a0<strong>Azure.Messaging.EventHubs<\/strong>\u00a0and the\u00a0<strong>Azure.Identity<\/strong>\u00a0NuGet packages. Press\u00a0<strong>ENTER<\/strong>\u00a0to run the last command.\n<div id=\"code-try-13\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">PowerShell<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\"><code class=\"lang-powershell\" data-author-content=\"Install-Package Azure.Messaging.EventHubs\r\nInstall-Package Azure.Messaging.EventHubs.Processor\r\nInstall-Package Azure.Identity\r\n\"><span class=\"hljs-pscommand\">Install-Package<\/span> Azure.Messaging.EventHubs\r\n<span class=\"hljs-pscommand\">Install-Package<\/span> Azure.Messaging.EventHubs.Processor\r\n<span class=\"hljs-pscommand\">Install-Package<\/span> Azure.Identity\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<div class=\"heading-wrapper\" data-heading-level=\"h3\">\n<h3 id=\"update-the-code\" class=\"heading-anchor\">Update the code<\/h3>\n<\/div>\n<p>Replace the contents of\u00a0<strong>Program.cs<\/strong>\u00a0with the following code:<\/p>\n<div id=\"tabgroup_5\" class=\"tabGroup\" data-bi-name=\"tab-group\">\n<ul role=\"tablist\">\n<li role=\"presentation\"><a tabindex=\"0\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_5_passwordless\" aria-controls=\"tabpanel_5_passwordless\" data-tab=\"passwordless\" aria-selected=\"true\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\">Passwordless (Recommended)<\/a><\/li>\n<li role=\"presentation\"><a tabindex=\"-1\" role=\"tab\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/event-hubs-dotnet-standard-getstarted-send?tabs=passwordless%2Croles-azure-portal#tabpanel_5_connection-string\" aria-controls=\"tabpanel_5_connection-string\" data-tab=\"connection-string\" data-linktype=\"self-bookmark\" data-bi-name=\"tab\" aria-selected=\"false\">Connection String<\/a><\/li>\n<\/ul>\n<section id=\"tabpanel_5_passwordless\" role=\"tabpanel\" data-tab=\"passwordless\">\n<ol>\n<li>Replace the existing code in the\u00a0<code>Program.cs<\/code>\u00a0file with the following sample code. Then, replace the\u00a0<code>&lt;STORAGE_ACCOUNT_NAME&gt;<\/code>\u00a0and\u00a0<code>&lt;BLOB_CONTAINER_NAME&gt;<\/code>\u00a0placeholder values for the\u00a0<code>BlobContainerClient<\/code>\u00a0URI. Replace the\u00a0<code>&lt;EVENT_HUB_NAMESPACE&gt;<\/code>\u00a0and\u00a0<code>&lt;HUB_NAME&gt;<\/code>\u00a0placeholder values for the\u00a0<code>EventProcessorClient<\/code>\u00a0as well.\n<p>Here are the important steps from the code:<\/p>\n<ol>\n<li>Creates an\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient\" data-linktype=\"absolute-path\">EventProcessorClient<\/a>\u00a0object using the Event Hubs namespace and the event hub name. You need to build\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.storage.blobs.blobcontainerclient\" data-linktype=\"absolute-path\">BlobContainerClient<\/a>\u00a0object for the container in the Azure storage you created earlier.<\/li>\n<li>Specifies handlers for the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient.processeventasync\" data-linktype=\"absolute-path\">ProcessEventAsync<\/a>\u00a0and\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient.processerrorasync\" data-linktype=\"absolute-path\">ProcessErrorAsync<\/a>\u00a0events of the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient\" data-linktype=\"absolute-path\">EventProcessorClient<\/a>\u00a0object.<\/li>\n<li>Starts processing events by invoking the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient.startprocessingasync\" data-linktype=\"absolute-path\">StartProcessingAsync<\/a>\u00a0on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient\" data-linktype=\"absolute-path\">EventProcessorClient<\/a>\u00a0object.<\/li>\n<li>Stops processing events after 30 seconds by invoking\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient.stopprocessingasync\" data-linktype=\"absolute-path\">StopProcessingAsync<\/a>\u00a0on the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/azure.messaging.eventhubs.eventprocessorclient\" data-linktype=\"absolute-path\">EventProcessorClient<\/a>\u00a0object.<\/li>\n<\/ol>\n<div id=\"code-try-15\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">C#<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\" tabindex=\"0\" role=\"group\" aria-label=\"Horizontally scrollable code\"><code class=\"lang-csharp\" style=\"box-sizing: inherit; outline-color: inherit; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 1em; direction: ltr; border: 0px; padding: 0px; line-height: 1.3571; display: block; position: relative;\" data-author-content=\"using Azure.Identity;\r\nusing Azure.Messaging.EventHubs;\r\nusing Azure.Messaging.EventHubs.Consumer;\r\nusing Azure.Messaging.EventHubs.Processor;\r\nusing Azure.Storage.Blobs;\r\nusing System.Text;\r\n\r\n\/\/ Create a blob container client that the event processor will use\r\n\/\/ TODO: Replace &lt;STORAGE_ACCOUNT_NAME&gt; and &lt;BLOB_CONTATINAER_NAME&gt; with actual names\r\nBlobContainerClient storageClient = new BlobContainerClient(\r\n    new Uri(&quot;https:\/\/&lt;STORAGE_ACCOUNT_NAME&gt;.blob.core.windows.net\/&lt;BLOB_CONTAINER_NAME&gt;&quot;),\r\n    new DefaultAzureCredential());\r\n\r\n\/\/ Create an event processor client to process events in the event hub\r\n\/\/ TODO: Replace the &lt;EVENT_HUBS_NAMESPACE&gt; and &lt;HUB_NAME&gt; placeholder values\r\nvar processor = new EventProcessorClient(\r\n    storageClient,\r\n    EventHubConsumerClient.DefaultConsumerGroupName,\r\n    &quot;&lt;EVENT_HUB_NAMESPACE&gt;.servicebus.windows.net&quot;,\r\n    &quot;&lt;HUB_NAME&gt;&quot;,\r\n    new DefaultAzureCredential());\r\n\r\n\/\/ Register handlers for processing events and handling errors\r\nprocessor.ProcessEventAsync += ProcessEventHandler;\r\nprocessor.ProcessErrorAsync += ProcessErrorHandler;\r\n\r\n\/\/ Start the processing\r\nawait processor.StartProcessingAsync();\r\n\r\n\/\/ Wait for 30 seconds for the events to be processed\r\nawait Task.Delay(TimeSpan.FromSeconds(30));\r\n\r\n\/\/ Stop the processing\r\nawait processor.StopProcessingAsync();\r\n\r\nTask ProcessEventHandler(ProcessEventArgs eventArgs)\r\n{\r\n    \/\/ Write the body of the event to the console window\r\n    Console.WriteLine(&quot;\\tReceived event: {0}&quot;, Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));\r\n    Console.ReadLine();\r\n    return Task.CompletedTask;\r\n}\r\n\r\nTask ProcessErrorHandler(ProcessErrorEventArgs eventArgs)\r\n{\r\n    \/\/ Write details about the error to the console window\r\n    Console.WriteLine($&quot;\\tPartition '{eventArgs.PartitionId}': an unhandled exception was encountered. This was not expected to happen.&quot;);\r\n    Console.WriteLine(eventArgs.Exception.Message);\r\n    Console.ReadLine();\r\n    return Task.CompletedTask;\r\n}\r\n\"><span class=\"hljs-keyword\">using<\/span> Azure.Identity;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Messaging.EventHubs;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Messaging.EventHubs.Consumer;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Messaging.EventHubs.Processor;\r\n<span class=\"hljs-keyword\">using<\/span> Azure.Storage.Blobs;\r\n<span class=\"hljs-keyword\">using<\/span> System.Text;\r\n\r\n<span class=\"hljs-comment\">\/\/ Create a blob container client that the event processor will use<\/span>\r\n<span class=\"hljs-comment\">\/\/ <span class=\"hljs-doctag\">TODO:<\/span> Replace &lt;STORAGE_ACCOUNT_NAME&gt; and &lt;BLOB_CONTATINAER_NAME&gt; with actual names<\/span>\r\nBlobContainerClient storageClient = <span class=\"hljs-keyword\">new<\/span> BlobContainerClient(\r\n    <span class=\"hljs-keyword\">new<\/span> Uri(<span class=\"hljs-string\">\"https:\/\/&lt;STORAGE_ACCOUNT_NAME&gt;.blob.core.windows.net\/&lt;BLOB_CONTAINER_NAME&gt;\"<\/span>),\r\n    <span class=\"hljs-keyword\">new<\/span> DefaultAzureCredential());\r\n\r\n<span class=\"hljs-comment\">\/\/ Create an event processor client to process events in the event hub<\/span>\r\n<span class=\"hljs-comment\">\/\/ <span class=\"hljs-doctag\">TODO:<\/span> Replace the &lt;EVENT_HUBS_NAMESPACE&gt; and &lt;HUB_NAME&gt; placeholder values<\/span>\r\n<span class=\"hljs-keyword\">var<\/span> processor = <span class=\"hljs-keyword\">new<\/span> EventProcessorClient(\r\n    storageClient,\r\n    EventHubConsumerClient.DefaultConsumerGroupName,\r\n    <span class=\"hljs-string\">\"&lt;EVENT_HUB_NAMESPACE&gt;.servicebus.windows.net\"<\/span>,\r\n    <span class=\"hljs-string\">\"&lt;HUB_NAME&gt;\"<\/span>,\r\n    <span class=\"hljs-keyword\">new<\/span> DefaultAzureCredential());\r\n\r\n<span class=\"hljs-comment\">\/\/ Register handlers for processing events and handling errors<\/span>\r\nprocessor.ProcessEventAsync += ProcessEventHandler;\r\nprocessor.ProcessErrorAsync += ProcessErrorHandler;\r\n\r\n<span class=\"hljs-comment\">\/\/ Start the processing<\/span>\r\n<span class=\"hljs-keyword\">await<\/span> processor.StartProcessingAsync();\r\n\r\n<span class=\"hljs-comment\">\/\/ Wait for 30 seconds for the events to be processed<\/span>\r\n<span class=\"hljs-keyword\">await<\/span> Task.Delay(TimeSpan.FromSeconds(<span class=\"hljs-number\">30<\/span>));\r\n\r\n<span class=\"hljs-comment\">\/\/ Stop the processing<\/span>\r\n<span class=\"hljs-keyword\">await<\/span> processor.StopProcessingAsync();\r\n\r\n<span class=\"hljs-function\">Task <span class=\"hljs-title\">ProcessEventHandler<\/span>(<span class=\"hljs-params\">ProcessEventArgs eventArgs<\/span>)<\/span>\r\n{\r\n    <span class=\"hljs-comment\">\/\/ Write the body of the event to the console window<\/span>\r\n    Console.WriteLine(<span class=\"hljs-string\">\"\\tReceived event: {0}\"<\/span>, Encoding.UTF8.GetString(eventArgs.Data.Body.ToArray()));\r\n    Console.ReadLine();\r\n    <span class=\"hljs-keyword\">return<\/span> Task.CompletedTask;\r\n}\r\n\r\n<span class=\"hljs-function\">Task <span class=\"hljs-title\">ProcessErrorHandler<\/span>(<span class=\"hljs-params\">ProcessErrorEventArgs eventArgs<\/span>)<\/span>\r\n{\r\n    <span class=\"hljs-comment\">\/\/ Write details about the error to the console window<\/span>\r\n    Console.WriteLine(<span class=\"hljs-string\">$\"\\tPartition '<span class=\"hljs-subst\">{eventArgs.PartitionId}<\/span>': an unhandled exception was encountered. This was not expected to happen.\"<\/span>);\r\n    Console.WriteLine(eventArgs.Exception.Message);\r\n    Console.ReadLine();\r\n    <span class=\"hljs-keyword\">return<\/span> Task.CompletedTask;\r\n}\r\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<\/section>\n<\/div>\n<ol start=\"2\">\n<li>Build the project, and ensure that there are no errors.\n<div class=\"alert is-info\">\n<p class=\"alert-title\">\u00a0Note<\/p>\n<p>For the complete source code with more informational comments, see\u00a0<a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/blob\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs.Processor\/samples\/Sample01_HelloWorld.md\" data-linktype=\"external\">this file on the GitHub<\/a>.<\/p>\n<\/div>\n<\/li>\n<li>Run the receiver application.<\/li>\n<li>You should see a message that the events have been received.\n<div id=\"code-try-17\" class=\"codeHeader\" data-bi-name=\"code-header\"><span class=\"language\">Bash<\/span><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\"><\/button><button class=\"action position-relative display-none-print\" type=\"button\" data-bi-name=\"copy\">Copy<\/button><\/p>\n<div class=\"successful-copy-alert position-absolute right-0 top-0 left-0 bottom-0 display-flex align-items-center justify-content-center has-text-success-invert has-background-success is-transparent\" aria-hidden=\"true\"><\/div>\n<\/div>\n<pre class=\"has-inner-focus\"><code class=\"lang-bash\" data-author-content=\"Received event: Event 1\r\nReceived event: Event 2\r\nReceived event: Event 3    \r\n\">Received event: Event 1\r\nReceived event: Event 2\r\nReceived event: Event 3    \r\n<\/code><\/pre>\n<p>These events are the three events you sent to the event hub earlier by running the sender program.<\/li>\n<li>In the Azure portal, you can verify that there are three outgoing messages, which Event Hubs sent to the receiving application. Refresh the page to update the chart. It may take a few seconds for it to show that the messages have been received.\n<p><span class=\"mx-imgBorder\"><a class=\"lightbox-enabled\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/verify-messages-portal-2.png#lightbox\" data-linktype=\"relative-path\" data-bi-name=\"lightbox\" aria-haspopup=\"dialog\"><img decoding=\"async\" src=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/media\/getstarted-dotnet-standard-send-v2\/verify-messages-portal-2.png\" alt=\"Image of the Azure portal page to verify that the event hub sent events to the receiving app\" data-linktype=\"relative-path\" \/><\/a><\/span><\/li>\n<\/ol>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"schema-validation-for-event-hubs-sdk-based-applications\" class=\"heading-anchor\">Schema validation for Event Hubs SDK based applications<\/h2>\n<\/div>\n<p>You can use Azure Schema Registry to perform schema validation when you stream data with your Event Hubs SDK-based applications. Azure Schema Registry of Event Hubs provides a centralized repository for managing schemas and you can seamlessly connect your new or existing applications with Schema Registry.<\/p>\n<p>To learn more, see\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/event-hubs\/schema-registry-dotnet-send-receive-quickstart\" data-linktype=\"relative-path\">Validate schemas with Event Hubs SDK<\/a>.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"clean-up-resources\" class=\"heading-anchor\">Clean up resources<\/h2>\n<\/div>\n<p>Delete the resource group that has the Event Hubs namespace or delete only the namespace if you want to keep the resource group.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"samples-and-reference\" class=\"heading-anchor\">Samples and reference<\/h2>\n<\/div>\n<p>This quick start provides step-by-step instructions to implement a scenario of sending a batch of events to an event hub and then receiving them. For more samples, select the following links.<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/tree\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs\/samples\" data-linktype=\"external\">Event Hubs samples on GitHub<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-sdk-for-net\/tree\/master\/sdk\/eventhub\/Azure.Messaging.EventHubs.Processor\/samples\" data-linktype=\"external\">Event processor samples on GitHub<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/Azure\/azure-event-hubs\/tree\/master\/samples\/DotNet\/Azure.Messaging.EventHubs\/ManagedIdentityWebApp\" data-linktype=\"external\">Azure role-based access control (Azure RBAC) sample<\/a><\/li>\n<\/ul>\n<p>For complete .NET library reference, see our\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/overview\/azure\/event-hubs\" data-linktype=\"absolute-path\">SDK documentation<\/a>.<\/p>\n<div class=\"heading-wrapper\" data-heading-level=\"h2\">\n<h2 id=\"next-steps\" class=\"heading-anchor\">Next steps<\/h2>\n<\/div>\n<p>See the following tutorial:<\/p>\n<div class=\"nextstepaction\">\n<p><a class=\"button button-primary button-filled has-text-wrap\" href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/stream-analytics\/stream-analytics-real-time-fraud-detection?toc=\/azure\/event-hubs\/TOC.json\" data-linktype=\"relative-path\">Tutorial: Visualize data anomalies in real-time events sent to Azure Event Hubs<\/a><\/p>\n<\/div>\n<\/div>\n<div id=\"assertive-live-region\" class=\"visually-hidden\" role=\"alert\" aria-live=\"assertive\" aria-relevant=\"additions\" aria-atomic=\"true\"><\/div>\n<div id=\"polite-live-region\" class=\"visually-hidden\" role=\"status\" aria-live=\"polite\" aria-relevant=\"additions\" aria-atomic=\"true\"><\/div>\n<p><\/main><\/p>\n<section class=\"feedback-section position-relative\" data-bi-name=\"feedback-section\">\n<h2 id=\"feedback\" class=\"title is-3 margin-top-sm\">Feedback<\/h2>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>cc from Article 08\/0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-7093","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=7093"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7093\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=7093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=7093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=7093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}