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.

Property Injection does not work

See original GitHub issue

I want to use the cache service in the Profile class. I using IocManager.Instance resolve service, but can’t unit test. Then I use property injection IIocResolver it’s always null.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Mardoxxcommented, Aug 14, 2018

So you could do something like this? Not tested it but should work 😄

public class AutoMapperProfileConventionalRegistrar : IConventionalDependencyRegistrar
{
    public void RegisterAssembly(IConventionalRegistrationContext context)
    {
        context.IocManager.IocContainer.Register(
            Classes.FromAssembly(context.Assembly)
                    .BasedOn<Profile>()
                    .WithService.Base()
                    .WithService.Self()
                    .LifestyleTransient()
        );
    }
}

[DependsOn(typeof(AbpAutoMapperModule))]
public class MyAbpModuleModule : AbpModule
{
    public override void PreInitialize()
    {
        IocManager.AddConventionalRegistrar(new AutoMapperProfileConventionalRegistrar());

        Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg =>
        {
            foreach (var profile in IocManager.ResolveAll<Profile>())
            {
                cfg.AddProfile(profile);
            }
        });
    }

    public override void Initialize()
    {
        IocManager.RegisterAssemblyByConvention(typeof(MyAbpModuleModule).GetAssembly());
    }
}

Actually not sure if this would work, mappings are created in AbpAutoMapperModule.PostInintialize(), but the configuration action defers Profile resolution… Give it a try? You get the idea though… I think this is what you were getting at, @maliming ?

0reactions
Mardoxxcommented, Aug 14, 2018

@syx670326 That’d work!

@maliming I don’t think it will having a look at it, I misunderstood what the .Configurators property was for…

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Property injection not working
Both of them result in a cyclic dependency. The third way, when I use property injection in both of them, results in a...
Read more >
Config property injection does not work in super class/base ...
Helidon MP application bootup fails with an exception if the base class of root resource has the config property injected(@Inject @ ...
Read more >
Setter & Property injection not supported/working
The current implementation of DI for module development seems to only support Constructor Injection. A bunch of tutorials mention both Setter ...
Read more >
Dependency injection guidelines - .NET
If a class has many injected dependencies, it might be a sign that the class has too many responsibilities and violates the Single ......
Read more >
[Solved]-Property injection not working-C#
Also I think that it will only work if you will use property injection for both classes and not only in one of...
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