Excel DNA breaking Singleton structure?

Sep 12, 2013 at 2:29 PM
HI

I have written a library using Excel DNA, Excel 2013 and net 4.5.

Running the library under visual studio in debug mode I can see that my code is creating two instances of a singleton. I noticed on the Output window of visual studio in starting excel the following lines

'EXCEL.EXE' (Managed (v4.0.30319)): Loaded 'ExcelDna.DynamicDelegateAssembly'

Shortly after I enter the constructor of the Singleton, which of course I should only ever do once. Holding a break point in the constructor I can see that we enter again. Some 10 seconds after the original entry. The clue I had is that the output window indicated the repeated loading of Excel DNA.

'EXCEL.EXE' (Managed (v4.0.30319)): Loaded 'ExcelDna.DynamicDelegateAssembly'

This is well after the code has got going. Is this likely to be the reason for multiple singletons? Any way I can prevent this?

Thanks
Sean
Coordinator
Sep 14, 2013 at 8:10 AM
Hi Sean,

This happens if your library is loaded multiple times by the Excel-DNA loader - typically once when it is referenced by another library, and once explicitly.
  • What does your .dna file look like?
  • Are you packing into a single-file .xll? If so, you might run the packed .xll in a directory which does not have a copy of your dll file too.
  • If you reference multiple libraries in the .dna file, you might try to re-order them.
Regards,
Govert
Sep 16, 2013 at 11:57 AM
Hi

I think I have fixed it by re-ordering the dna as follows.

<DnaLibrary Name="Oleum Analytics Library" RuntimeVersion="v4.0">
<ExternalLibrary Path="CentralLibrary.dll" LoadFromBytes="false" ExplicitExports="true" Pack="true" />
<ExternalLibrary Path="OleumLibrary.dll" LoadFromBytes="false" ExplicitExports="true" Pack="true" />
<Reference Path="AlgLib.dll" Pack="true" />
<Reference Path="Mono.Security.dll" Pack="true" />
<Reference Path="Npgsql.dll" Pack="true" />

I also discovered that me pausing the code in VisualStudio was also contributing to the problem. When the code is paused, or rather one thread was, I think Excel was reloading the library in another thread, thus doubling up the singleton.

Ps This library is great, thanks for your work and responding to my question.

Thanks
Sean