1

我正在尝试在我的 ASP.NET MVC 应用程序中获取已安装 NuGet 包的列表,并且我正在使用 NuGet.VisualStudio,如下所示:

var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInstallerServices>();
if (!installerServices.IsPackageInstalled(project, "TemplateBuilder"))
{
    var installer = componentModel.GetService<IVsPackageInstaller>();
    installer.InstallPackage("All", project, "TemplateBuilder", (System.Version)null, false);
}

由于某些原因,我将上面的代码放在Global.asax.cs文件中,但componentModel始终为空。知道有什么问题吗?

4

1 回答 1

0

由于某些原因,我将上面的代码放在 Global.asax.cs 文件中,但 componentModel 始终为空。知道有什么问题吗?

根据IComponentModel Interface,您可以注意到icomponentModelin 中的 InterfaceMicrosoft.VisualStudio.ComponentModelHost.dll是 Visual Studio SDK Reference。因此,如果您使用IComponentModel,则需要 Visual Studio 实例。一般用于Visual Studio Integrate 项目。

此外,当你深入课堂时IsPackageInstalled

bool IsPackageInstalled(global::EnvDTE.Project project, string id);

您需要传递参数project,但 ASP.NET MVC 项目没有任何项目对象。因此,在这种情况下,我们似乎无法在 ASP.NET 项目中使用此方法。

如果您想以编程方式安装 NuGet 包,可以参考如何以编程方式安装 NuGet 包?了解更多详细信息。

于 2017-08-22T04:13:28.887 回答