logeeks
logeeks

Reputation: 4979

objects being null even after calling SatisfyImportOnce - MEF

In my program I am using the following statements in program.cs

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var init = new Initializer();
    var container = new CompositionContainer(new DirectoryCatalog(Environment.CurrentDirectory));
    container.ComposeExportedValue(string.Empty);
    container.SatisfyImportsOnce(init);
if (init.PreleminaryCheck())
            {
                Form1 frm = new Form1();
                container.SatisfyImportsOnce(frm);
                Application.Run(frm);
            }

I am exporting class like below

[Export(typeof(DatabaseMaster))]
    public class DBManager : DatabaseMaster
[ImportingConstructor]
        public DBManager(string filepath = "")

        {
            DbPath = filepath;
        }

The problem is i am only getting the variable initialized in the Initializer class, In the form class the instance of DBManager is always null. I tried calling SatisfyImportOnce on Form1 variable but no luck.

Upvotes: 0

Views: 93

Answers (1)

logeeks
logeeks

Reputation: 4979

I solved this problem by moving the member function call from the constructor to the form load method,

Upvotes: 1

Related Questions