1

我正在使用 NuGet 中的 NuGet.targets 文件自动下载 NuGet.exe,然后在我的项目中恢复数据包。

这运行良好,但是在工作中我们有一个代理,并且由于 (407) 需要代理身份验证异常,此方法失败。我修改了目标文件以使用代理详细信息,尽管此方法在应用程序中有效,但在 MSBuild 任务中无效,但代码是相同的。

如果我对代理和我的登录详细信息进行硬编码,它会在我构建解决方案时下载 NuGet.exe 并正确恢复包。问题似乎只是 MSBuild 任务中的身份验证,我完全不知道为什么。我附上了我修改后的代码。

如果有人可以提供帮助,我将不胜感激。谢谢

 <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
    <ParameterGroup>
        <OutputFilename ParameterType="System.String" Required="true" />
    </ParameterGroup>
    <Task>
        <Reference Include="System.Core" />
        <Using Namespace="System" />
        <Using Namespace="System.IO" />
        <Using Namespace="System.Net" />
        <Using Namespace="Microsoft.Build.Framework" />
        <Using Namespace="Microsoft.Build.Utilities" />
        <Code Type="Fragment" Language="cs">
            <![CDATA[
            try 
            {
                OutputFilename = Path.GetFullPath(OutputFilename);

                Log.LogMessage("Downloading latest version of NuGet.exe...");

                using(WebClient webClient = new WebClient())
                {
                  webClient.UseDefaultCredentials = true;
                  webClient.Proxy = WebRequest.GetSystemWebProxy();
                  webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                  webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);

                  return true;
                }
            }
            catch (Exception ex) 
            {
                Log.LogErrorFromException(ex);
                return false;
            }
            ]]>
        </Code>
    </Task>
</UsingTask>
4

0 回答 0