Change current web.config with custom Nuget package - c#

I am new at creating custom nuget package. I used NuGet Package Explorer and added new Class Library dll to my local nuget repository. Now I can install it to my new projects.
But Sometimes I need install my custom package and change current project web.config file. Add new key or section. Is this possible?

You can modify the web.config through a web.config.transform file or by using an XML document transform (XDT).
For a .transform you create a web.config.transform file and put in the Content directory of your NuGet package. The web.config.transform file contains the same as a web.config file and it will be applied to the web.config file when you install the NuGet package.
XDTs are more powerful and can do more complicated modifications to the web.config file, such as inserting or removing existing elements. You create a web.config.install.xdt and optionally a web.config.uninstall.xdt file in the Content directory of the NuGet package. These transforms are then run when the NuGet package is installed or uninstalled. An example, taken from the NuGet documentation, is shown below.
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<modules>
<add name="MyNuModule" type="Sample.MyNuModule" xdt:Transform="Insert" />
</modules>
</system.webServer>
</configuration>
The full XDT syntax is documented on the MDSN website

Related

Google Cloud build and private Nuget repository

I have a private NuGet repository created with BaGet.
My software is hosted in Google Cloud where there is also the Git repository with my code.
I have some Cloud build triggers that run the build in the cloud and automatically deploy the applications.
I have created some NuGet package with my projects and now I need to change the reference from code (project reference) to NuGet package reference in my private repository.
Locally all works, but now I need to do the same in my Google Cloud build trigger and I don't know where I can specify the reference to my private repository.
My build script is a yaml file but inside I don't have a reference to the standard NuGet repository and I don't know how I can specify an additional NuGet package repository for some libraries.
NuGet reads configuration from nuget.config files. In fact, it reads from multiple config files. So you can commit a nuget.config to your source code repository, and therefore anyone else who clones your repo (even a CI agent) doesn't need to be configured, it "Just Works".
The part of the config that's relevant to your question is the <packageSources> section, so a minimal config with only package sources defined is:
<configuration>
<packageSources>
<!-- make sure other nuget.config files don't add unexpected sources -->
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="private" value="https://my-server/index.json" />
</packageSources>
</configuration>

How can I configure a local feed for nuget packages in VSCode?

I'm using VSCode with the official C# extension to develop a .NET Core console app on Ubuntu. I'd like to use a private nuget package in this console app. Usually one would add a local feed for nuget packages like described here with nuget init c:\packages \\myserver\packages. I've tried to find some settings in the C# extension which would allow me to configure a local feed for nuget packages but did not find any. Is it possible to do the same with the VSCode + C# extension builtin support?
Alternatively, you can create a file named "NuGet.Config" in the directory where the solution is located and do its contents as follows;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="KetumMyGet" value="https://www.myget.org/F/ketum/api/v3/index.json" />
<add key="KetumMyBaget" value="http://localhost:5000/v3/index.json" />
<add key="TestSource" value="c:\packages" />
</packageSources>
</configuration>
See more here

Change nuget packages folder and use another NuGet.Config in Visual Studio

I want to change nuget package folder but it doesn't work.
I tried very much tricks but not work.
I restart many times VS but not work.
My config:
Visual Studio Community 2019
Windows 10 1909 x64
I have a VS solution folder
MyProjectSln\
HelloWorld\
bin
nuget_packages
NuGet.Config
I want VS studio nuget package manager console use the "NuGet.Config" file and put all packages downloaded in "nuget_packages"
So nuget packages must be in D:\MyProjetSln\HelloWorld\nuget_packages.
Content example of D:\MyProjetSln\HelloWorld\NuGet.Config :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\nuget_packages" />
<!-- I have also tried with repositoryPath but not work -->
</config>
</configuration>
The xml syntax is correct in my case (in my file).
Example, when i execute the command "install-package NUnit" from Package Manager Console, it put downloaded packages in D:\MyProjectSln\packages\ and I don't want that.
Thank you for helping !
Just as this document said, the new nuget.config file must be under the same level directory of the project folder rather than put the file inside the project folder.
In other words, it must be located in at least the solution directory or a higher-level directory.
Note: if you use this way, it will act on all the projects in the same level directory and all the projects in the sub directory.
So you should put the new nuget.config file on the D:\MyProjetSln.
Then modify its content as:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<config>
<add key="repositoryPath" value="HelloWorld\nuget_packages" />
</config>
</configuration>
Then, close VS and then reopen your project to enable that function.
In my side,
At the time of writing this message, what I want is not possible at the moment.
Maybe it will be a feature later.

Azure Devops build not restoring automapper nuget

I am trying to build a .Net project but it doesn't find Automapper v3.3.1.
I have the following build steps:
Use NuGet 4.3.0
NuGet restore
Build solution ...
When running step 3 it gives the error:
Error CS0246: The type or namespace name 'AutoMapper' could not be
found (are you missing a using directive or an assembly reference?)
In the Nuget Restore step I have the
Path to solution, packages.config, or project.json set to the .sln file.
Under Feeds and authentication I checked the option Feeds and in my NuGet.config I left path to NuGet.config empty since I have a global Nuget.config file on the build server under:
C:\Users\user\AppData\Roaming\NuGet\NuGet.config
In this file I have the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<AzureDevOpsFeed>
<add key="Username" value="MyUsername" />
<add key="PAT" value="MyPatKey" />
</AzureDevOpsFeed>
</configuration>
I would assume the build would use the package source https://api.nuget.org/v3/index.json to restore Automapper.
Any idea why this is not working?
The solution quoated from
NuGet not restoring packages on build
I had to go into Source Control and delete all of the files in the
packages folder (except repositories.config) before NuGet would
restore the missing packages. The idea is that you are using package
restore rather than checking your packages in to source control. If it
sees the packages in source control, it won't download them.
I had same problem. Try to delete nuget packages folder and rebuild the solution.

How can I restore NuGet packages from a custom package source when using LINQPAD's lprun.exe?

We use the lprun feature of LINQPAD to run our build scripts; However as we are in a corporate environment we block access to the NuGet.org package source:
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
And instead use a corporate proxy e.g. a custom package source:
<add key="MyProxy" value="https://proxy.server/custom-nuget-repo" />
When developing locally these sources are specified in:
C:\Users\Me\AppData\Roaming\Nuget.config
However LINQPAD is ignoring this file, how can I provide custom package sources when using the lprun to restore NuGet packages referenced?
From newer LinqPad versions this is built in to the LinqPad NuGet manager. Go to QueryProperties (F4) -> 'Add Nuget' -> 'Settings' and new package sources can be added just as easily as in VS.
Adding a new answer here since Google led me to this question first when trying to find the answer.
lprun and LINQPad both use the file %AppData%\LINQPad\NuGetSources.xml for NuGet Package sources, so this is the file you need to edit.
In case this file doesn't exist, here is an example:
<?xml version="1.0" encoding="utf-8"?>
<NuGetSources>
<Source Name="(default)" />
<Source Name="My Packages" URI="https://www.myget.org/F/your-username-here/" UserName="Example" Password="base64encodedstring" Enabled="false" />
</NuGetSources>

Categories