question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency Injection wiring lost

See original GitHub issue

Hi,

Thanks for the great library!

I have run into an issue where variables are lost that are setup in the constructor via the DI framework. I use StructureMap to wire the class up, this wiring instantiates a field that I need to reference in a method that I am applying advice to. The issue is only prevalent when I place the attribute at the top of the class, when I place the attribute on top of the method in question, all works _fine!

This is the relevant code:

RepositoryClass:

[RepositoryExceptionAdvice] _<-- _settings is null if attribute is placed here_
public class ExampleRepository : IExampleRepository
{
  private readonly ISettings _settings;

  public ComponentTermsRepository(ISettings settings)
  {
    _settings = settings;
  }

  //Advice
  _<-- _settings is fine if attribute is placed here_
  public async Task<List<Example>> List(Guid? ExampleId)
  {
    var test = _settings.TenantId _<- Settings here is null_
  }
}

**Advice Class**
    public class RepositoryExceptionAdvice : Attribute, IMethodAsyncAdvice
    {
        public async Task Advise(MethodAsyncAdviceContext context)
        {
            try
            {
                if (context.IsTargetMethodAsync)
                {
                    await context.ProceedAsync();
                }
            }
            catch (DatabaseException e)
            {
            }
         }
      }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BOENDAGGERcommented, May 31, 2017

Eish, just got your second response, so you can ignore the above comment.

Yes, that was a bit stupid of me blush

What would the recommended way be to ignore the advice for the constructor? Would this be the best approach:

if (!context.TargetMethod.IsConstructor) [proceed]

0reactions
picrapcommented, May 31, 2017

The idea is that a IMethodAsyncAdvice works fine with both sync and async methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wiring not done correctly with PyInstaller #438
Hi, having the same issue. Python 3.8.8; PyInstaller 4.3; dependency-injector 4.35.2. I too have project running when I execute it as a python ......
Read more >
Determining missing dependencies statically when using ...
When using a dependency injection container, missing dependencies are detected when you execute resolve. This is at runtime.
Read more >
The problem with dependency injection frameworks
I gave up on dependency injection frameworks a while ago. Now there's just some "wiring" code somewhere that wires up the components.
Read more >
How to catch and fix a Guice dependency injection issue ...
RunTime Exceptions from Incorrect Dependency Injection Wiring ... Sensei to pick up the missing requestStaticInjection since all our Guice ...
Read more >
Dependency Injection — Back to the Basics | by Luís Soares
Wiring. Let's put it all together. Wiring means the assembling of your app's components — all the DIs happening in one place, forming...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found