locked
Dependency Injection in VSTO and Excel Add-in fail to load RRS feed

  • Question

  • Hi

    I'm working in a VSTO2010 Excel solution and I'm trying to configure DI 

    I'm working in VS2012

    in the app.config I have

    <configSections>
        ...
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
      </configSections>

    ...

    <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="IContext" type="Models.DataInterfaces.IContext, Models" />
        <alias alias="Context" type="DataAccess.Context, DataAccess" />

        <alias alias="IRepositoryFactory" type="DataInterfaces.IRepositoryFactory, Models" />
        <alias alias="RepositoryFactory" type="DataAccess.RepositoryFactory, DataAccess" />

        <alias alias="ILogger" type="Unity.ILogger, Base" />
        <alias alias="Logger" type="Unity.Logger, Base" />

        <container>
          <register type="IContext" mapTo="Context">
            <lifetime type="PerThreadLifetimeManager" />
          </register>
          <register type="IRepositoryFactory" mapTo="RepositoryFactory">
            <constructor>
              <param name="context" />
            </constructor>
          </register>
        </container>
      </unity>

    and I put 

            private void RegisterUnityContainer()
            {
                UnityContainer container = new UnityContainer();
                UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);

                Registry.DependencyLocator = new UnityDependencyLocator(container);
            }

    in ThisAddin.cs , and I call this method in the ThisAddin_StartUp 

    when I try to load the Add-in in Excel 2013, a message says : "Not Loaded, a runtime error occured during the load COM Add-in"

    I'm not sure if the DI is causing the error, or if the Repository pattern implementation is the cause or something else.

    please, can you help me and tell me if the DI is correct configured, or how can I figure out what is causing the Add-in fail to load ?

    thank you

    Tuesday, July 16, 2013 8:37 PM

Answers

  • always in crucial parts where your code integrate with host (startup, shutdown, click events, etc.) put your code in try/catch block and log or show somewhere all exceptions.
    • Marked as answer by Quist Zhang Thursday, July 18, 2013 8:42 AM
    Wednesday, July 17, 2013 1:47 PM
  • Hi Diego,

    I did a quick test on my lab machine (Visual Studio 2012 + Office 2013) and I’m not able to re-produce your issue.

    According to your description, I suggest that you can upgrade your VSTO2010 to VSTO2012. The download link is here.

    Here is my sample code:

    namespace ExcelAddIn
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                var container = new UnityContainer();
                var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);
    
                IContext context = container.Resolve<IContext>();
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
      </configSections>
      <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="IContext" type="Models.DataInterfaces.IContext, Models" />
        <alias alias="Context" type="Models.Context, Models" />
        <container>
          <register type="IContext" mapTo="Context">
            <lifetime type="PerThreadLifetimeManager" />
          </register>
        </container>
      </unity>
    </configuration>

    Regards,

    Jeffrey

    • Marked as answer by Quist Zhang Thursday, July 18, 2013 8:42 AM
    Wednesday, July 17, 2013 3:05 PM

All replies

  • always in crucial parts where your code integrate with host (startup, shutdown, click events, etc.) put your code in try/catch block and log or show somewhere all exceptions.
    • Marked as answer by Quist Zhang Thursday, July 18, 2013 8:42 AM
    Wednesday, July 17, 2013 1:47 PM
  • yes, that is right, but the code isn't reached, at last the debugger don't reach the start.

    So I'm not sure what is happening, bcos no line of code is executed.

    thank you.

    Wednesday, July 17, 2013 2:31 PM
  • Hi Diego,

    I did a quick test on my lab machine (Visual Studio 2012 + Office 2013) and I’m not able to re-produce your issue.

    According to your description, I suggest that you can upgrade your VSTO2010 to VSTO2012. The download link is here.

    Here is my sample code:

    namespace ExcelAddIn
    {
        public partial class ThisAddIn
        {
            private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                var container = new UnityContainer();
                var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                section.Configure(container);
    
                IContext context = container.Resolve<IContext>();
            }
    
            private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisAddIn_Startup);
                this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
            }
            
            #endregion
        }
    }

    App.config:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
      </configSections>
      <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <alias alias="IContext" type="Models.DataInterfaces.IContext, Models" />
        <alias alias="Context" type="Models.Context, Models" />
        <container>
          <register type="IContext" mapTo="Context">
            <lifetime type="PerThreadLifetimeManager" />
          </register>
        </container>
      </unity>
    </configuration>

    Regards,

    Jeffrey

    • Marked as answer by Quist Zhang Thursday, July 18, 2013 8:42 AM
    Wednesday, July 17, 2013 3:05 PM
  • does your add-in loads correctly on other office versions then 2013?
    Thursday, July 18, 2013 7:39 AM
  • In same situation I put IoC registration into the static constructor:

    static ThisAddIn(){

    Bootstrapper.RegisterIoC();

    }

    This code executes before all other codes.

    Friday, November 25, 2016 6:40 AM