Trouble with a project

Add-in Express™ Support Service
That's what is more important than anything else

Trouble with a project
 
Sean Ram




Posts: 27
Joined: 2013-03-13
Hi there. I've been trying to rebuild and register an old project (likely built with earlier version of Addin Express) and now I run into this error:

Severity Code Description Project File Line Suppression State
Error Cannot register assembly "C:\Users\xyz\Documents\my-addons\officeaddons\My.Office.AddIn\bin\Debug\My.Office.AddIn.dll". Exception has been thrown by the target of an invocation. My.Office.AddIn



I am running Visual Studio 2017 running Addin Express 9.4

Do I need to upgrade references in this project to the newer version? And if so, how to do it?
Posted 12 Feb, 2020 15:04:06 Top
Andrei Smolin


Add-in Express team


Posts: 18830
Joined: 2006-05-11
Hello Sean,

You should be aware that by design, an instance of the module is created under the following circumstances:
- When the project is being built;
- When the Office extension is being registered or unregistered;
- When Office loads the extension.

The restrictions below are a direct result of these facts. To bypass the restrictions, you should execute custom code in an event of the module, not in the constructor. This guarantees that your code is only executed when within the host Office application.

Adding custom code to the module's constructor is not recommended

When the module is created in the scenarios above, no Office-related objects, properties, methods or events are available. Also, the Add-in Express related properties are not initialized yet. If the add-in code is not prepared for this, the add-in installer may fail.

Initialize class-level variables defined on the module in an event of the module

This restriction applies to complex-type variables only. Consider a complex-type variable declared on the class level of the add-in module. Here's how the declaration of such a variable might look like; the declaration also contains an initializer:
- C#: MyType myVar = null;
- VB.NET: Dim myVar as MyType = Nothing

When an instance of the module is created, in order to initialize such a variable, the .NET Framework loads the assembly containing the variable's type. If that assembly doesn't expect to be loaded in the scenario above, it fails (say, because of an improper context) and, consequently, the add-in installer may fail.

To avoid this, you should declare the variable with no initializer:
- C#: MyType myVar;
- VB.NET: Dim myVar as MyType

Use an event of the add-in module to initialize class-level variables and to run any initialization code. The very first event that the add-in module recieves is either OnRibbonBeforeCreate or AddinInitialize. Which of them fires first depends on the Office application, on its version, and also on the way you start it.


Andrei Smolin
Add-in Express Team Leader
Posted 13 Feb, 2020 03:00:44 Top