我正在使用Visual Studio通用项目系统创建项目类型,并连接了NuGet程序包管理器。但是,当我安装软件包时,它失败并显示以下错误:
Attempting to gather dependency information for package '[PackageName]' with respect to project '[ProjectName]', targeting '.NETFramework,Version=v4.7'
Gathering dependency information took 3.23 ms
Attempting to resolve dependencies for package '[PackageName]' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package '[PackageName]'
Resolved actions to install package '[PackageName]'
Retrieving package '[AssemblyName] [Version]' from '[Source]'.
Adding package '[PackageName]' to folder '[ProjectDirectory]\packages'
Added package '[PackageName]' to folder '[ProjectDirectory]\packages'
Install failed. Rolling back...
Package '[PackageName]' does not exist in project '[ProjectName]'
Removing package '[PackageName]' from folder '[ProjectDirectory]\packages'
Removed package '[PackageName]' from folder '[ProjectDirectory]\packages'
Executing nuget actions took 1.17 sec
Failed to add reference to '[AssemblyName]'.
Unable to find a type of reference that is appropriate for this file: "[ProjectDirectory]\packages\[PackageName]\lib\net46\[AssemblyName].dll".
Time Elapsed: 00:00:01.2728560
========== Finished ==========
我反编译了NuGet程序集并对其进行了调试,以发现该行NuGet.PackageManagement.VisualStudio.VsMSBuildProjectSystem.AddReferenceAsync
上的reference = References.Add(assemblyFullPath);
中发生错误。因此,我尝试执行相同的代码,并在尝试“系统”和“ [FullAssemblyPath]”时遇到相同的错误。
Unable to find a type of reference that is appropriate for this file: [Assembly].
VsMSBuildProjectSystem Source Code
我尝试调试一个正常的Visual Basic项目,发现它执行的命令稍有不同,References3.AddFiles(new[] { assemblyFullPath }, out var referencesArray);
,这是因为References对象可以转换为References3。事实证明,Visual Basic项目是VSLangProj80.VSProject2
类型,而我的自定义项目是VSLangProj.VSProject
类型。
我还注意到,在我的自定义项目类型中,引用未解析,因此它们旁边带有黄色的感叹号。我相信这是问题的根源。我不确定Visual Studio如何解析引用,但是通过调试,我发现使用了MSBuild目标ResolveAssemblyReferences
。当我在Visual Basic项目文件中覆盖它时,引用也显示黄色惊叹号,但是即使当我的自定义项目返回相同的信息时,引用仍然无法解析。必须使用其他东西。
我想确认它是否可以工作,但是要覆盖该功能,我发现MSBuildNuGetProjectSystemFactory
基于硬编码项目类型GUID创建了VsMSBuildProjectSystem
类,它是一个内部类。它似乎是通过导出实现MSBuildNuGetProjectProvider
的{{1}}来连接的。我想用自己的提供程序覆盖它,并使用此代码返回一个自定义实现,但不幸的是,它从未实例化。 MSBuildNuGetProjectProvider Source Code
INuGetProjectProvider
因此,我只是对其进行了修改,并使用了反射将我的类添加到<Export(GetType(INuGetProjectProvider))>
<Name("MSBuildNuGetProjectProvider")>
<Microsoft.VisualStudio.Utilities.Order(After:="ProjectJsonProjectProvider")>
Friend Class MyNuGetProjectProvider : Implements INuGetProjectProvider
<ImportingConstructor>
Public Sub New(threadingService As IVsProjectThreadingService)
Me.New(AsyncServiceProvider.GlobalProvider, threadingService)
End Sub
Public Sub New(vsServiceProvider As Microsoft.VisualStudio.Shell.IAsyncServiceProvider, threadingService As IVsProjectThreadingService)
End Sub
Public ReadOnly Property ProjectType As RuntimeTypeHandle Implements INuGetProjectProvider.ProjectType
Get
Return GetType(MSBuildNuGetProject).TypeHandle
End Get
End Property
Public Async Function TryCreateNuGetProjectAsync(project As IVsProjectAdapter, context As ProjectProviderContext, forceProjectType As Boolean) As Task(Of NuGetProject) Implements INuGetProjectProvider.TryCreateNuGetProjectAsync
Dim projectSystem As New TestProjectSystem(project, context.ProjectContext)
Await projectSystem.InitializeProperties()
Return New MSBuildNuGetProject(projectSystem, context.PackagesPathFactory.Invoke, project.ProjectDirectory)
End Function
End Class
中,我的自定义类被执行,覆盖了添加引用的方法,而不执行任何操作。添加NuGet包成功,但是当然不会将引用添加到项目中。 MSBuildNuGetProjectSystemFactory Source Code
MSBuildNuGetProjectSystemFactory
所以我仍然不知道Visual Studio如何解析程序集,我希望一旦修复,NuGet程序包管理器也将正常工作。我以为它可能使用Friend Class TestProjectSystem : Inherits VsMSBuildProjectSystem
Private mProjectAdapter As IVsProjectAdapter
Public Sub New(a As IVsProjectAdapter, c As INuGetProjectContext)
MyBase.New(a, c)
Me.mProjectAdapter = a
End Sub
Public Overrides Async Function AddReferenceAsync(referencePath As String) As Threading.Tasks.Task
'Dim project = TryCast(Me.mProjectAdapter.Project.Object, VSLangProj.VSProject)
'project.References.Add(referencePath)
End Function
End Class
,但我永远无法通过MEF实例化我的课程。我想知道它是否是在Visual Studio非托管代码中处理的,并且不存在扩展点,因为它是正在创建的不同项目类。
如果是这种情况,那么我想我可以使用IVsDesignTimeAssemblyResolution
接口重写添加引用,并通过直接编辑XML文件来实现。但是为什么从未通过MEF实例化我的INuGetProjectProvider
实现?我尝试将Order属性更改为0和1000。VisualStudio MEF是否在自己的程序集中具有Nuget的作用域,所以我的类永远不会被拾取?