AutofacOnFunctions: Named services support, fixes for breaking changes

A new version of Azure Functions was just released, called 1.0.19. While there are quite some changes in this release, like the fact that Function Apps must have exactly one single language and WebJobs had been updated to 3.0.0-beta8, there are some breaking changes. For complete information about the changes see the announcement.

Breaking Changes

Azure Functions Version 1.0.19 comes with some breaking changes. Extensions cannot be created like they had been before. Have a look at these links:

As AutofacOnFunction relies on the ExtensionConfigContext, there was some stuff to do. Actually, it was possible to fix all breaking changes in the AutofacOnFunctions nuget package itself. While these fixes had been applied, I just introduced a small feature that had been requested by community.

Named Services

Autofac allows for named services. AutofacOnFunctions had been enhanced to support named services in most initutive way. Actually there are only two steps to do.

using Autofac;
using AutofacOnFunctions.Samples.NetStandard.Services.Functions;

namespace AutofacOnFunctions.Samples.NetStandard.Services.Modules
{
    public class ServicesModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<TestIt>().As<ITestIt>();

            builder.RegisterType<TestItByName>().Named<ITestItByName>("registration1");
            builder.RegisterType<TestItByName>().Named<ITestItByName>("registration2");
        }
    }
}

Use the inject atttribute to specify the named instance:

using AutofacOnFunctions.Services.Ioc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

namespace AutofacOnFunctions.Samples.NetStandard.Services.Functions
{
    public static class Function2
    {
        [FunctionName("Function2")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest req, TraceWriter log,
            [Inject("registration1")] ITestItByName testitbyName1,
            [Inject("registration2")] ITestItByName testitbyName2)
        {
            log.Info("C# HTTP trigger function processed a request.");
            return new OkObjectResult($"Hello, this is Function2. Dependency injection sample returns \n'{testitbyName1.CallMe()}', \n'{testitbyName2.CallMe()}'");
        }
    }
}

There are two samples available that work identically:
Net Standard
Net Framework

4 Comments

  1. benjamin said:

    This is really great !

    can you use parameters in the name ?

    function :
    [HttpTrigger(AuthorizationLevel.Anonymous, “get”, “post”, Route = “demo/{myParam}”)]HttpRequest req,
    string myParam,
    [Inject(“named{myParam}”)] IEventValidator validator,

    module :
    builder.RegisterType().Named(“namedA”);
    builder.RegisterType().Named(“namedB”);

    thanks

    November 20, 2018
    • Holger Leichsenring said:

      Hi Benjamin,

      no, this is not supported by now, but it is a quite interesting idea. 🙂
      As the HttpTrigger supports that, just one question to the “[Inject(“named{myParam}”)]”. You registered the type via names namedA and B. Does it mean, myParam is either A or B? There is one problem in resolving the type. I need to check if it is possible to read out all parameters to use myParam for resolving named{myParam}. Will have a look!

      Holger

      November 21, 2018
      • benjamin said:

        hi Holger

        yes, myParam is either “A” or “B”

        there is a problem in my previous comment , we should read :
        module :
        builder.RegisterType ().Named(“namedA”);
        builder.RegisterType ().Named(“namedB”);

        this should really be a great feature !
        thanks

        November 21, 2018
        • benjamin said:

          generics in html :/
          builder.RegisterType EventValidatorA ().Named(“namedA”);
          builder.RegisterType EventValidator ().Named(“namedB”);

          November 21, 2018

Comments are closed.