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.
publicAzureFunctionService(string name)
- name: string - name of the service used to generate an action cmd.
Creates an instance of this service.
publicAzureFunctionService()
Fields
Instance methods
Configure
Configures a component by passing its configuration parameters.
publicvoid Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
ApplyValidation
Performs a validation.
protectedFunc<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.
publicTask 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.
publicvoid Configure(ConfigParams config)
- config: ConfigParams - configuration parameters to be set.
GenerateActionCmd
Adds ‘.cmd’ to a command name
publicstring 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.
publicIList<AzureFunctionAction> GetActions()
- returns: IList<AzureFunctionAction> - array with supported actions.
GetCommand
Returns a command from the Azure Function context.
This method can be overloaded in child classes.
protectedstring 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.
protectedstring 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.
protectedCounterTiming 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.
protectedvoid 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.
publicbool IsOpen()
- returns: bool - true if the component is open and false otherwise.
OpenAsync
Opens the component.
publicTask OpenAsync(string correlationId)
- correlationId: string - (optional) transaction id used to trace execution through the call chain.
RegisterAction
Registers an action in Azure Function function.
protectedvoid 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.
protectedvoid 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.
protectedvoid 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 virtualvoid 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 abstractvoid 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);
}
);
}
}