Skip to content

Azure Functions and Dependency Injector #662

Open
@inirudebwoy

Description

@inirudebwoy

Hi 👋 ,

We are using this awesome tool with many Azure services. One that does not cooperate nicely with DI is Azure Functions.
I'm hoping I'll get some light shed on why they don't cooperate.

Azure Functions is a docker container with a bunch of modules, those modules are instances of Azure Functions, as AWS Lambdas. The code is placed usually in __init__.py
Below is a simple example of how this may look like.

.
├── Dockerfile
├── dependencies.py
├── maintenance_break_cron
│   ├── __init__.py
│   └── function.json
├── promotions_consumer
│   ├── __init__.py
│   ├── function.json
│   └── utils.py

In the dependencies.py is our container

class Container(containers.DeclarativeContainer):

    wiring_config = containers.WiringConfiguration(
        modules=[
            "promotions_consumer"
        ]

    feature_flag_service = providers.Factory(
        FeatureFlagService, provider=feature_flag_provider
      )
    metrics = providers.Singleton(Metrics)

The wiring does not work if the @inject/Provide pair is placed in __init__.py but it does work in utils.py. Our workaround at this point is move the code with DI into submodule (like utils.py). Another one is to access dependencies in this way

container = Container()
metrics = container.metrics()

I'm wondering what is the issue in our case? Is our wiring not right?
Any help would be greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions