• Guides
  • Api Reference
  • FAQ
  • Guides
  • Cross Cutting Concerns
  • IoC Containers
Show / Hide Table of Contents
  • Overview
    • Introduction
    • Getting Started
    • Philosophy
    • FAQ
    • Upgrading to 1.0
  • Triggers
    • HTTP Triggers
      • Overview
      • Routing
      • Responses
      • Authorization
      • Claims Mapping
      • Headers
      • Swagger / OpenAPI
    • Service Bus Triggers
      • Queues
      • Topic and Subscriptions
      • Message Properties
      • Message Sessions
    • Cosmos DB
      • Overview
      • Error Handling
    • SignalR
    • Timer Triggers
    • Azure Storage Triggers
      • Queues
      • Blobs
    • Event Hub Triggers
  • Output Bindings
  • Cross Cutting Concerns
    • Connection Strings
    • IoC Containers
    • Logging
    • Trigger Contexts
    • Validation
    • Serialization
    • Suggested Solution Structure
  • Contributing
    • Compiling
    • Debugging

IoC Containers

By default Function Monkey makes use of the Microsoft ASP.Net Core container as represented by the IServiceCollection and IServiceProvider interfaces. It is possible to use a different IoC container as long as it supports these abstractions - essentially if its compatible with ASP.Net Core.

To use a different container your function app configuration class also needs to implement the IContainerProvider interface. I'll demonstrate how this works using the FunctionAppConfiguration class presented in the getting started guide:

public class FunctionAppConfiguration : IFunctionAppConfiguration
{
    public void Build(IFunctionHostBuilder builder)
    {
        builder
            .Setup((serviceCollection, commandRegistry) =>
            {
                commandRegistry.Register<HelloWorldCommandHandler>();
            })
            .Functions(functions => functions
                .HttpRoute("v1/HelloWorld", route => route
                    .HttpFunction<HelloWorldCommand>()
                )
            );
    }
}

Lets assume we want to use Autofac instead of the Microsoft implementation. First we need to add the required Autofac packages to our project:

Install-Package Autofac
Install-Package Autofac.Extensions.DependencyInjection

Then we update our FunctionAppConfiguration class to also implement the IContainerProvider interface has shown below:

public class FunctionAppConfiguration : IFunctionAppConfiguration, IContainerProvider
{
    public void Build(IFunctionHostBuilder builder)
    {
        builder
            .Setup((serviceCollection, commandRegistry) =>
            {
                commandRegistry.Register<HelloWorldCommandHandler>();
            })
            .Functions(functions => functions
                .HttpRoute("v1/HelloWorld", route => route
                    .HttpFunction<HelloWorldCommand>()
                )
            );
    }

    public IServiceCollection CreateServiceCollection()
    {
        return new ServiceCollection();
    }

    public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection)
    {
        ContainerBuilder containerBuilder = new ContainerBuilder();
        containerBuilder.Populate(serviceCollection);
        IContainer autofacContainer = containerBuilder.Build();
        IServiceProvider serviceProvider = new AutofacServiceProvider(autofacContainer);
        return serviceProvider;
    }
}

Note that if you want to take advantage of container capabilities that are not represented in the Microsoft abstractions then you can register things here in addition to Setup.

  • Improve this Doc
  • 0 Comments
Back to top Copyright © 2018 James Randall