ExcelDnaDoc.1.5.1 throwing unexpected exception: System.IO.FileNotFoundException: Could not load file or assembly

70 views
Skip to first unread message

Bart Duijndam

unread,
Jan 16, 2022, 2:20:42 PMJan 16
to Excel-DNA
Greetings all,

I tried to solve this puzzle from scratch by rebuilding my project on a different PC, but so far without success. After creating 80+ routines in my AddIn, I now get an exception from ExcelDnaDoc that I really don't understand. I'm about to step into the ExcelDnaDoc code , but before doing so; maybe someone else has experienced the same problem.

My AddIn can be found here :  
https://github.com/Duijndam-Dev/TopoLib

The code compiles fine, resulting in 32/64 bit AddIns. But ExcelDnaDoc suddenly complains it cannot find "SharpProj". This is strange as (a) the code builds fine so the linker can find it, and (b) the file is properly referenced in "packages.config":

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="ExcelDna.AddIn" version="1.5.1" targetFramework="net472" developmentDependency="true" />
  <package id="ExcelDna.Integration" version="1.5.1" targetFramework="net472" />
  <package id="ExcelDna.IntelliSense" version="1.5.1" targetFramework="net472" />
  <package id="ExcelDna.XmlSchemas" version="1.0.0" targetFramework="net472" />
  <package id="ExcelDnaDoc" version="1.5.1" targetFramework="net472" />
  <package id="Serilog" version="2.10.0" targetFramework="net472" />
  <package id="Serilog.Sinks.ExcelDnaLogDisplay" version="1.5.0" targetFramework="net472" />
  <package id="SharpProj" version="8.2001.106" targetFramework="net472" />
  <package id="SharpProj.Core" version="8.2001.106" targetFramework="net472" />
</packages>


The only thing maybe worth mentioning is that "SharpProj.dll" is not a 'true' C# dll, but written in C+/CLI. But this never caused problems so far . As mentioned earlier; I've already created 80+ function calls to date. My latest steps have been adding logging through Serilog.

