An open API service indexing awesome lists of open source software.

https://github.com/daulet/servernet

Azure Functions made easy
https://github.com/daulet/servernet

azure-functions serverless serverless-framework

Last synced: 8 months ago
JSON representation

Azure Functions made easy

Awesome Lists containing this project

README

          

# ServerNET
*Azure Functions made easy.*

Declare your Azure Function triggers, inputs and outputs in code:
``` cs
[HttpOutput]
public static async Task RunAsync(
[HttpTrigger(HttpMethod.Post, "users/{username:alpha}")] HttpRequestMessage request,
string username,
[Table("User", partitionKey: "{username}", rowKey: "payment")] PaymentInstrument paymentInstrument,
[Queue("user-queue")] IAsyncCollector paymentQueue,
TraceWriter log)
{
// your code here
}
```
and then use servernet command line tool to generate Azure Functions release package:
```
servernet.CLI.exe -a Servernet.Samples.DocumentationSamples.dll -o release/path
```
that will copy your assembly with its dependencies and generate *project.json* that is ready to deploy:
```
{
"disabled": false,
"scriptFile": "Servernet.Samples.DocumentationSamples.dll",
"entryPoint": "Servernet.Samples.DocumentationSamples.ReadmeFunction.RunAsync",
"bindings": [
{
"direction": "out",
"name": "$return",
"type": "http"
},
{
"authLevel": "Anonymous",
"direction": "in",
"methods": [
"POST"
],
"name": "request",
"route": "users/{username:alpha}",
"type": "httpTrigger"
},
{
"connection": "ReadmeFunction_input_table_paymentInstrument",
"direction": "in",
"filter": null,
"name": "paymentInstrument",
"partitionKey": "{username}",
"rowKey": "payment",
"tableName": "User",
"take": 0,
"type": "table"
},
{
"connection": "ReadmeFunction_output_queue_paymentQueue",
"direction": "out",
"name": "paymentQueue",
"queueName": "user-queue",
"type": "queue"
}
]
}
```

## Using command line tool

