Assembley resolve is not called in a console application

Asked

Viewed 198 times

1

Hello. I have a console application that runs in a different folder than the folder where the ERP PRIMAVERA core assemblies I need to use are located. The problem is that when you run my application, it says it does not find dependencies.

I have an Assembly resolve to indicate the folder where my dependencies are but it never gets called. What’s wrong?

     class Program
        {

            static void Main(string[] args)
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

                EnumTipoPlataforma Plat = EnumTipoPlataforma.tpEmpresarial;
                string Empresa = "Demo";
                string User = "Teste";
                string Password = "";

                ErpBS Erp = new ErpBS();
                StdBETransaccao objStdTransac = new StdBETransaccao();
                Erp.AbreEmpresaTrabalho(Plat, Empresa, User, Password, objStdTransac, "DEFAULT");
            }


            static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                string assemblyFullName;
                System.Reflection.AssemblyName assemblyName;
                const string PRIMAVERA_API_FILES_FOLDER = @"PRIMAVERA\SG100\Apl"; //pasta da API do Primavera
                assemblyName = new System.Reflection.AssemblyName(args.Name);
                assemblyFullName = System.IO.Path.Combine(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), PRIMAVERA_API_FILES_FOLDER), assemblyName.Name + ".dll");
                if (System.IO.File.Exists(assemblyFullName))
                    return System.Reflection.Assembly.LoadFile(assemblyFullName);
                else
                    return null;
            }
}

1 answer

2


The use of Assembly resolve assumes that before any call/use of external references, this is always instantiated in first place, that is, it has to be the first thing that the application does.

In the case of the console application is to put this in the class constructor.

class Program
    {
        // Defenir aqui o resolver.
        static Program()
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }

        static void Main(string[] args)
        {
            EnumTipoPlataforma Plat = EnumTipoPlataforma.tpEmpresarial;
            string Empresa = "Demo";
            string User = "sergio.sereno";
            string Password = "qualquer";

            ErpBS Erp = new ErpBS();
            StdBETransaccao objStdTransac = new StdBETransaccao();
            Erp.AbreEmpresaTrabalho(Plat, Empresa, User, Password, objStdTransac, "DEFAULT");
        }

        static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assemblyFullName;
            System.Reflection.AssemblyName assemblyName;
            const string PRIMAVERA_API_FILES_FOLDER = @"PRIMAVERA\SG100\Apl"; //pasta da API do Primavera
            assemblyName = new System.Reflection.AssemblyName(args.Name);
            assemblyFullName = System.IO.Path.Combine(System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), PRIMAVERA_API_FILES_FOLDER), assemblyName.Name + ".dll");
            if (System.IO.File.Exists(assemblyFullName))
                return System.Reflection.Assembly.LoadFile(assemblyFullName);
            else
                return null;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.