Mono-LibreOffice System.TypeLoadException

南楼画角 提交于 2019-12-02 05:30:47
Marco

I finally answer my question, but I want to thank @Hans for helping me in finding hidden problem.
As I wrote, trying to run a simple app with Mono using LibreOffice, I got this error

Unhandled Exception: System.TypeLoadException: A type load exception has occurred.
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: A type load exception has occurred.

There was no way to let Mono tell me which was the error, nor my app was writing anything to console so I could understand which part/line raise the error.
A paragraph in Hans answer showed me the way

The .NET framework has a true just-in-time compiler. Mono doesn't, it has an AOT (Ahead Of Time) compiler. In other words, it aggressively compiles all the code in the assembly, not just what is about to be executed.

So when I declare

private XComponentContext context;
private XMultiServiceFactory service;
private XComponentLoader component;
private XComponent doc;

Mono tries to find referenced assemblies right when the app is executed, not when those lines are to be processed! Thinking about this, I decided to move on dynamics.
So I removed variables declaration and used:

//private XComponentContext context;
//private XMultiServiceFactory service;
//private XComponentLoader component;

var context = uno.util.Bootstrap.bootstrap();
var service = (XMultiServiceFactory)context.getServiceManager();
var component = (XComponentLoader)service.createInstance(
    "com.sun.star.frame.Desktop");

Executing my app again, I was able to see console messages I expected and finally, when line var context = ... were processed I got

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'cli_uretypes, Version=1.0.8.0, Culture=neutral, PublicKeyToken=ce2cb7e279207b9e' or one of its dependencies.

So I finally managed to find the problem: LibreOffice in Ubuntu 11.10 does not install CLI interface package and this package has been stripped off from current Ubuntu distribution.
Even if I tried to manually install other older packages, even converting some rpm package, I was not able to use LibreOffice with Mono.
Too bad, even because with previous distribution using OpenOffice there was cli-uno-bridge package doing this job. Hope better in future...
I also tried to post a question at AskUbuntu, but no useful answer were given at this moment.

Usually Mono tells us which library can't load

That's not the problem, it found an assembly with the correct name. The problem is with the content of the assembly. A type that was present in the reference assembly you used isn't actually present in the assembly it found at runtime. Odds that this happens are pretty good if you built on Windows and run on Linux. It is supposed to be prevented by giving the assembly a different [AssemblyVersion] but that rule does get ignored sometimes. Microsoft did for example.

The exception's InnerException should tell you what type is missing. You can solve it by working backwards, use the LibreOffice assemblies you found/installed on the Linux machine as the reference assembly so you know for a fact that there's a match between the reference and the runtime assembly. A compile error on the missing type is now likely. If the missing type is not directly referenced in your program but exist in a helper assembly then you have a bigger problem.

Update: be careful drawing conclusions from your Init() method. The TypeLoadException is generated by the jitter when it compiles IL to machine code. The .NET framework has a true just-in-time compiler. Mono doesn't, it has an AOT (Ahead Of Time) compiler. In other words, it aggressively compiles all the code in the assembly, not just what is about to be executed. So the exception can be raised due to other code in the assembly, not just the code inside Init().

eosphere

Altough it is not easy to figure out, this "type load exception" error can be linked to missing packages on your linux machine.

See my answer on this post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!