Saturday, June 4, 2016

Visual Studio 2015 Update 2 NuGet.config Proxy/Firewall Issues

Overview
My "dotnet restore" does not work with NuGet's v3 package source.  This post analyzes why it was occurring and how it was resolved.

Analysis:
I've been playing around with my nuget.config file recently due to "dotnet restore" issues with a project built with VS 2015 Update 2 dotnetcore project. To start, nuget.config is located here for me: C:\Users\<username>\AppData\Roaming\NuGet\NuGet.Config

When I change this file it affects my VS 2015 builds (i.e. restore)

For the purposes of this post I will be using the command line to do "dotnet restore"s using ConEmu.
I was having issues with a couple of the package sources in nuget.config requiring proxy authentication. Here is a sample ran from the command line:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 250ms
error: Unable to load the service index for source https://dotnet.myget.org/F/dotnet-cli/api/v3/index.json.
error:   Response status code does not indicate success: 407 (Proxy Authentication Required).

So I reverted back to an earlier version of NuGet.Config which is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
  <packageSources>
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

Now when I run "dotnet restore" from the command line I see this:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'
info :   OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver' 807ms
info :   GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'
info :   OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost' 136ms
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
info : Committing restore...
log  : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore completed in 9030ms.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://www.nuget.org/api/v2/
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

I like the command line tool "dotnet restore" and the info it provides.  Using it helps see what NuGet Config files are being used and which feeds are being used. Additionally I'm currently using https://www.nuget.org/api/v2/ as a package source where I should be using v3.

For grins and giggles I added "Hangfire.Core", of which I know nothing about, to my project.json to see how it would react:
"Hangfire.Core": "1.5.6"
It looks like the v2 package source found it but unfortunately my project is not compatible with it so...
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'
info :   CACHE https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
error: Package Hangfire.Core 1.5.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.5.6 supports: net45 (.NETFramework,Version=v4.5)
error: Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
error: One or more packages are incompatible with .NETCoreApp,Version=v1.0.
info : Committing restore...
log  : Lock file has not changed. Skipping lock file write. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore failed in 1219ms.

Errors in C:\<projectPath>\project.json
    Package Hangfire.Core 1.5.6 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.5.6 supports: net45 (.NETFramework,Version=v4.5)
    Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://www.nuget.org/api/v2/
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

What's weird is that v2 works at all for me. Granted I have VS 2010, 2012, 2013, and 2015 on my machine. Here is what I found from the nuget homepage:
•NuGet feed v3 (VS 2015 / NuGet v3.x): https://api.nuget.org/v3/index.json
•NuGet feed v2 (VS 2013 and earlier / NuGet 2.x): https://www.nuget.org/api/v2
So once more into the breach..... (i.e. get v3 to work for my "dotnet restore"s in VS 2015)

I changed the project.json to see if "Hangfire.Core" is supported by dotnetcore in it's current beta release:
"Hangfire.Core": "1.6.0-beta3"

I also modified my NuGet.Config as follows to include the v3 version:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

However when I run "dotnet restore" it fails with the above NuGet.Config file:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
error: Unable to load the service index for source https://api.nuget.org/v3/.
error:   An error occurred while sending the request.
error:   A connection with the server could not be established

Resolution:
Ultimately removing the activePackageSources section fixed the error for me and allowed me to pick up v3 finally as a valid package source. Here is the new NuGet.Config that works:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
  </packageSources>
</configuration>

And here are the command line results, although again "Hangfire.Core" in it's current beta state is not compatible with dotnetcore. But hey, at least the feed is now using v3:
$ dotnet restore
log  : Restoring packages for C:\<projectPath>\project.json...
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json
info :   GET https://api.nuget.org/v3-flatcontainer/hangfire.core/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 168ms
info :   GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 182ms
info :   OK https://api.nuget.org/v3-flatcontainer/hangfire.core/index.json 578ms
info :   GET https://api.nuget.org/v3-flatcontainer/hangfire.core/1.6.0-beta3/hangfire.core.1.6.0-beta3.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/hangfire.core/1.6.0-beta3/hangfire.core.1.6.0-beta3.nupkg 215ms
log  : Installing Hangfire.Core 1.6.0-beta3.
log  : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...
error: Package Hangfire.Core 1.6.0-beta3 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.6.0-beta3 supports: net45 (.NETFramework,Version=v4.5)
error: Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
error: One or more packages are incompatible with .NETCoreApp,Version=v1.0.
info : Committing restore...
log  : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json
log  : C:\<projectPath>\project.json
log  : Restore failed in 5477ms.

Errors in C:\<projectPath>\project.json
    Package Hangfire.Core 1.6.0-beta3 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Hangfire.Core 1.6.0-beta3 supports: net45 (.NETFramework,Version=v4.5)
    Package Owin 1.0.0 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Owin 1.0.0 supports: net40 (.NETFramework,Version=v4.0)
    One or more packages are incompatible with .NETCoreApp,Version=v1.0.

NuGet Config files used:
    C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config
    C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config

Feeds used:
    https://api.nuget.org/v3/index.json
    C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

So I came at this from a couple different directions and ultimately got v3 to work.

To throw a wrench into this solution:
So I continued to have issues connecting to the nuget v2 and v3 package sources via "dotnet restore". Most of the time it doesn’t work, sometimes it works. I’m thinking that perhaps my company has multiple proxy servers out there filtering our requests out to the world. I’m thinking our requests out to the world go through one of these servers. Sometimes we’re going through one server for a while when suddenly we are on another server. My guess is that one of these servers lets us through while another doesn’t allow it. I tested it by simply running "dotnet restore" from the command line over and over again. Mostly it failed but it would sometimes work:

 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 info :  GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver'  
 info :  OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHostResolver' 733ms  
 info :  GET https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost'  
 info :  OK https://www.nuget.org/api/v2/FindPackagesById()?id='Microsoft.NETCore.DotNetHost' 186ms  
 log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...  
 info : Committing restore...  
 log : Writing lock file to disk. Path: C:\<projectPath>\project.lock.json  
 log : C:\<projectPath>\project.json  
 log : Restore completed in 11027ms.  
 NuGet Config files used:  
   C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config  
   C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config  
 Feeds used:  
   https://www.nuget.org/api/v2/  
   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v2/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 error: Unable to load the service index for source https://api.nuget.org/v3/index.json.  
 error:  An error occurred while sending the request.  
 error:  A connection with the server could not be established  
 <user>@<computer> C:\<projectPath>  
 $ dotnet restore  
 log : Restoring packages for C:\<projectPath>\project.json...  
 info :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json  
 info :  OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethostresolver/index.json 189ms  
 info :  GET https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json  
 info :  OK https://api.nuget.org/v3-flatcontainer/microsoft.netcore.dotnethost/index.json 282ms  
 log : Restoring packages for tool 'Microsoft.AspNetCore.Server.IISIntegration.Tools' in C:\<projectPath>\project.json...  
 info : Committing restore...  
 log : Lock file has not changed. Skipping lock file write. Path: C:\<projectPath>\project.lock.json  
 log : C:\<projectPath>\project.json  
 log : Restore completed in 2410ms.  
 NuGet Config files used:  
   C:\Users\<user>\AppData\Roaming\NuGet\NuGet.Config  
   C:\ProgramData\nuget\Config\Microsoft.VisualStudio.Offline.config  
 Feeds used:  
   https://api.nuget.org/v3/index.json  
   C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\  
 <user>@<computer> C:\<projectPath>  
 $  
So we have an issue with our proxy/firewall that resulted in the rigmarole of this post above. Enjoy!

No comments:

Post a Comment