V10 - Motor opening error

3

The example that is in github works. Copying that same code to a new project no longer works. Error: "Can not perform runtime binding on a null reference"

To simplify I tested with the code below and the error is the same. I added the references: Erpbs100 and Stdbe100. Trying to open the Platform gives the same error. That is, it does not work on a new project. What will be the example of github that a new project does not have?

ErpBS100.ErpBS motor = new ErpBS100.ErpBS();
motor.AbreEmpresaTrabalho(StdBE100.StdBETipos.EnumTipoPlataforma.tpProfissional, "Testev10", "user", "password");
    
asked by anonymous 29.05.2018 / 22:48

1 answer

1

The code you have displayed has no problem whatsoever, it is really what you should use (as in V9) to open the engine and the company. The problem is that the references are not registered in the system, as was the case with the COM references in earlier versions (for this you would need to manually register all the references / dependencies in the GAC ).

In this case we have two possible solutions:

  • Create an assembly resolve so that all DLLs will load when launching the application
  • If you want to use the 2 option, simply place the following (static) class in your project:

    static class AssemblyResolve
    {
        const string PRIMAVERA_FILES_FOLDER = "PRIMAVERA\SG100\Apl";
    
        public static void Resolve() => AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    
        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(args.Name);
            string assemblyFullName = assemblyFullName = System.IO.Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), PRIMAVERA_FILES_FOLDER), assemblyName.Name + ".dll");
    
            if (System.IO.File.Exists(assemblyFullName))
                return System.Reflection.Assembly.LoadFile(assemblyFullName);
            else return null;
        }
    }
    

    Then, in the Program.cs class, where your application starts, just invoke the Resolve() method of the AssemblyResolve class:

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        AssemblyResolve.Resolve();
    
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    

    Please note that all references you have in the project should have the Copy Location and Specific Version properties of the false .

        
    30.05.2018 / 11:32