“startIndex 不能大于字符串长度”:使用 Unity nuget 包时出现奇怪错误

问题描述 投票:0回答:1

我使用

Package Manager Console
Unity
package 安装到位于
c:\%SOLUTION_BASE_PATH%\SomeFolder\SomeInnerFolder2\ParticularSolutionFolder
中的 .NET Framework 4.5.2 中的遗留项目中。我使用这个命令:

  Install-Package -ProjectName $ProjectName -Id Unity -Version 2.1.505.2 -Source nuget.org -Verbose -FileConflictAction IgnoreAll -ErrorAction Stop -WarningAction Ignore -IncludePrerelease

一切都很好,运作良好。现在我想更改

repositoryPath
中的
nuget.config
以将所有解决方案的所有包安装到同一个文件夹中。我将以下行添加到
nuget.config
:

<config>
   <add key="repositoryPath" value="c:\%SOLUTION_BASE_PATH%\packages" />
</config>

大多数情况下它也工作得很好,但是

Unity
包给了我以下输出并失败了。

一开始可以正常工作:

    Added package 'Unity.2.1.505.2' to folder 'c:\%SOLUTION_BASE_PATH%\packages' from source '
    https://api.nuget.org/v3/index.json'
    Added reference 'Microsoft.Practices.Unity.Configuration' to project:'X'. The Reference was Resolved To Package (resolvedToPackage):'True', where Reference Path from DTE(dteOriginalPath):'C:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Configuration.dll' and Reference Path from package reference(assemblyFullPath):'c:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.Configuration.dll'.
    Added reference 'Microsoft.Practices.Unity' to project:'X'. The Reference was Resolved To Package (resolvedToPackage):'True', where Reference Path from DTE(dteOriginalPath):'C:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.dll' and Reference Path from package reference(assemblyFullPath):'c:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\lib\NET35\Microsoft.Practices.Unity.dll'.
    Added package 'Unity.2.1.505.2' to 'packages.config'

但随后失败并出现以下错误:

    Executing script file 'c:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\tools\install.ps1'
    Exception calling "Substring" with "1" argument(s): "startIndex cannot be larger than length of string.
    Parameter name: startIndex"
    At C:\%SOLUTION_BASE_PATH%\packages\Unity.2.1.505.2\tools\Utils.psm1:71 char:12
    +     return ($targetPath.Substring($basePath.Length)).TrimStart([Syste ...
    +            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ArgumentOutOfRangeException

然后它只会删除受影响的软件包:

    Removed package 'Unity.2.1.505.2 : CommonServiceLocator [1.0.0, )' from 'packages.config'
    Removed reference 'Microsoft.Practices.Unity.Configuration.dll' from project 'X'
    Removed reference 'Microsoft.Practices.Unity.dll' from project 'X'
    Removed package 'CommonServiceLocator.1.0.0' from 'packages.config'
    ...

并且整个运行失败:

    ...
    Executing nuget actions took 5.91 sec
    The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Cannot bind argument to parameter 'Path' because it is null.

它只发生在我的工作机器上,所以当我在另一台机器上尝试它时,它不会出现任何问题,因此我可以检查失败的包内的内容。失败的文件只有这些行:

    param($installPath, $toolsPath, $package, $project)

    Import-Module (Join-Path $toolsPath Utils.psm1)

    $relativeInstallPath = Get-RelativePath ([System.Io.Path]::GetDirectoryName($dte.Solution.FullName)) $installPath

    $folder = (Join-Path (Join-Path $relativeInstallPath "lib")  "NET35")

    Add-ToolFolder $folder

失败的函数放置在 Utils.psm1 中:

    function Get-RelativePath([System.String] $basePath, [System.String] $targetPath)
    {
        # not a general purpose relative path calculation algorithm
        
        return ($targetPath.Substring($basePath.Length)).TrimStart([System.Io.Path]::DirectorySeparatorChar)
    }

但是,我还不确定这里到底出了什么问题。如有任何帮助,我们将不胜感激。

c# unity-container nuget-package package-manager-console nuget-config
1个回答
0
投票

该消息暗示 $basePath 的(字符串)“长度”比字符串“$targetPath”“更长”,并且使用此“长度”作为 $targetPath 的“偏移量”(通过子字符串)会导致“startIndex”问题。您需要确认相关变量的内容。 (即确认“$targetPath”实际上比“$basePath”更长;显示的代码中没有分配)

公共字符串子字符串(int startIndex);

© www.soinside.com 2019 - 2024. All rights reserved.