Skip to content

Support library for .NET developers to build E5E functions.

License

Notifications You must be signed in to change notification settings

anexia/dotnet-e5e

Repository files navigation

dotnet-e5e

Test Coverage

dotnet-e5e is a client library for Anexia e5e - our Functions as a Service offering. With our client library, it's easy to build functions that can scale indefinitely!

Install

With a correctly set up .NET SDK:

dotnet add package Anexia.E5E

Getting started

Creating our application

Anexia.E5E is built on top of the Microsoft.Extensions.Hosting.IHost, so we need to create a new Worker application. This can be easily done on the command line by invoking:

dotnet new worker --name MyNewFunctionHandler
cd MyNewFunctionHandler
dotnet add package Anexia.E5E

Inline handler

With that, we have a Program.cs that we can modify to use our library:

using Anexia.E5E.Extensions;
using Anexia.E5E.Functions;

using var host = Host.CreateDefaultBuilder(args)
	.UseAnexiaE5E(builder =>
	{
		// Register our entrypoint "Hello" which just responds with the name of the person.
		builder.RegisterEntrypoint("Hello", request =>
		{
			var (evt, context) = request;
			// Let's assume we got the name as a plain text message.
			var name = evt.AsText();
			var res = E5EResponse.From($"Hello {name}");
			return Task.FromResult(res);
		});
	})
	.UseConsoleLifetime() // listen to SIGTERM and Ctrl+C, recommended by us
	.Build();

// Finally run the host.
await host.RunAsync();

Class handler

We can also use a handler that satisfies the IE5EFunctionHandler. A very simple adaption of the above handler would look like this:

using Anexia.E5E.Functions;

public class HelloHandler : IE5EFunctionHandler {
	private readonly ILogger<HelloHandler> _logger;
	public HelloHandler(ILogger<HelloHandler> logger) {
		_logger = logger;
	}

	public Task<E5EResponse> HandleAsync(E5ERequest request, CancellationToken token = default) {
		_logger.LogDebug("Received a {Request}", request);

		var (evt, context) = request;
		// Let's assume we got the name as a plain text message.
		var name = evt.AsText();
		var res = E5EResponse.From($"Hello {name}");
		return Task.FromResult(res);
	}
}

Those handlers are automatically registered as scoped. That means, that you can use dependency injection, just like you'd do for ASP.NET Controllers!

using Anexia.E5E.Extensions;
using Anexia.E5E.Functions;

using var host = Host.CreateDefaultBuilder(args)
	.UseAnexiaE5E(builder =>
	{
		builder.RegisterEntrypoint<HelloHandler>("Hello");
	})
	.UseConsoleLifetime() // listen to SIGTERM and Ctrl+C, recommended by us
	.Build();
await host.RunAsync();

Further examples can be found in the examples folder.

Note on startup initialization

Due to the fact that the communication with e5e relies on the IHost to run, any initialization code should not be done before starting the host. It must be done after starting the host, for example by splitting up the RunAsync call like in the example below.

using var host = /* ...omitted for brevity */

// Don't do this or bad things are going to happen.
await SomeExtremelyLongStartupTask();

// Instead do this:
await host.StartAsync();
await SomeExtremelyLongStartupTask();
await host.WaitForShutdownAsync();

Supported versions

Supported
.NET 6.0
.NET 8.0 (without AOT)
.NET 8.0 (with AOT)

About

Support library for .NET developers to build E5E functions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages