0
Azure Rest API SOA

Azure Function with signature and prototype

Some of the basic Azure Function with signature and prototype used in application

 

 

public static class BlobFunction

{

[FunctionName(“BlobFunction”)]

public static void Run([BlobTrigger(“samples-workitems/{name}”, Connection = “BlobTriggerConnection”)]Stream myBlob, string name, ILogger log)

{

log.LogInformation($”C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes”);

}

}

 

 

 

public static class CosmosDBFunction

{

[FunctionName(“CosmosDBFunction”)]

public static void Run([CosmosDBTrigger(

databaseName: “databaseName”,

collectionName: “collectionName”,

ConnectionStringSetting = “ConnectionString-CosmosDB”,

LeaseCollectionName = “leases”)]IReadOnlyList<Document> input, ILogger log)

{

if (input != null && input.Count > 0)

{

log.LogInformation(“Documents modified ” + input.Count);

log.LogInformation(“First document Id ” + input[0].Id);

}

}

}

 

public static class HttpFunction

{

[FunctionName(“HttpFunction”)]

public static async Task<IActionResult> Run(

[HttpTrigger(AuthorizationLevel.Function, “get”, “post”,”put”, Route = null)] HttpRequest req,

ILogger log)

{

log.LogInformation(“C# HTTP trigger function processed a request.”);

 

string name = req.Query[“name”];

 

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

dynamic data = JsonConvert.DeserializeObject(requestBody);

name = name ?? data?.name;

 

return name != null

? (ActionResult)new OkObjectResult($”Hello, {name}”)

: new BadRequestObjectResult(“Please pass a name on the query string or in the request body”);

}

}

 

OrchestrationFunction

 

public static class OrchestrationFunction

{

[FunctionName(“OrchestrationFunction”)]

public static async Task<List<string>> RunOrchestrator(

[OrchestrationTrigger] DurableOrchestrationContext context)

{

var outputs = new List<string>();

 

// Replace “hello” with the name of your Durable Activity Function.

outputs.Add(await context.CallActivityAsync<string>(“OrchestrationFunction_Hello”, “Tokyo”));

outputs.Add(await context.CallActivityAsync<string>(“OrchestrationFunction_Hello”, “Seattle”));

outputs.Add(await context.CallActivityAsync<string>(“OrchestrationFunction_Hello”, “London”));

 

// returns [“Hello Tokyo!”, “Hello Seattle!”, “Hello London!”]

return outputs;

}

 

[FunctionName(“OrchestrationFunction_Hello”)]

public static string SayHello([ActivityTrigger] string name, ILogger log)

{

log.LogInformation($”Saying hello to {name}.”);

return $”Hello {name}!”;

}

 

[FunctionName(“OrchestrationFunction_HttpStart”)]

public static async Task<HttpResponseMessage> HttpStart(

[HttpTrigger(AuthorizationLevel.Anonymous, “get”, “post”)]HttpRequestMessage req,

[OrchestrationClient]DurableOrchestrationClient starter,

ILogger log)

{

// Function input comes from the request content.

string instanceId = await starter.StartNewAsync(“OrchestrationFunction”, null);

 

log.LogInformation($”Started orchestration with ID = ‘{instanceId}’.”);

 

return starter.CreateCheckStatusResponse(req, instanceId);

}

}

 

 

public static class QueueFunction

{

[FunctionName(“QueueFunction”)]

public static void Run([QueueTrigger(“HosterMasterQueue”, Connection = “MasterQueueConnetion “)]string myQueueItem, ILogger log)

{

log.LogInformation($”C# Queue trigger function processed: {myQueueItem}”);

}

}

 

public static class SendGridFunction

{

[FunctionName(“SendGridFunction”)]

[return: SendGrid(ApiKey = “Test@Sample”, To = “{CustomerEmail}”, From = “rupeshrupak07l@gmail.com”)]

public static SendGridMessage Run([QueueTrigger(“MasterQueueMessages”, Connection = “MasterQueueConnection”)]Order order, ILogger log)

{

log.LogInformation($”C# Queue trigger function processed order: {order.OrderId}”);

 

SendGridMessage message = new SendGridMessage()

{

Subject = $”Thanks for your order (#{order.OrderId})!”

};

 

message.AddContent(“text/plain”, $”{order.CustomerName}, your order ({order.OrderId}) is being processed!”);

return message;

}

}

public class Order

{

public string OrderId { get; set; }

public string CustomerName { get; set; }

public string CustomerEmail { get; set; }

}

 

 

 

public static class TimerFunction

{

[FunctionName(“TimerFunction”)]

public static void Run([TimerTrigger(“0 */5 * * * *”)]TimerInfo myTimer, ILogger log)

{

log.LogInformation($”C# Timer trigger function executed at: {DateTime.Now}”);

}

}

You Might Also Like...

No Comments

    Leave a Reply