New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Functions with Execution Context #652

Closed
stevengopmo opened this Issue Jan 11, 2019 · 4 comments

Comments

2 participants
@stevengopmo
Copy link

stevengopmo commented Jan 11, 2019

hi Simple Injector team,

I'm looking for some clarification on integration with Azure Functions. In #536 you show a static DIConfig class where most of the configuration is done in the static constructor. However, I have a need to use some configuration and that comes from the function Execution context. Is there a way to register the Execution Context in the container somehow for retrieval later?

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

dotnetjunkie commented Jan 11, 2019

When it comes to handling runtime data, there are typically two good ways of handling this, independently of which container you are using:

  1. pass runtime data through method calls of the API or
  2. retrieve runtime data from specific abstractions that allow resolving runtime data.

The second option is valuable in case passing that data through method calls on the public API would cause ripple effects polluting the API and leak information details about that runtime data to consumers that are otherwise uneffected.

Scoping seems an ideal solution to your case, as it helps you pass that runtime data using option 2. Here's an example:

// Defined in an application layer
public interface IContextProvider { Context Current { get; } }

// Composition Root
private class CachedContextProvider : IContextProvider { public Context Current { get; set; } }

// configuration
container.Register<IContextProvider , CachedContextProvider>(Lifestyle.Scoped);
container.Register<CachedContextProvider>(Lifestyle.Scoped);

// Usage
using (AsyncScopedLifestyle.BeginScope(container))
{
    // Set your runtime data at the start of the scope
    container.GetInstance<CachedContextProvider>().Current = value;

    // Resolve an object graph that that depends on IContextProvider and invoke the root object.
    container.GetInstance<SomeHandler>().Handle();
}
@stevengopmo

This comment has been minimized.

Copy link

stevengopmo commented Jan 14, 2019

oh, I think I get it, you are actually SETTING the value during the Scope execution so that when it gets injected in my handler the value will be set. That works but it feels a little dirty but it's because Azure functions is a little dirty

@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

dotnetjunkie commented Jan 14, 2019

it's because Azure functions is a little dirty

Agreed. Azure functions are quite dirty. That's why I prefer making the Azure functions as thin as possible. They become Humble Objects and can, in that case, be considered part of the Composition Root.

@stevengopmo

This comment has been minimized.

Copy link

stevengopmo commented Jan 17, 2019

Thanks, @dotnetjunkie I was able to solve using this technique thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment