Mono-LibreOffice System.TypeLoadException

我是研究僧i 提交于 2019-12-02 10:02:55

问题


In the past I wrote a C# library to work with OpenOffice and this worked fine both in Windows than under Ubuntu with Mono.
Part of this library is published here as accepted answer.
In these days I discovered that Ubuntu decided to move to LibreOffice, so I tried my library with LibreOffice latest stable release.
While under Windows it's working perfectly, under Linux I receive this error:

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

Usually Mono tells us which library can't load, so I can install correct package and everything is OK, but in this case I really don't know what's going bad.

I'm using Ubuntu oneiric and my library is compiled with Framework 4.0.
Under Windows I had to write this into app.config:

<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
  </startup>
</configuration>

because LibreOffice assemblies uses Framework 2.0 (I think).

How can I find the reason of this error to solve it?
Thanks

UPDATE:
Even compiling with Framework 2.0 problem (as expected) is the same.
Problem (I think) is that Mono is not finding cli-uno-bridge package (installable on previous Ubuntu releases and now marked as superseded), but I cannot be sure.

UPDATE 2:
I created a test console application referencing cli-uno dlls on Windows (they are registered in GAC_32 and GAC_MSIL).

CONSOLE app

static void Main(string[] args)
{
    Console.WriteLine("Starting");
    string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    string doc = Path.Combine(dir, "Liberatoria siti web.docx");
    using (QOpenOffice.OpenOffice oo = new QOpenOffice.OpenOffice())
    {
        if (!oo.Init()) return;
        oo.Load(doc, true);
        oo.ExportToPdf(Path.ChangeExtension(doc, ".pdf"));
    }
}

LIBRARY:

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.view;
using unoidl.com.sun.star.document;
using System.Collections.Generic;
using System.IO;
using System;

namespace QOpenOffice
{
    class OpenOffice : IDisposable
    {
        private XComponentContext context;
        private XMultiServiceFactory service;
        private XComponentLoader component;
        private XComponent doc;

        public bool Init()
        {
            Console.WriteLine("Entering Init()");
            try
            {
                context = uno.util.Bootstrap.bootstrap();
                service = (XMultiServiceFactory)context.getServiceManager();
                component = (XComponentLoader)service.createInstance("com.sun.star.frame.Desktop");
                XNameContainer filters = (XNameContainer)service.createInstance("com.sun.star.document.FilterFactory");
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (ex.InnerException != null)
                    Console.WriteLine(ex.InnerException.Message);
                return false;
            }
        }
    }
}

but I'm not able to see "Starting" !!!
If I comment using(...) on application, I see line on console... so I think it's something wrong in DLL. There I'm not able to see "Entering Init()" message on Init(). Behaviour is the same when LibreOffice is not installed and when it is !!! try..catch block is not executed...
I start to think that mono cannot find LibreOffice CLI libraries...
I used updatedb and then locate to find them, but I always get an empty result; I don't understand, on Windows everything works...

UPDATE 3:
After Hans comment, I've just removed everything but Init() in my library but error remained. So I moved to dynamic

//private XComponentContext context;
//private XMultiServiceFactory service;
//private XComponentLoader component;
//private XComponent doc;
//private List<string> filters = new List<string>();

#region Constructors
public OpenOffice()
{
    Console.WriteLine("Entering Init()");
    try
    {
        var context = uno.util.Bootstrap.bootstrap();
        var service = (XMultiServiceFactory)context.getServiceManager();
        var component = (XComponentLoader)service.createInstance("com.sun.star.frame.Desktop");
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        if (ex.InnerException != null)
            Console.WriteLine(ex.InnerException.Message);
    }
}

and now in console I'm able to see

Starting

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.

This doesn't solve my problem, but helps!!
Question is: why LibreOffice's Linux installation (installation package + SDK) does not install this library?


回答1:


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.




回答2:


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().




回答3:


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



来源:https://stackoverflow.com/questions/10029620/mono-libreoffice-system-typeloadexception

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