GithubHelp home page GithubHelp logo

christopherhouse / autofac-azurefunctions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from junalmeida/autofac-azurefunctions

0.0 0.0 0.0 97 KB

Autofac implementation of the dependency injection factory for Azure Functions.

License: MIT License

C# 100.00%

autofac-azurefunctions's Introduction

Autofac.Extensions.DependencyInjection.AzureFunctions

Autofac is an IoC container for Microsoft .NET. It manages the dependencies between classes so that applications stay easy to change as they grow in size and complexity. This is achieved by treating regular .NET classes as components.

NuGet

Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.

Get Started in Azure Functions

This quick start shows how to use the UseAutofacServiceProviderFactory integration to help automatically build the root service provider for you.

  • Reference the Autofac.Extensions.DependencyInjection.AzureFunctions package from NuGet.
  • Create your Startup class, following the documentation on how to Use dependency injection in .NET Azure Functions
  • In your Configure method, where you configure the IFunctionsHostBuilder, call UseAutofacServiceProviderFactory(ConfigureContainer) to hook Autofac into the startup pipeline.
  • In the ConfigureContainer method of your Startup class register things directly into an Autofac ContainerBuilder.
  • If you want to use functions declared in referenced projects, add <FunctionsInDependencies>true</FunctionsInDependencies> to the <PropertyGroup> of your main Azure Functions project.

The IServiceProvider will automatically be created for you, so there's nothing you have to do but register things.

[assembly: FunctionsStartup(typeof(Startup))]

public class Startup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder
            .UseAppSettings() // this is optional, this will bind IConfiguration in the container.
            .UseAutofacServiceProviderFactory(ConfigureContainer);
    }

    private void ConfigureContainer(ContainerBuilder builder)
    {
        builder
            .Register(activator =>
            {
                // Example on how to bind settings from appsettings.json
                // to a class instance
                var mySettings = new MySettings();

                var config = activator.Resolve<IConfiguration>();
                config.GetSection(nameof(MySettings)).Bind(mySettings);

                return mySettings;
            })
            .AsSelf()
            .SingleInstance();

        // Register all functions that resides in a given namespace
        // The function class itself will be created using autofac
        builder
            .RegisterAssemblyTypes(typeof(Startup).Assembly)
            .InNamespace("MyNamespace.Functions")
            .AsSelf() // Azure Functions core code resolves a function class by itself.
            .InstancePerTriggerRequest() // This will scope nested dependencies to each function execution

        builder
            .RegisterAssemblyTypes(typeof(Startup).Assembly)
            .InNamespace("MyNamespace.Services")
            .AsImplementedInterfaces()
            .InstancePerTriggerRequest();

    }
}

This is a basic function example, observe that classes and functions are not declared as static:

    public class Function1 : Disposable
    {
        public Function1(IService1 service1, ILogger logger)
        {
            // ...
        }

        [FunctionName(nameof(Function1))]
        public async Task Run([QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")]string myQueueItem)
        {
            await Task.Delay(2000);
            _logger.LogInformation($"C# Queue trigger function processed: {myQueueItem}");
        }

        // ...
    }

Get Help

Need help with Autofac? We have a documentation site as well as API documentation. We're ready to answer your questions on Stack Overflow or check out the discussion forum.

autofac-azurefunctions's People

Contributors

junalmeida avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.