C# 尝试以编程方式安装NuGet软件包-package.GetGlobalService返回null

C# 尝试以编程方式安装NuGet软件包-package.GetGlobalService返回null,c#,asp.net-mvc,visual-studio,nuget,componentmodel,C#,Asp.net Mvc,Visual Studio,Nuget,Componentmodel,我正在尝试在我的ASP.NET MVC应用程序中获取已安装NuGet软件包的列表,我使用的NuGet.VisualStudio如下所示: var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); IVsPackageInstallerServices installerServices = componentModel.GetService<IVsPackageInsta

我正在尝试在我的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);
}
var componentModel=(IComponentModel)Package.GetGlobalService(typeof(scocomponentmodel));
IVsPackageInstallerServices installerServices=componentModel.GetService();
如果(!installerServices.IsPackageInstalled(项目“TemplateBuilder”))
{
var installer=componentModel.GetService();
installer.InstallPackage(“全部”,项目,“TemplateBuilder”,(System.Version)null,false);
}
出于某些原因,我将上述代码放在了
Global.asax.cs
文件中,但
componentModel
始终为空。 知道有什么问题吗

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

根据,您可以注意到,
icomponentModel
是Microsoft.VisualStudio.ComponentModelHost.dll中的一个接口,它是Visual Studio SDK参考。因此,如果使用
IComponentModel
,则需要visual studio实例。 通常,它用于VisualStudio集成项目

此外,当您深入了解类
IsPackageInstalled

bool IsPackageInstalled(global::EnvDTE.Project project, string id);
您需要传递参数project,但是ASP.NET MVC project没有任何项目对象。因此,在这种情况下,我们似乎无法在ASP.NET项目中使用此方法

如果您想以编程方式安装NuGet软件包,可以参考以获取更多详细信息

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

根据,您可以注意到,
icomponentModel
是Microsoft.VisualStudio.ComponentModelHost.dll中的一个接口,它是Visual Studio SDK参考。因此,如果使用
IComponentModel
,则需要visual studio实例。 通常,它用于VisualStudio集成项目

此外,当您深入了解类
IsPackageInstalled

bool IsPackageInstalled(global::EnvDTE.Project project, string id);
您需要传递参数project,但是ASP.NET MVC project没有任何项目对象。因此,在这种情况下,我们似乎无法在ASP.NET项目中使用此方法


如果您想以编程方式安装NuGet软件包,可以参考以获取更多详细信息。

Change
var componentModel=(IComponentModel)package.GetGlobalService(typeof(SComponentModel))
to
var-bob=Package.GetGlobalService(typeof(scoComponentModel));var componentModel=(icomonentmodel)bob什么是
bob
?它是空的吗?是的,在那段代码中,bob是空的吗?不,我以前读过。更改
var componentModel=(IComponentModel)包。GetGlobalService(typeof(scocomponentmodel))
to
var-bob=Package.GetGlobalService(typeof(scoComponentModel));var componentModel=(icomonentmodel)bob什么是
bob
?它是空的吗?是的,在那段代码中bob是空的吗?不,我以前读过。根据链接,它需要一个给定的
.csproj
文件,所以它可以在ASP.NET中使用吗?是的,我没有调试该链接中的所有脚本,但我在安装nuget.core包后在C#console应用程序中调试GetNugetPackage方法,并成功地建立它。我想该链接中的所有SCRP都应该在ASP.NET中工作,因为该方法使用NuGet API而不是Visual Studio SDK。它在ASP.NET上不工作,因为它仍然需要项目object@MohammadSadeghipour,很抱歉,您在该链接的接受答案中找到它需要project对象的位置:?根据该链接,它需要一个给定的
.csproj
文件,因此它可以在ASP.NET中使用吗?是的,我没有调试该链接中的所有脚本,但是在安装nuget.core包之后,我在C#控制台应用程序中调试了GetNugetPackage方法,并成功地构建了它。我想该链接中的所有SCRP都应该在ASP.NET中工作,因为该方法使用NuGet API而不是Visual Studio SDK。它在ASP.NET上不工作,因为它仍然需要项目object@MohammadSadeghipour,对不起,您在何处找到此链接中接受的答案中的it需要项目对象:?