Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

New Custom Module no Injected ILogger #7436


User avatar
0
andmattia created

I've create a new module for exporting and I make my module dependent from it with DependOn attribute and it work as aspected but the Ioc not inject the ILogger.

Below some part of my code. So If I don't call the Initialize() on creator ILogger still remain to NullLogger and the Initialize() never call from Ioc What I'm wrong?

public abstract class EpPlusExcelExporterBase<T> : ITransientDependency, IDisposable, IShouldInitialize
{
...
        public ILocalizationManager LocalizationManager { get; set; }
        public ILogger Logger { get; set; }
        ....
        
        protected EpPlusExcelExporterBase()
        {
            AppFolders = NullAppFolder.Instance;
            Logger = NullLogger.Instance;
            LocalizationManager = NullLocalizationManager.Instance;

        }

        protected EpPlusExcelExporterBase(string localizationName)
        : this()
        {
            LocalizationSourceName = localizationName;
            Initialize();
        }
        
         public virtual void Save(ExcelPackage excelPackage, FileDto file)
        {
            
            var filePath = Path.Combine(AppFolders.TempFileDownloadFolder, file.FileToken);
            excelPackage.SaveAs(new FileInfo(filePath));

        }
        
        public void Initialize()
        {
            AppFolders = IocManager.Instance.IocContainer.Resolve<IAppFolders>();
            Logger = IocManager.Instance.IocContainer.Resolve<ILogger>();
            LocalizationManager = IocManager.Instance.IocContainer.Resolve<ILocalizationManager>();
        }
}

10 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    You don't need to call the Initialize method, the framework will call it automatically.

    When the framework calls the Initialize method, your property injection should be working.

  • User Avatar
    0
    andmattia created

    mmm but I try to put a break point but it's never call ... are you sure?

    So the big question is why my class ha not ILogger correctly initialize...

  • User Avatar
    0
    maliming created
    Support Team

    I am very sure.

    Can you share more code? It would be best if I could reproduce your problem.

  • User Avatar
    0
    andmattia created

    For sure. I add my module file

    
        [DependsOn(typeof(AbpKernelModule))]
        public class AbpExportingModule : AbpModule
        {
            public override void PreInitialize()
            {
                IocManager.Register<IAppFolders, AppBaseFolders>();
            }
    
            public override void Initialize()
            {
                //IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
                
                IocManager.RegisterAssemblyByConvention(typeof(AbpExportingModule).GetAssembly());
            }
    
            public override void PostInitialize()
            {
                //AppFolders = Dependency.IocManager.Instance.IocContainer.Resolve<IAppFolders>();
                //Logger = Dependency.IocManager.Instance.IocContainer.Resolve<ILogger>();
                //LocalizationManager = Dependency.IocManager.Instance.IocContainer.Resolve<ILocalizationManager>();
            }
        }
    
  • User Avatar
    0
    maliming created
    Support Team

    @andmattia

    Can you share the complete code of your project? Or simply use the Demo project to reproduce your problem.

    At present, the reason for the problem cannot be analyzed based on the above code.

    My email: [email protected]

  • User Avatar
    0
    andmattia created

    @maliming

    I send via email my complete module source code

  • User Avatar
    0
    maliming created
    Support Team

    Have viewed it remotely, if there is any problem, please continue to feedback.

  • User Avatar
    0
    andmattia created

    Hi maliming

    I invastigate on my code like your suggestion and now the ILogger it will be injected automaticly if I derive on application a class from AbpExport module like the code below

    public class VatCheckExporter : ExcelExporter<VatChekcExcelOutDto>, IVatCheckExporter
        {
        ...
        }
    

    Probably I need to extend my module to do exact what I need. So I see that is possible to inject via IocManger, but is not a god option for testing in this way

    using (var excelExport = IocManager.Instance.ResolveAsDisposable<ExcelExporter<VatChekcExcelOutDto>>())
                {
                ...
                }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @andmattia

    Is this issue resolved for you ?

  • User Avatar
    0
    andmattia created

    Hi @ismcagdas

    all information is enough to solve my issue so I close the ticket.

    thank for you support