Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Assess and migrate workloads from AWS, GCP, or other clouds to Azure services.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/functions/runtimes/csharp.md
1# C# — Azure Functions Isolated Worker Model Triggers & Bindings23> **Model**: .NET isolated worker model (recommended). Uses attributes on methods/parameters.4> Import: `using Microsoft.Azure.Functions.Worker;`56## HTTP Trigger78```csharp9[Function("HttpFunction")]10public static HttpResponseData Run(11[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req,12FunctionContext context)13{14var response = req.CreateResponse(HttpStatusCode.OK);15response.WriteString("Hello!");16return response;17}18```1920## Blob Storage2122```csharp23// Trigger (EventGrid source)24[Function("BlobTrigger")]25public static void Run(26[BlobTrigger("samples-workitems/{name}", Source = BlobTriggerSource.EventGrid,27Connection = "AzureWebJobsStorage")] string blobContent,28string name, FunctionContext context)29{30context.GetLogger("BlobTrigger").LogInformation($"Blob: {name}");31}3233// Input34[BlobInput("input/{name}", Connection = "AzureWebJobsStorage")] string inputBlob3536// Output37[BlobOutput("output/{name}", Connection = "AzureWebJobsStorage")] out string outputBlob38```3940## Queue Storage4142```csharp43// Trigger44[Function("QueueTrigger")]45public static void Run(46[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] string message,47FunctionContext context)48{49context.GetLogger("Queue").LogInformation($"Message: {message}");50}5152// Output (via return type)53[Function("QueueOutput")]54[QueueOutput("outqueue", Connection = "AzureWebJobsStorage")]55public static string Run(56[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)57{58return "queue message";59}60```6162## Timer6364```csharp65[Function("TimerFunction")]66public static void Run(67[TimerTrigger("0 */5 * * * *")] TimerInfo timer,68FunctionContext context)69{70context.GetLogger("Timer").LogInformation($"Last: {timer.ScheduleStatus?.Last}");71}72```7374## Event Grid7576```csharp77// Trigger78[Function("EventGridTrigger")]79public static void Run(80[EventGridTrigger] EventGridEvent eventGridEvent,81FunctionContext context)82{83context.GetLogger("EG").LogInformation($"Event: {eventGridEvent.Subject}");84}8586// Output87[EventGridOutput(TopicEndpointUri = "MyTopicUri", TopicKeySetting = "MyTopicKey")]88```8990## Cosmos DB9192```csharp93// Trigger (Change Feed)94[Function("CosmosDBTrigger")]95public static void Run(96[CosmosDBTrigger("mydb", "mycontainer",97Connection = "CosmosDBConnection",98CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> documents,99FunctionContext context)100{101foreach (var doc in documents)102context.GetLogger("Cosmos").LogInformation($"Doc: {doc.Id}");103}104105// Input106[CosmosDBInput("mydb", "mycontainer", Connection = "CosmosDBConnection",107Id = "{id}", PartitionKey = "{partitionKey}")] MyDocument doc108109// Output110[CosmosDBOutput("mydb", "mycontainer", Connection = "CosmosDBConnection")]111```112113## Service Bus114115```csharp116// Queue Trigger117[Function("SBQueueTrigger")]118public static void Run(119[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] string message,120FunctionContext context)121{122context.GetLogger("SB").LogInformation($"Message: {message}");123}124125// Topic Trigger126[Function("SBTopicTrigger")]127public static void Run(128[ServiceBusTrigger("mytopic", "mysubscription",129Connection = "ServiceBusConnection")] string message,130FunctionContext context)131{132context.GetLogger("SB").LogInformation($"Topic: {message}");133}134135// Output136[ServiceBusOutput("outqueue", Connection = "ServiceBusConnection")]137```138139## Event Hubs140141```csharp142// Trigger143[Function("EventHubTrigger")]144public static void Run(145[EventHubTrigger("myeventhub", Connection = "EventHubConnection")] EventData[] events,146FunctionContext context)147{148foreach (var e in events)149context.GetLogger("EH").LogInformation($"Event: {e.EventBody}");150}151152// Output153[EventHubOutput("outeventhub", Connection = "EventHubConnection")]154```155156## Table Storage157158```csharp159// Input160[TableInput("mytable", "{partitionKey}", "{rowKey}",161Connection = "AzureWebJobsStorage")] TableEntity entity162163// Output164[TableOutput("mytable", Connection = "AzureWebJobsStorage")]165```166167## SQL168169```csharp170// Trigger171[Function("SqlTrigger")]172public static void Run(173[SqlTrigger("dbo.MyTable", "SqlConnection")] IReadOnlyList<SqlChange<MyItem>> changes,174FunctionContext context)175{176foreach (var change in changes)177context.GetLogger("SQL").LogInformation($"Change: {change.Item.Id}");178}179180// Input181[SqlInput("SELECT * FROM dbo.MyTable WHERE Id = @Id",182"SqlConnection", parameters: "@Id={id}")] IEnumerable<MyItem> items183184// Output185[SqlOutput("dbo.MyTable", "SqlConnection")]186```187188## SignalR189190```csharp191// Output192[SignalROutput(HubName = "myhub", ConnectionStringSetting = "AzureSignalRConnectionString")]193```194195## SendGrid196197```csharp198[SendGridOutput(ApiKey = "SendGridApiKey", From = "[email protected]")]199```200201## Multiple Outputs202203```csharp204// Use a custom return type for multiple outputs205public class MultiOutput206{207[QueueOutput("outqueue", Connection = "AzureWebJobsStorage")]208public string QueueMessage { get; set; }209210public HttpResponseData HttpResponse { get; set; }211}212213[Function("MultiOutput")]214public static MultiOutput Run(215[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequestData req)216{217return new MultiOutput218{219QueueMessage = "new item",220HttpResponse = req.CreateResponse(HttpStatusCode.OK)221};222}223```224225> Full reference: [Azure Functions C# isolated worker guide](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide)226