Giter Site home page Giter Site logo

azure-samples / azeventgrid-local-debug-aspnet-core-ngrok Goto Github PK

View Code? Open in Web Editor NEW
0.0 13.0 3.0 480 KB

Tutorial demonstrates how to debug a ASP.Net Core Web API endpoint that receives messages when a new blob is created in an Azure Blob Storage Account.

License: MIT License

Dockerfile 26.81% Shell 32.96% C# 40.23%

azeventgrid-local-debug-aspnet-core-ngrok's Introduction

page_type languages products description urlFragment
sample
bash
csharp
aspnet-core
azure-event-grid
azure-storage
How to Locally Debug a ASP.Net Core Web API that Receives Messages from Azure EventGrid.
azeventgrid-local-debug-aspnet-core-ngrok

How to Locally Debug a ASP Net Core Web API that Receives Messages from Azure EventGrid

This tutorial demonstrates how to debug a ASP.Net Core Web API endpoint that receives messages when a new blob is created in an Azure Blob Storage Account.

For convienience, I am using the Visual Studio Code Dev Containers extension.

Aslo, I have created 2 bash scripts to aid in this tutorial. The scripts are used to create an Azure Storage Account & to delete the Azure Storage Account. You can find more information about these scripts here

Prerequisites

  • An Azure Subscription
  • azure-cli
  • Docker
  • Git
  • Visual Studio Code + Dev Containers

Dev Container

Let's open up the code in a Dev Container. You will want to select the Dev Container icon at the bottom right of the screen & choose Remote-Containers: Open Folder in Container.. & select this cloned repo root directory, which contains the .devcontainer folder.

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-011

Also, let's use azure-cli to login & set the default subscription:

    az login && az account set -s {your_azure_subscription}

ngrok

First, we need to donwload a tool called ngrok. Once you create an account, following the instructions on how to register your authtoken.

Register the Azure EventGrid Provider

If not already registered, we will need to register the Azure EventGrid. Open up a terminal and run the following command.

    az provider show --namespace Microsoft.EventGrid --query "registrationState"

Create Azure Blog Storage Account

We will clone the sample application and navigate to the scripts folder and execute the following command to create our Azure Blog Storage Account and a Blob container called demo:

    chmod +x create-az-storage-account.sh &&
    ./create-az-storage-account.sh -r {name_of_resource_group} -l {location} -a {name_of_blob_storage_account}

If the script ran successfully, it will return back the message The script has created the Azure Resources.

Debugging

Open up the ASP.Net Core Web API project in Visual Studio Code and press F5 to run the project. A quick note, You will want to run the project under http (as opposed to https) to allow ngrok to work.

In the EventGridController, place breakpoints in various places to debug the application when a message is received from Azure EventGrid.

Next, we need to run ngrok. We do this by executing the command below: (note: for Windows user, you need to use the command prompt as I could not get git bash to work).

    # Windows non-wsl users, should be ngrok.exe
    ngrok http -host-header=localhost 5000

Next, we will need to copy the generated url so we can use it to subscribe to events via Azure EventGrid.

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-001

Next, we need to navigate to the Azure Portal to create the Subscription and upload a file, so we can debug the application.

In the Azure Portal, we need to find the Azure Blob Storage Account we created when we ran the create-az-storage-account.sh bash script. Select "Events" and then "Event Subscription".

For the Endpoint Type, we will choose "webhook" and copy the url ngork generated and add "/api/eventgrid" to the end ie https://ngrok-url/api/eventgrid. See the images below for guidance:

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-002

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-003

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-004

When we hit the create button to create the Subscription, for security reasons, Azure EventGrid will validate the endpoint and if you have a breakpoint similar to the picture below, the breakpoint should be activated in Visual Studio Code:

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-005

Also, the new Subscription should be displayed on the Events page of the Azure Blob Storage Account:

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-006

Now let's upload a file and debug the HandleEventGridEventAsync Method inside of the EventGridController (make sure you have a breakpoint in the HandleEventGridEventAsync method).

In the Azure Portal, we need to find the Azure Blog Storage Account and navigate to the demo container. Inside of the container, we will press the upload button. Included in the Git repository root directory is a file called sample.json, you can choose this file or a file of your own choosing. For guidance, see the pictures below:

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-007

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-008

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-009

Once the file is finished uploading, our breakpoint should be activated in Visual Studio Code:

how-to-locally-debug-a-asp-net-core-webapi-that-receives-messages-from-azure-eventgrid-010

Clean Up

Now that we are finished, we can run the delete-az-storage-account.sh bash script to clean up our resources. Run the following command:

    chmod +x delete-az-storage-account.sh &&
    ./delete-az-storage-account.sh -r {name_of_resource_group}

If the script ran successfully, it will return back the message The script has deleted the Azure Resources.

azeventgrid-local-debug-aspnet-core-ngrok's People

Contributors

luyunmt avatar michaeldeongreen avatar microsoft-github-operations[bot] avatar microsoftopensource avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

azeventgrid-local-debug-aspnet-core-ngrok's Issues

[Action Needed] This repo is inactive

This GitHub repository has been identified as a candidate for archival

This repository has had no activity in more than [x amount of time]. Long periods of inactivity present security and code hygiene risks. Archiving will not prevent users from viewing or forking the code. A banner will appear on the repository alerting users that the repository is archived.

Please see https://aka.ms/sunsetting-faq to learn more about this process.

Action

✍️

❗**If this repository is still actively maintained, please simply close this issue. Closing an issue on a repository is considered activity and the repository will not be archived.🔒

If you take no action, this repository is still inactive 30 days from today it will be automatically archived..

Need more help? 🖐️

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.