A comprehensive example demonstrating Azure Service Bus integration with .NET applications and Infrastructure as Code using Terraform.
This repository contains a complete example of Azure Service Bus messaging patterns with:
- Infrastructure as Code (Terraform) for Azure resources
- .NET Applications demonstrating Service Bus messaging
- Azure Functions for serverless message processing
- Complete CI/CD ready setup
ASB/
??? ASB.Pusher/ # Azure Functions app (Service Bus message sender)
??? ASB.Reader/ # Console app (Service Bus message consumer)
??? Terraform/ # Infrastructure as Code
? ??? *.tf # Terraform configuration files
? ??? README.md # Terraform-specific documentation
? ??? example-dotnet-function/ # Example Azure Function with Service Bus triggers
??? README.md # This file
- .NET 8/9 SDK
- Azure CLI
- Terraform
- Azure Functions Core Tools (optional)
- Azure subscription with appropriate permissions
# Navigate to Terraform directory
cd Terraform
# Login to Azure
az login
# Configure variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your values
# Deploy infrastructure
terraform init
terraform plan
terraform apply
# Build the solution
dotnet build
# Run the message reader (consumer)
cd ASB.Reader
dotnet run
# In another terminal, run the message pusher (producer)
cd ASB.Pusher
func start # If using Azure Functions locally
# or
dotnet run # If running as console app
- Type: Azure Functions (.NET 8)
- Purpose: Sends messages to Azure Service Bus
- Features:
- HTTP triggers for message sending
- Timer triggers for scheduled messages
- Integration with Application Insights
- Managed Identity authentication
- Type: Console Application (.NET 9)
- Purpose: Consumes messages from Azure Service Bus
- Features:
- Service Bus message processing
- Configurable via appsettings.json
- Structured logging
- Graceful shutdown handling
- Azure Functions with Linux hosting
- Azure Service Bus (Namespace, Queues, Topics)
- Application Insights for monitoring
- Storage Account for Functions runtime
- Managed Identity for secure authentication
- RBAC role assignments
See Terraform/README.md for detailed infrastructure documentation.
Both applications use configuration files and environment variables:
{
"ServiceBus": {
"ConnectionString": "Endpoint=sb://...",
"QueueName": "example-queue",
"TopicName": "example-topic"
},
"ApplicationInsights": {
"ConnectionString": "InstrumentationKey=..."
}
}
AZURE_CLIENT_ID
- Managed Identity client IDSERVICEBUS_NAMESPACE
- Service Bus namespace nameAPPLICATIONINSIGHTS_CONNECTION_STRING
- Application Insights connection
This project demonstrates several Service Bus messaging patterns:
// Send message to queue
await sender.SendMessageAsync(new ServiceBusMessage("Hello Queue!"));
// Process queue messages
await processor.StartProcessingAsync();
// Send to topic
await sender.SendMessageAsync(new ServiceBusMessage("Hello Topic!"));
// Multiple subscribers can process the same message
[Function("ProcessServiceBusMessage")]
public void Run([ServiceBusTrigger("myqueue")] ServiceBusReceivedMessage message)
{
// Process message automatically
}
var client = new ServiceBusClient(
"yournamespace.servicebus.windows.net",
new DefaultAzureCredential());
var client = new ServiceBusClient(connectionString);
- Use Azure Service Bus connection string for local testing
- Configure
local.settings.json
for Azure Functions - Use Service Bus Explorer for message inspection
# Run integration tests
dotnet test
# Test message flow end-to-end
./Terraform/Test-ExampleTestTopic.ps1
- Application Insights for telemetry and logging
- Service Bus metrics in Azure Portal
- Function execution logs in Azure Functions monitor
- Custom dashboards for message processing insights
The project is ready for CI/CD with:
- GitHub Actions workflows (add
.github/workflows/
) - Azure DevOps pipelines
- Infrastructure deployment via Terraform
- Application deployment via Azure Functions deployment
Example GitHub Actions workflow:
name: Deploy ASB Example
on:
push:
branches: [ main ]
jobs:
infrastructure:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy Infrastructure
run: |
cd Terraform
terraform init
terraform apply -auto-approve
applications:
needs: infrastructure
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Deploy Functions
run: |
cd ASB.Pusher
func azure functionapp publish ${{ env.FUNCTION_APP_NAME }}
- Azure Service Bus Documentation
- Azure Functions Documentation
- .NET Azure SDK
- Terraform Azure Provider
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- ?? Check the Terraform README for infrastructure issues
- ?? Report bugs via GitHub Issues
- ?? Ask questions in Discussions
Happy messaging with Azure Service Bus! ??