AzureFunctionService

Abstract service that receives remove calls via the Azure Function protocol.

Implements: IAzureFunctionService, IOpenable, IConfigurable, IReferenceable

Description

The AzureFunctionService class allows you to create a service that receives remove calls via the Azure Function protocol.

Important points

  • This service is intended to work inside an AzureFunction container that exposes registered actions externally.

Configuration parameters

  • dependencies:
    • controller: override for Controller dependency

References

  • *:logger:*:*:1.0: (optional) ILogger components to pass log messages.
  • *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements.

Constructors

Creates an instance of this service.

public AzureFunctionService(string name)

  • name: string - name of the service used to generate an action cmd.

Creates an instance of this service.

public AzureFunctionService()

Fields

_counters

Performance counters.

protected _counters: CompositeCounters

_dependencyResolver

Dependency resolver.

protected _dependencyResolver: DependencyResolver

_logger

Logger.

protected _logger: CompositeLogger

_tracer

Tracer.

protected _tracer: CompositeTracer

Instance methods

Configure

Configures a component by passing its configuration parameters.

public void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

ApplyValidation

Performs a validation.

protected Func<HttpRequest, Task<IActionResult>> ApplyValidation(Schema schema, Func<HttpRequest, Task<IActionResult>> action)

  • schema: Schema - schema used in the validation
  • action: Func<HttpRequest, Task<IActionResult>> - action
  • returns: Func<HttpRequest, Task<IActionResult>> - returned result

CloseAsync

Closes a component and frees used resources.

public Task CloseAsync(string correlationId)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.

Configure

Configures a component by passing its configuration parameters.

public void Configure(ConfigParams config)

  • config: ConfigParams - configuration parameters to be set.

GenerateActionCmd

Adds ‘.cmd’ to a command name

public string GenerateActionCmd(string name)

  • name: string - command name
  • returns: string - command name with ‘.cmd’ added at its end.

GetActions

Get all actions supported by the service.

public IList<AzureFunctionAction> GetActions()

GetCommand

Returns a command from the Azure Function context.

This method can be overloaded in child classes.

protected string GetCommand(HttpRequest context)

  • context: HttpRequest - context.
  • returns: string - returned command from context.

GetCorrelationId

Returns a correlationId from the Azure Function context.

This method can be overloaded in child classes.

protected string GetCorrelationId(HttpRequest context)

  • context: HttpRequest - context.
  • returns: string - returned correlationId from context.

Instrument

Adds instrumentation to log calls and measure call time. It returns a CounterTiming object that is used to end the time measurement.

protected CounterTiming Instrument(string correlationId, string name)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.
  • name: string - method name.
  • returns: CounterTiming - CounterTiming object to end the time measurement.

InstrumentError

Adds instrumentation to error handling.

protected void InstrumentError(string correlationId, string methodName, Exception ex, bool rethrow = false)

  • correlationId: string - (optional) transaction id to trace execution through call chain.
  • methodName: string - a method name.
  • ex: Exception - Error that occured during the method call.
  • rethrow: bool - True to throw the exception.

IsOpen

Checks if the component is open.

public bool IsOpen()

  • returns: bool - true if the component is open and false otherwise.

OpenAsync

Opens the component.

public Task OpenAsync(string correlationId)

  • correlationId: string - (optional) transaction id used to trace execution through the call chain.

RegisterAction

Registers an action in Azure Function function.

protected void RegisterAction(string name, Schema schema, Func<HttpRequest, Task<IActionResult>> action)

  • name: string - action name
  • schema: Schema - validation schema used to validate received parameters.
  • action: Func<HttpRequest, Task<IActionResult>> - action function that is called when the operation is invoked.

registerActionWithAuth

Registers an action with authorization.

protected void RegisterActionWithAuth(string name, Schema schema, Func<HttpRequest, Func<HttpRequest, Task<IActionResult>>, Task<IActionResult>> authorize, Func<HttpRequest, Task<IActionResult>> action)

  • name: string - action’s name
  • schema: Schema - validation schema used to validate received parameters.
  • authorize: Func<HttpRequest, Func<HttpRequest, Task<IActionResult>>, Task<IActionResult>> - authorization interceptor
  • action: Func<HttpRequest, Task<IActionResult>> - action function that is called when the operation is invoked.

RegisterInterceptor

Registers a middleware for actions in Azure Function service.

protected void RegisterInterceptor(string cmd, Func<HttpRequest, Func<HttpRequest, Task<IActionResult>>, Task<IActionResult>> action)

  • cmd: string - command name or pattern.
  • action: Func<HttpRequest, Func<HttpRequest, Task<IActionResult>>, Task<IActionResult>> - action function that is called when middleware is invoked.

SetReferences

Sets references to dependent components.

public virtual void SetReferences(IReferences references)

  • references: IReferences - references to locate the component’s dependencies.

Abstract methods

Register

Registers all service routes in an HTTP endpoint.

This method is called by the service and must be overridden in child classes.

protected abstract void Register()

Examples

public class MyAzureFunctionService : AzureFunctionService
{
    private IMyController _controller;

    public MyAzureFunctionService(IMyController controller) : base("v1.myservice")
    {
        _controller = controller;

        this._dependencyResolver.Put("controller", new Descriptor("mygroup", "controller", "*", "*", "1.0"));
    }

    public override void SetReferences(IReferences references)
    {
        base.SetReferences(references);
        _controller = _dependencyResolver.GetRequired<IMyController>("controller");
    }

    public static void m()
    {
        var service = new MyAzureFunctionService(controller);
        service.Configure(ConfigParams.FromTuples(
            "connection.protocol", "http",
            "connection.host", "localhost",
            "connection.port", 8080
        ));
        service.SetReferences(References.FromTuples(
           new Descriptor("mygroup", "controller", "default", "default", "1.0"), controller
        ));

        await service.OpenAsync("123");
    }

    protected override void Register()
    {
        RegisterAction("get_dummies", new ObjectSchema()
            .WithOptionalProperty("body",
                new ObjectSchema()
                    .WithOptionalProperty("filter", new FilterParamsSchema())
                    .WithOptionalProperty("paging", new PagingParamsSchema())
                    .WithRequiredProperty("cmd", TypeCode.String)
            ),
            async (req) =>
            {
                var correlationId = GetCorrelationId(req);
                var body = AzureFunctionContextHelper.GetBodyAsParameters(req);
                var id = body.GetAsString("id");
                return await this._controller.getMyData(correlationId, id);
            }
        );
    }
}