The error I receive is as follows:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'SharpProj, Version=8.2001.106.0, Culture=neutral, PublicKeyToken=3a930e679d9a0873' or one of its dependencies. The system cannot find the file specified.
   at System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly assembly, ObjectHandleOnStack retTypes)
   at System.Reflection.RuntimeAssembly.GetExportedTypes()
   at ExcelDnaDoc.Utility.ModelHelper.<>c__DisplayClass3_0.<CreateAddInModel>b__0(ExternalLibrary library)
   at System.Linq.Enumerable.<SelectManyIterator>d__17`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at System.Linq.Lookup`2.Create[TSource](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.GroupedEnumerable`3.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at ExcelDna.Documentation.Models.AddInModel.<get_Functions>d__13.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at CompiledRazorTemplates.Dynamic.RazorEngine_32f74dfa1e1d4378b308c557cac602b4.Execute()
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context, TextWriter reader)
   at RazorEngine.Templating.RazorEngineService.RunCompile(ITemplateKey key, TextWriter writer, Type modelType, Object model, DynamicViewBag viewBag)
   at RazorEngine.Templating.RazorEngineServiceExtensions.<>c__DisplayClass16_0.<RunCompile>b__0(TextWriter writer)
   at RazorEngine.Templating.RazorEngineServiceExtensions.WithWriter(Action`1 withWriter)
   at ExcelDnaDoc.Templates.ViewBase`1.Publish()
   at ExcelDnaDoc.HtmlHelp.Create(String dnaPath, String helpSubfolder, Boolean excludeHidden, Boolean skipCompile, Boolean runAsync)
   at ExcelDnaDoc.Program.Main(String[] args)

Has anyone come across a similar situation ? 
Some help is really appreciated, as I'm not able to create CHM files anymore...

Cheers,

Bart Duijndam

Govert van Drimmelen

unread,
Jan 16, 2022, 4:06:12 PMJan 16
to exce...@googlegroups.com

Hi Bart,

 

I get this error when rebuilding:

 

1>  C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2202,5): warning MSB3270: There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "SharpProj, Version=8.2001.106.0, Culture=neutral, PublicKeyToken=3a930e679d9a0873, processorArchitecture=x86", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.

 

I’m guessing the SharpProj.dll that ends up in the output directory is a 32-bit library.

It might be that on your new machine (or in the new version of ExcelDnaDoc), ExcelDnaDoc is running as a 64-bit process, then can’t load the 32-bit SharpProj.dll.

Then your add-in also won’t work on 64-bit Excel with this SharpProj.dll.

 

I see that the SharpProj.Core package has both 32-bit and 64-bit files.

One would have to dig a bit to understand how the build decides which one to use, and whether the project file has to change.

Maybe because you are using a packages.config or something with the project file.

 

I hope that give you some clue.

I’ll try to poke some more if I get a chance.

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/b3129f3b-2af0-4a8b-ac75-c51682b93601n%40googlegroups.com.

Govert van Drimmelen

unread,
Jan 16, 2022, 4:24:32 PMJan 16
to exce...@googlegroups.com

Hi Bart,

 

The mechanism SharpProj uses to copy the right bitness version of SharpProj.dll to the output (using a runtimes directory in the NuGet package) only works under .NET Core.

See more here NuGet: references to assemblies in runtimes folder not added - Stack Overflow

So for .NET Framework you need something more explicit.

 

My Excel is 64-bit, so that’s the version I want to build and run.

So I’ve fixed up the post-build step like this, to copy the 64-bit version and then run ExcelDnaDoc.

 

REM if $(ConfigurationName)==Release  "$(SolutionDir)batch\publish.bat" $(SolutionDir)  $(ConfigurationName)

copy "C:\Work\Github\Duijndam-Dev\TopoLib\packages\SharpProj.Core.8.2001.106\runtimes\win-x64\lib\net45\SharpProj.dll" "$(TargetDir)"

"C:\Work\Github\Duijndam-Dev\TopoLib\packages\ExcelDnaDoc.1.5.1\tools\ExcelDnaDoc.exe" "$(TargetDir)TopoLib-AddIn.dna" /X

 

After doing this, the ExcelDnaDoc runs fine, and the add-in works beautifully,  including the help file.

 

I’m not sure what the most elegant way is to make the two add-ins (32/64) with the respective SharpProj.dll versions.

But at least it makes sense what you’re seeing.

 

-Govert

Bart Duijndam

unread,
Jan 17, 2022, 3:14:53 AMJan 17
to Excel-DNA

Hi Govert,

Indeed the SharpProj DLL comes with 32-bit and 64-bit versions for both .Net45 an .NetCore.
By default it (somehow) puts the 32-bit .Net45 version in .bin\Release and in .bin\Debug.

That works fine with me, because on my development PC I still work with 32-bit Excel 2019.
That's for backwards compatibility - a bit of a nuisance, but so be it...

I take care of distributing the right version (bitness) of SharpProj.dll at the same time as taking care of the bitness of the XLL file. 
So the end user always gets the right version of the XLL combined with the right version of SharpProj.dll.

SharpProj is quite a nifty bit of C++/CLI software, and it quite mature.
The only downside is that it isn't platform neutral (AnyCPU), so when buillding the project, you will always get a warning message:

There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "SharpProj, Version=8.2001.106.0, Culture=neutral, PublicKeyToken=3a930e679d9a0873, processorArchitecture=x86", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
  
When the right bitness of the SharpProj.dll is used, the warning message can safely be ignored. 
This warning has been there from the beginning, but isn't the cause of ExcelDnaDoc 'playing up' (I believe).

Kind regards,
Bart

Govert van Drimmelen

unread,
Jan 17, 2022, 3:27:24 AMJan 17
to exce...@googlegroups.com

Hi Bart,

 

If you copy the 64-bit version of SharpProj.dll to the output directory before ExcelDnaDoc is called, does it run successfully for you?

 

That seemed to work for me.

Bart Duijndam

unread,
Jan 17, 2022, 3:42:33 AMJan 17
to Excel-DNA
Hi Govert,

Thanks for following up further, much appreciated !

I never had to look at the inner workings of NuGet packages before, 
it all seems quite complicated and not too well documented.

I can see why you need to copy the 64-bit SharpProj.dll, as you are developing with 64-bit Excel. 
That should not be needed in my case, as I'm still on 32-bit Excel 2019 on my development PC.
So that still leaves me a little puzzled on why ExcelDnaDoc doesn't work at the moment. 

But I have a second PC where I run 64-bit Office-365.
I could try building the CHM file on that machine, and see if it works with your 'hack'.

Thanks so far !

Bart

 

On Sunday, 16 January 2022 at 22:24:32 UTC+1 Govert van Drimmelen wrote:

Govert van Drimmelen

unread,
Jan 17, 2022, 3:51:37 AMJan 17
to exce...@googlegroups.com

Hi Bart,

 

I’m copying the 64-bit version of SharpProj.dll to the output directory for two reasons:

  1. ExcelDnaDoc is running as a 64-bit process, and when it loads the add-in assemblies to inspect them for writing out the help file, it needs the 64-it SharpProj.dll file to be the one present in that directory.
  2. I run the 64-bit version of Excel, so need that when running the add-in.

 

While you are right that you need the 32-bit version there for running the add-in, I think you will anyway need the 64-bit version there when ExcelDnaDoc runs. Afterwards you can put back the 32-bit version to run the add-in.

 

On a 32-bit installation of _Windows_, ExcelDnaDoc would run as a 32-bit process.

 

-Govert

 

 

From: exce...@googlegroups.com <exce...@googlegroups.com> On Behalf Of Bart Duijndam


Sent: 17 January 2022 10:43
To: Excel-DNA <exce...@googlegroups.com>

Govert van Drimmelen

unread,
Jan 17, 2022, 4:04:00 AMJan 17
to exce...@googlegroups.com

So I guess for your post-build step you would then have something like this (adjusting the paths as needed)

 

 

REM if $(ConfigurationName)==Release  "$(SolutionDir)batch\publish.bat" $(SolutionDir)  $(ConfigurationName)

copy "C:\Work\Github\Duijndam-Dev\TopoLib\packages\SharpProj.Core.8.2001.106\runtimes\win-x64\lib\net45\SharpProj.dll" "$(TargetDir)"

"C:\Work\Github\Duijndam-Dev\TopoLib\packages\ExcelDnaDoc.1.5.1\tools\ExcelDnaDoc.exe" "$(TargetDir)TopoLib-AddIn.dna" /X

copy "C:\Work\Github\Duijndam-Dev\TopoLib\packages\SharpProj.Core.8.2001.106\runtimes\win-x86\lib\net45\SharpProj.dll" "$(TargetDir)"

 

-Govert

 

 

 

From: exce...@googlegroups.com <exce...@googlegroups.com> On Behalf Of Bart Duijndam


Sent: 17 January 2022 10:43
To: Excel-DNA <exce...@googlegroups.com>

Bart Duijndam

unread,
Jan 17, 2022, 4:54:58 AMJan 17
to Excel-DNA
Hi Govert,

With your help I found a workable solution.

It appears that ExcelDnaDoc only 'likes' 64-bit dll's, while my copy of Excel needs to 'work' with a 32-bit version. 

This leads to the following Post-build event (I only build my CHM file in Release mode)

REM copy 64-bit SharpProj.dll to output folder (for ExcelDnaDoc to work)
if $(ConfigurationName)==Release  copy "D:\Source\VS19\TopoLib\packages\SharpProj.Core.8.2001.106\runtimes\win-x64\lib\net45\SharpProj.dll" "$(TargetDir)"

REM run ExcelDnaDoc
if $(ConfigurationName)==Release  "D:\Source\VS19\TopoLib\packages\ExcelDnaDoc.1.5.1\tools\ExcelDnaDoc.exe" "$(TargetDir)TopoLib-AddIn.dna" /X

REM copy 32-bit SharpProj.dll back to output folder (running 32-bit Excel)
if $(ConfigurationName)==Release  copy "D:\Source\VS19\TopoLib\packages\SharpProj.Core.8.2001.106\runtimes\win-x86\lib\net45\SharpProj.dll" "$(TargetDir)"

REM cleanup RazorEngine files in temp folder.
REM See https://superuser.com/questions/764348/using-wildcards-with-the-rmdir-or-rd-command
if $(ConfigurationName)==Release  cd /D C:\Users\Bart\AppData\Local\Temp
if $(ConfigurationName)==Release  for /f %%i in ('dir /a:d /b RazorEngine_*') do rmdir /s /q %%i
if $(ConfigurationName)==Release  cd /D D:

REM copy project files to publish folder

if $(ConfigurationName)==Release  "$(SolutionDir)batch\publish.bat" $(SolutionDir)  $(ConfigurationName)

So everything is working fine now; it's a bit of a hack, but that's okay ;-)

Cheers,

bart

Govert van Drimmelen

unread,
Jan 17, 2022, 5:01:01 AMJan 17
to exce...@googlegroups.com

Hi Bart,

 

Thank you very much for signing up as a Sponsor @Excel-DNA on GitHub Sponsors!

 

I’m glad you’ve got your project working now - the TopoLib add-in looks very nice.

 

-Govert

 

 

From: exce...@googlegroups.com <exce...@googlegroups.com> On Behalf Of Bart Duijndam


Sent: 17 January 2022 11:55
To: Excel-DNA <exce...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages