Add custom package source to Visual Studio Code - visual-studio-code

Does anybody know how to add custom package source to Visual Studio Code?
E.g. I'd like to add https://www.myget.org/F/aspnet-contrib/api/v3/index.json as a package source and drive these packages through project.json.

To add to the answer, adding a nuget.config in the project solves it for the project. Adding to the root is ok. The config could look like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="MyGet" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
</packageSources>
</configuration>

Another option that worked for me and was actually needed as part of my CI/CD pipeline is to use dotnet nuget add source <repo-url> --name <repo-name>
Simply call that before you call dotnet restore or dotnet build
Reference:
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-nuget-add-source

Note - if your custom package source is password protected, then the credentials should also be part of the config file.
<packageSourceCredentials>
<MyGet> <!--package src name-->
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</MyGet>
</packageSourceCredentials>
And for anyone wondering why the credentials have to be in clear text (which defeats the whole purpose of having credentials in the first place, since this config file will also go into source control).
This is an open issue in dotnet / nuget cli. Ref github issue # 5909 & 1851

You can add a NuGet.config file and specify the package source in there. Some reference docs: https://learn.microsoft.com/en-us/nuget/schema/nuget-config-file

Related

Build Fails Entity Framework - Compilation Error

I have installed entity framework v 6.0.0.0 nuget package.
When building solution locally, it works great without any issues. But, when I check in the code and when the build runs on server, it throws below compilation error.
Primary reference "EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL".
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Any suggestions would be appreciated. Thank you in advance.
The problem you probably have is that your build agent isn't fetching the Nuget package for you.
In my company this is caused by the service account that the build agent is running under not having the corporate proxy set by default.
So firstly you need to run the build agent service as a real user (i.e. not Network Service or Local Service). Then you need to log in to the build agent as that user and set the proxy by hand. Hopefully you can use your own proxy settings.
It's been a long time since I did this, but I also seem to remember that there might be some Visual Studio setting that is off by default about Nuget package restore. So what I generally do when I'm troubleshooting this kind of thing is try to build the solution as the build agent user on the build agent from within Visual Studio. Once you can do that successfully the build should work. It should also work as you add Nuget packages.
If this completely fails to work because you can't get the build agent user through the proxy then you can always create a local Nuget file share and copy the packages to that share, then add the share to your Nuget config. This is configured in the nuget.config file. Mine looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageSources>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="Private package source" value="\\BuildController\nuget\Packages" />
</packageSources>
</configuration>
The second package source is a file share where we put our own packages.
Probably the versions are not updated properly , Uninstall and reinstall entity framework from Nuget Package Manager and make sure that package.config and web.config has the same version that's present in your reference / bin .

How do I include NuGet packages in my solution for .Net Core projects?

With classic .Net projects, if I added a reference to a NuGet package, it would get downloaded to a packages folder and I could check that into source control along with the rest of my code. This allowed any developer to download the code, along with the NuGet packages, without having to set up a package source to separately download the packages. This is not how .Net Core projects work. There does not seem to be a packages folder for the solution, and it is up to each developer to set up the custom package source and download the packages when they get the code. Is there a way to configure the .Net Core project to do like the classic .Net projects did and manage a packages folder?
A lot of NuGet behaviour can be controlled via NuGet.Config files (See this reference for more details)
If you place a NuGet.Config file next to the solution with the following content, you can override the location that the packages will be restored into:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
If the problem is that you'd need to set up additional sources in VS on every machine, you can also add those sources via a NuGet.Config in your repository so VS will pick up the feeds to use when opening a solution:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="CompanyFeed" value="https://my.company.com/private/nuget" />
</packageSources>
</configuration>
If you have no feed to host packages and need to include packages with the solution, you can use a directory containing .nupkg files as well in NuGet.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value=".\NuGetPackages" />
</packageSources>
</configuration>

visual studio code and private nuget

I have a library that comes from a private nuget feed. I have the url and credentials for that but dont know how to connect to the feed properly with visual studio code. I am using dotnetcore framework.
I created a Nuget.Config file in the root of my console application with the feed url and username and password but this didn't seem to pick up the packages from that feed when imputting them in the project.json. Even doing a restore would produce errors.
Does anyone have an example of how they would set up a project to do this? I know it is not normal to have the Nuget.Config file in the project but this is a test project so would not live there once the project got past proof of concept.
My nuget.config looked like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="CustomRepo" value="https://nuget.feed/nuget/" />
</packageSources>
<!-- Used to store credentials -->
<packageSourceCredentials>
<CustomRepo>
<add key="Username" value="something" />
<add key="ClearTextPassword" value="thepassword" />
</CustomRepo>
</packageSourceCredentials>
<!-- Used to disable package sources -->
<disabledPackageSources />
</configuration>
Apoligies this is now resolved and working with the nuget.config file in the root folder. I dont know what I did. Only thing I did was re-type the whole xml but dont know what what would have done. Anyway it is working which is great.

Can I get the location of the packages folder for the current solution when applying an xdt?

I want to install a design time plugin for a visual studio extension (ie runs when using visual studio, and not when the application is running). This requires that I modify the app.config with an entry that points at the dll containing the extension code. The plugin is installed via nuget and if I add this dll to a folder under the project and use a fixed path in the app.config.install.xdt then everything work ok. However what I want is for the xdt to insert a value which points to the dll in the packages folder, where it gets installed via nuget. My problem is that the relative path to the nuget folder is not fixed for the projects. Each project may be in a different folder (several folders deep inside the solution folder) and not just in a direct child of the solution folder, so I want to be able to use some variable in my xdt.
Currently I have this in my xdt:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path=".\SpecflowPlugins\" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
but I want something like this:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<specFlow>
<plugins xdt:Transform="InsertIfMissing">
<add name="NCrunch.Generator" path="$(SolutionDir)\packages\Specflow.Ncrunch.1.0.0\lib\net" type="Generator" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)"/>
</plugins>
</specFlow>
</configuration>
is this possible in xdt? if not is there another option for getting this to munge the app.config correctly when my nuget package installs?
The XDT syntax doesn't allow replacement tokens such as the one you put in your example.
One option I can think of would be to modify the app.config.install.xdt file in your init.ps1 PowerShell script, replacing the tokens for the actual values before the XDT transform is applied by NuGet.
For more information regarding init.ps1 see: Creating and Publishing a Package

Nuget package restore not respecting my repository path configuration?

I have the following setup in my solution's .nuget/Nuget.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<config>
<add key="repositoryPath" value="C:\Xos\packages\" />
</config>
</configuration>
The point of this is to try and make Nuget package restore manageable, and share packages across our many solutions. I then went into my current solution's packages directory and deleted all the packages (so they would instead restore them to C:\Xos\packages). I then closed VS 2012 and reloaded the solution and rebuilt.
However, all my packages are being restored in my solution's packages directory, NOT the directory specified in repositoryPath configuration.
What am I doing wrong?
I had the same issue with VS2015 RC. One of two things suddenly made it work:
(1) I checked-out the project file from source control, and completely closed Visual Studio (not just the solution).
(2) I edited the csproj file and noticed that the entry for packages.config had a <SubType>Designer</SubType> inner tag, which I removed.
I suspect it was the close-down of Visual Studio...
This appears to be a bug and is fixed in 2.3 (which doesn't appear to be released yet).
Until then I modified my Nuget.Targets as described in that bug and using that for now.