Skip to content

davidatorres/AutofacOnFunctionsLifetimeSample

 
 

Repository files navigation

Autofac On Functions Liftime Scope Sample

Azure Function Autofac Integration, using version 1 available in nuget!

Forked from AutofacOnFunctions

Key updates...

Named Services

First register your services by name.

    public class ServicesModule : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<TestIt>().As<ITestIt>();

            builder.RegisterType<PerDependency>().As<IPerDependency>().InstancePerDependency();
            builder.RegisterType<PerLifetimeScope>().As<IPerLifetimeScope>().InstancePerLifetimeScope();
            builder.RegisterType<PerSingleInstance>().As<IPerSingleInstance>().SingleInstance();

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

Use the inject atttribute to specify the named instance:

public static class Function1
    {
        [FunctionName("Function1")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
            HttpRequest request
            , [Inject]
            ILogger logger
            , [Inject]
            ITestIt testIt
        )
        {
            log.Info("C# HTTP trigger function processed a request.");
            return new OkObjectResult($"Hello, this is Function1. Dependency injection sample returns \n'{testitbyName1.CallMe()}', \n'{testIt.CallMe()}'");
        }
    }

Original samples is available

About

Azure Function Autofac Integration Sample

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%