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.

First register your services by name.

    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:

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()}'");
        }
    }

Full sample is available AutofacOnFunctions Sample.

Leave a Reply

Your email address will not be published. Required fields are marked *