VSIX Extension uses 3rd party DLLs unable to load one of the dependency

血红的双手。 提交于 2020-02-24 12:08:59

问题


I am developing an extension to VS2013. Since it will be installed through MSI, I am changing the base directory to installation folder using ProvideBindingPath attribute to package class. But the 3rd party dll reference which will be loaded in runtime is not picking dll from the probed path. Its always looking into Visual studio devenv.exe folder. Is there any way to force the dll to look into my installation folder.

using MD=Microsoft.VisualStudio.Modeling.Shell;

MD.ProvideBindingPath(SubPath = @"")]
public sealed class AutomationCommandPanePackage : Package
    { 

     public AutomationCommandPanePackage()        
     {

        string installationPath = HelperMethods.GetInstallationPath();

        if (string.IsNullOrEmpty(HelperMethods.GetInstallationPath())) return;

        // Change default config file at runtime.
        using (AutomationConfigurationManager.Change(installationPath, "AutomationPro.config"))
        {
            // Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
        }            

        Assembly a = Assembly.GetExecutingAssembly();
        Type type = a.GetType("AutomationCommandPanePackage", true);
        System.Reflection.MemberInfo info = type;
        var attributes = info.GetCustomAttributes(true);

        foreach (var attrib in attributes)
        {
            if (attrib is MD.ProvideBindingPathAttribute)
            {
                ((MD.ProvideBindingPathAttribute)attrib).SubPath = installationPath;
                break;
            }
        }            

回答1:


I have been able to successfully load third party (telerik) assemblies in my extension using below code.

Register to AssemblyResolve event in your Package class constructor

AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

Then in handler load packages as below:

string path = Assembly.GetExecutingAssembly().Location;
path = Path.GetDirectoryName(path);

if (args.Name.ToLower().Contains("telerik.windows.controls.gridview"))
{
       path = Path.Combine(path, "telerik.windows.controls.gridview.dll");
       Assembly ret = Assembly.LoadFrom(path);
       return ret;
}

I have not had any issues with the above approach.




回答2:


I resolved issue using LoadLibrary() from

System.Runtime.InteropServices; 

since my dll to be loaded is a COM iterop dll.

public static class win32 
{ 
[DllImport("kernel32.dll")] 
public static extern IntPtr LoadLibrary(string dllToLoad); 
[DllImport("kernel32.dll")] 
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); [DllImport("kernel32.dll")] 
public static extern bool FreeLibrary(IntPtr hModule);
}

in package.cs I loaded assembly like this

win32.LoadLibrary(Path.Combine(installationPath, "apidsp_windows.dll")); 


来源:https://stackoverflow.com/questions/20001191/vsix-extension-uses-3rd-party-dlls-unable-to-load-one-of-the-dependency

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