Using [Azure Functions CLI](https://www.npmjs.com/package/azure-functions-cli) create a new Function App, and then generate a function from your binary:
```
servernet.CLI.exe -a assembly.dll -f Namespace.Type.FunctionName -o release/path
```
Or generate functions for all types decorated with [AzureFunction] in the assembly:
```
servernet.CLI.exe -a assembly.dll -o release/path
```
Ready to run it? Follow [these steps](https://blogs.msdn.microsoft.com/appserviceteam/2017/03/16/publishing-a-net-class-library-as-a-function-app/) to configure you Function App as Visual Studio project. If you are going to test it locally be sure to setup connection strings in appsettings.json (you most likely will need to set *IsEncrypted: false*), but if you are not sure how, just start running the app locally, the console prompt will guide you.

## Using [AzureFunction] attribute

Use [AzureFunction] attribute to let command line tool to autodetect functions to generate ([see above](#using-command-line-tool)), or to override parameters like *Disabled* and *Name*:
```cs
[AzureFunction(Disabled = false, Name = "NameToUseInAzureFunctions")]
public class MyFunctionClass
{
// currently expecting a single public static method defined
}
```

# Bindings

![Code + Events](./docs/code%2Bevents.jpg)

Azure Functions provides binding of your code to various events/triggers. What ServerNET provides is letting you to focus on your code and generate deployable Azure Functions package, including boilerplate like *function.json* with trigger/input/output bindings and release layout of the Azure Function. ServerNET also limits you to things that are supported by Azure Functions, e.g. according to [Azure Function guidelines](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook#httptrigger) when you want to setup a webhook trigger you can't use *methods* property that you'd normally use for HTTP trigger. ServerNET provides strongly typed parameterization of your triggers, input and output parameters. What can't be enforced in design time (i.e. at compile time) is enforced at generation time (using ServerNET CLI), before you deploy to Azure, which means if there is any problem with your function definition you'll find out as soon as possible.

This library supports all the same trigger, input and output parameters as Azure Functions. Few exceptions are patterns that are bad practice, e.g. *out HttpResponseMessage* parameter. Pick entry method/function (currently only *public static* methods are supported) for your logic, decorate it with provided attributes and ServerNET CLI will generate deployable Azure Functions for you:

| Kind | Trigger | Input | Output |
| ---- | :-----: | :---: | :----: |
| [HTTP](#http-and-webhook-bindings) | [✔](#http-trigger) | | [✔](#http-output) |
| [WebHook](#http-and-webhook-bindings) | [✔](#webhook-trigger) | | [✔](#http-output) |
| [Timer](#timer-bindings) | [✔](#timer-trigger) | | |
| [Storage Blob](#blob-bindings) | [✔](#blob-trigger) | [✔](#blob-input) | [✔](#blob-output) |
| [Storage Queue](#queue-bindings) | [✔](#queue-trigger) | | [✔](#queue-output) |
| [Storage Table](#table-bindings) | | [✔](#table-input) | [✔](#table-output) |

## HTTP and WebHook bindings

For complete documentation on how to use HTTP and WebHook parameters in Azure Functions see [official documentaion](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook). For complete example that uses HTTP request and response see [HttpOutputFunction](./samples/DocumentationSamples/HttpOutputFunction.cs).

### HTTP Trigger

Decorate your entry method parameter with [HttpTrigger] attribute ([sample](./samples/DocumentationSamples/HttpTriggerFunction.cs)). Below is the list of parameter types that can be used with [HttpTrigger] attribute:
* [HttpRequestMessage](https://msdn.microsoft.com/en-us/library/system.net.http.httprequestmessage(v=vs.118).aspx);
* Custom type, request body will be deserialized from JSON into your object;

### WebHook Trigger

Decorate your entry method parameter with [WebHookTrigger] attribute ([sample](./samples/DocumentationSamples/WebHookTriggerFunction.cs)). Works with the same parameter types as [HTTP Trigger](#http-trigger).

### HTTP Output

Decoreate your entry method (not parameter) with [HttpOutput] attribute ([sample](./samples/DocumentationSamples/HttpOutputFunction.cs)). Using *out HttpResponseMessage* parameter is bad practice, hence only return parameter is supported (including async option with Task\). Below is the list of parameter types that can be used with [HttpResponse] attribute:
* [HttpResponseMessage](https://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.118).aspx), including async equivalent Task\;

## Timer bindings

For complete documentation on how to use Timer parameters in Azure Functions see [official documentaion](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer).

### Timer Trigger

Decorate your entry method parameter with \[[TimerTrigger](https://github.com/Azure/azure-webjobs-sdk-extensions/wiki/TimerTrigger)\] attribute ([sample](./samples/DocumentationSamples/TimerTriggerFunction.cs)). Below is the list of parameter types that can be used with [TimerTrigger] attribute:
* [TimerInfo](https://github.com/Azure/azure-webjobs-sdk-extensions/blob/master/src/WebJobs.Extensions/Extensions/Timers/TimerInfo.cs);

## Blob bindings

For complete documentation on how to use Blob parameters in Azure Functions see [official documentaion](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob).

### Blob Trigger

Decorate your entry method parameter with \[[BlobTrigger](https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/BlobTriggerAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/BlobTriggerFunction.cs)). Below is the list of parameter types that can be used with [BlobTrigger] attribute:
* Custom type, blob will be deserialized from JSON into your object;
* [String](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx);
* [TextReader](https://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.110).aspx);
* [Stream](https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx);
* [ICloudBlob](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.storage.blob.icloudblob.aspx);
* [CloudBlockBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudblockblob.aspx);
* [CloudPageBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudpageblob.aspx);
* [CloudBlobContainer](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobcontainer.aspx);
* [CloudBlobDirectory](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobdirectory.aspx);
* IEnumerable<[CloudBlockBlob](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblockblob.aspx)>;
* IEnumerable<[CloudPageBlob](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudpageblob.aspx)>;
* Other types deserialized [ICloudBlobStreamBinder](https://docs.microsoft.com/en-us/azure/app-service-web/websites-dotnet-webjobs-sdk-storage-blobs-how-to#icbsb);

### Blob Input

Decorate your entry method parameter with \[[Blob](https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/BlobAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/BlobInputOutputFunction.cs)). Below is the list of parameter types that can be used with [Blob] attribute:
* Custom type, blob will be deserialized from JSON into your object;
* [String](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx);
* [TextReader](https://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.110).aspx);
* [Stream](https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx);
* [ICloudBlob](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.storage.blob.icloudblob.aspx);
* [CloudBlockBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudblockblob.aspx);
* [CloudPageBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudpageblob.aspx);

### Blob Output

Decorate your entry method *out* parameter with \[[Blob](https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/BlobAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/BlobInputOutputFunction.cs)). Below is the list of parameter types that can be used with [Blob] attribute:
* Custom type, blob will be deserialized from JSON into your object;
* [String](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx);
* [TextReader](https://msdn.microsoft.com/en-us/library/system.io.textreader(v=vs.110).aspx);
* [Stream](https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx);
* [CloudBlobStream](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.cloudblobstream.aspx);
* [ICloudBlob](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.storage.blob.icloudblob.aspx);
* [CloudBlockBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudblockblob.aspx);
* [CloudPageBlob](https://msdn.microsoft.com/en-us/library/azure/microsoft.windowsazure.storage.blob.cloudpageblob.aspx);

## Queue bindings

For complete documentation on how to use Queue parameters in Azure Functions see [official documentaion](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue).

### Queue Trigger

Decorate your entry method parameter with \[[QueueTrigger](hhttps://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/QueueTriggerAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/QueueTriggerFunction.cs)). Below is the list of parameter types that can be used with [QueueTrigger] attribute:
* T, where T is the type that you want to deserialize the queue message into as JSON;
* [String](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx);
* [byte](https://msdn.microsoft.com/en-us/library/5bdb6693.aspx)[];
* [CloudQueueMessage](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.storage.queue.cloudqueuemessage.aspx);

### Queue Output

Decorate your entry method *out* parameter with \[[Queue](hhttps://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/QueueTriggerAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/QueueOutputFunction.cs)). Below is the list of parameter types that can be used with [Queue] attribute:
* out T, where T is the type that you want to deserialize as JSON into queue message;
* out [String](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx);
* out [byte](https://msdn.microsoft.com/en-us/library/5bdb6693.aspx)[];
* out [CloudQueueMessage](https://msdn.microsoft.com/library/azure/microsoft.windowsazure.storage.queue.cloudqueuemessage.aspx);
* ICollector\ or IAsyncCollector\, where T is one of the supported types;

## Table bindings

For complete documentation on how to use Table parameters in Azure Functions see [official documentaion](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-table).

### Table Input

Decorate your entry method parameter with \[[Table](https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/TableAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/TableInputFunction.cs)). Below is the list of parameter types that can be used with [Table] attribute:
* T, where T is the data type that you want to deserialize the data into;
* Any type that implements [ITableEntity](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.itableentity.aspx);
* IQueryable\, where T is the data type that you want to deserialize the data into;

### Table Output

Decorate your entry method parameter with \[[Table](https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs/TableAttribute.cs)\] attribute ([sample](./samples/DocumentationSamples/TableOutputFunction.cs)). Below is the list of parameter types that can be used with [Table] attribute:
* out \, where T is the data type that you want to serialize the data into;
* out \, where T implements [ITableEntity](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.itableentity.aspx);
* ICollector\, where T is the data type that you want to serialize the data into;
* IAsyncCollector\ (async version of ICollector\);
* [CloudTable](https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.cloudtable.aspx);