如何在VsPackage中使用类库

时间:2018-01-10 15:38:25

标签: c# visual-studio-2017 vs-extensibility vspackage fileloadexception

以下方案。我在VS 2017中创建了一个新的VSIX项目。我在那里插入了一个新的工具窗口。另外我创建了一个类库。得到的结构如下: Project Structure

我唯一改变的是

  • 我在ToolWindow1Package类中添加了两个属性,以便在加载解决方案时立即加载

    [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasSingleProject_string)]
    [ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
    
  • Initialize方法

    protected override void Initialize()
    {
        ToolWindow1Command.Initialize(this);
        base.Initialize();
        Class1 class1 = new Class1();
        Console.WriteLine(class1);
    }
    

当我加载解决方案时,无法使用ActivityLog中的以下条目加载包

<entry>
   <record>1396</record>
   <time>2018/01/10 15:16:12.081</time>
   <type>Error</type>
   <source>VisualStudio</source>
   <description>LegacySitePackage failed for package [ToolWindow1Package]Source: &apos;TestExtension&apos; Description: Could not load file or assembly &apos;TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&apos; or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)&#x000D;&#x000A;System.IO.FileLoadException: Could not load file or assembly &apos;TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&apos; or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)&#x000D;&#x000A;File name: &apos;TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&apos;&#x000D;&#x000A;   at TestExtension.ToolWindow1Package.Initialize()&#x000D;&#x000A;   at Microsoft.VisualStudio.Shell.Package.Microsoft.VisualStudio.Shell.Interop.IVsPackage.SetSite(IServiceProvider sp)&#x000D;&#x000A;&#x000D;&#x000A;</description>
   <guid>{673860A0-BAF8-46CB-BDFD-F758C4C6EE3C}</guid>
   <hr>80131044</hr>
   <errorinfo></errorinfo>

尽管如此,库的dll仍在扩展名dll的文件夹中:

Files

我甚至尝试在.vsixmanifest中添加库作为资产而不改变情况。有谁知道这个问题和解决方法?

1 个答案:

答案 0 :(得分:3)

您必须通过向项目添加key.snk文件来强烈签署TestLibrary dll /项目,如错误消息所示。 (项目属性,签名)