.net 6 BuildServiceProvider is missing - .net-core

net 6, create console application with 'minimal template'. try add DI but BuildServiceProvider is missing. I try google about how migrate ServiceCollection but did not find nothing.
var services2 = new ServiceCollection();
var serviceProvider = services2.BuildServiceProvider();

In my idiotic case, I was using Microsoft.Extensions.DependencyInjection.Abstractions instead of Microsoft.Extensions.DependencyInjection. I didn't notice at first because ServiceCollection, AddSingleton etc were available using the first NuGet as well. Icon and author are the same for both NuGets.

The following two steps fixed it for me (VS2022, .NET Standard 2.0):
Make sure all your projects are referring to the same version of Microsoft.Extensions.DependencyInjection. At the time of this writing, the latest version is 7.0.0. You can use Consolidate tab in solution's Nuget Configuration page to achieve this.
Go to project's Nuget Configuration page (and not Solution's Nuget Configuration page) by right-clicking the problematic project and choosing Manage Nuget Packages. Therein, uninstall Microsoft.Extensions.DependencyInjection and then re-install it (again making sure you're using the same version as other projects in the solution).
Rebuild the solution after this and things should look good.

Related

How to download NuGet packages programatically from .NET Core, .NET Standard projects

I want to download Nuget packages programmatically from the .NET core/.NET Standard projects
We have some console apps that are tools NuGet packages and I want to download those and start them as a background process.
Best way to achieve it is by referring NugetDownloader Nuget package in Project and use it to download any package programmatically(in dotnet core as well)
Install-Package NugetDownloader
Source code and help guide on the same is available at :
https://github.com/paraspatidar/NugetDownloader
Here is a quick sample on how to achieve it :
string packageName="Newtonsoft.json";
string version="10.2.1.0"; \\optional
\\initilize NugetEngine from NugetDownloader namespaces
NugetEngine nugetEngine = new NugetEngine();
nugetEngine.GetPackage(packageName, version).Wait();
sample client is also available at https://github.com/paraspatidar/NugetDownloader/tree/master/NugetDownloaderTestConsole
Alternately In case if you want to build your Nugetdownloader engine from scratch , then you might also refer https://github.com/Azure/azure-functions-host/blob/dev/src/WebJobs.Script/Description/DotNet/PackageManager.cs as it has something similar implementation , but that too much of code understanding and extraction.

Microsoft.Practices.EnterpriseLibrary.Common

I have been using previous version of unity extension. Now I have to upgrade my project with a newer version of Unity Extension.
I have code in my previous version as :-
Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.UnityContainerExtensions.AddNewExtensionIfNotPresent<Microsoft.Practices.Unity.InterceptionExtension.Interception>(Context.Container);
this.Context.Container.AddExtension(new ContainerRegistration());
But in new version Unity is not found under Configuration, has it moved to a new method?
The Unity building block can be installed with NuGet. You can get more informations here, or by opening the package manager in Visual Studio and searching for Unity.
The class you're looking for are now in another namespace. For example, UnityContainerExtensions is in Microsoft.Practices.Unity. I'd recommend adding a using on top of the classes where you're dealing with the UnityContainer to simplify the reading.

Nuget does not update Package References in ASP.NET website

About: I have a ASP.NET website (not Web project) with 3 class library projects in the solution. Earlier I was using SVN but now Git is used source management. I have installed the git locally on a computer (used as server) and using it for merging the source code from other developers. Also, I am using Visual studio 2015 community edition which provides the tools to work with git.
Problem: After cloning the project from the master repository, I build the project to run it. Building the project shows a dialog box saying "Package Restore is in progress". This process creates a folder named "Packages" and that folder includes every package listed in the packages.config file. But after restoration completes, the project throws the following exception:
This exception shows for each package (Autofac here).
The type or namespace name 'Autofac' could not be found (are you missing a using directive or an assembly reference?)
Work around To Solve this problem, I need to uninstall each package and installed it again and problem is solved. This thing I need to do again and again for each developer machine, which is frustrating and time consuming too.
Does anyone has faced the same problem working with Nuget, git and website in ASP.NET.
I faced a problem like this before. In my case, the reason was that I changed the project path (moved the project to another directory), and the path of the packages directory (that contains the NuGet packages) was stored in the csproj file for the old path, that is VS cannot restore NuGet packages. The solution for this was to edit the csproj manually and make it referring to the correct new packages path.
If this doesn't work for you, you can still use your workaround, but using the following PowerShell command (in NuGet Console) for simplicity:
Update-Package -reinstall -Project Your.Project.Name
Note: project name doesn't contain csproj extension, just the project name
It is good practice to not put third party packages into source control. It bloats your repository (even on a large web application, the size of the external packages will massively out weigh your code).
If NuGet package restore is slow, you could look at using a local cache (this can be as simple as a shared folder) or a better internet connection.
That said, you should only have this problem once per machine. While the packages are downloading you could be giving the new team member an overview of the design…
Make sure that all of your projects are using the same target framework, when this isn't done you can often get the
type or namespace [name] could not be found
warning.
To do this, right click each of the projects in the solution explorer > Properties > Application tab > Target Framework. They should all be the same or there will be incompatibilities between the references in your projects. Here's a question regarding this, hopefully this helps.
The main reason is there no Autofac reference in packages.config file.
When you see on the screen Restore packages message box this mean that nuget package manager is trying to install all the packages which are missing in the package folder.
Try to do this step:
In Visual Studio Solution Explorer pick the project and via context menu pick Unload project
After project was unloaded via context menu pick Edit your project
Go to section group and find you Autofac Reference section
If HintPath doesnt looks like ..\packages\Autofac.4.1.1\lib\net45\Autofac.dll (Actual for 4.1.1 version) remove the Autofac reference Item
Save csproj file and reload project
Install Autofac via NuGet Package manager
Commit and push changes to git repository
Had the same problem with VS 2019. In ASP.NET, packages are updated via the .refresh files that appear in your bin folders. If these aren't checked into Git, they wouldn't be copied down to your cloned repro.
I added the .refresh files for all of my package dlls, made sure the versions and paths in the .refresh files were correct, and now everything updates as expected.

Referencing class library in ASP.NET 5 - dependency issues

My custom class library (targeting .NET 4.5.1) references Newtonsoft.Json 8.0.2 (the latest version to date). I've added this class library as a reference to a new ASP.NET 5 Web API project.
From the Error List output:
Error NU1002 The dependency Newtonsoft.Json 1.0.0 in project MyProject does not support framework DNXCore,Version=v5.0.
That part seems somewhat self-explanatory, however, I couldn't understand why it was changing my referenced version to 1.0.0.
From the References --> DNX 4.5.1 --> MyClassLibrary Node:
NU1007 Dependency specified was Newtonsoft.Json >= 8.0.2 but ended up with Newtonsoft.Json 1.0.0.
I've tried removing/re-adding the references several times, all to no avail. I did notice that several of the ASP.NET libraries use Newtonsoft.Json 6.0.6 -- I'm not sure if this has anything to do with it.
If I navigate to the wrap folder --> Newtonsoft.Json --> json file, and replace "Newtonsoft.Json 1.0.0-*" with "Newtonsoft.Json 8.0.2-*", it fixes the references for the DNX 4.5.1 node.
However, errors still show afterwards saying that Newtonsoft.Json 8.0.2 referenced in my class library does not support DNXCore 50.
I'm at a loss as to what to do. Any help would be greatly appreciated.
Answer to my issue:
Thanks to Oleg's response I was able to make it work -- however there were two hurdles. One was the "wrap" folder that was created for Newtonsoft.Json. In the root of my solution was a "wrap" folder that contained a folder called Newtonsoft.Json. Apparently(?) that folder shouldn't have been created (how it was created I have no idea). Once removing that, I was able to add a reference to my custom class library without any errors referencing Newtonsoft.Json.
However when I began trying to use my custom assembly as in code, I would get an error about DNX50 not being supported -- which sort've makes sense because my custom referenced assembly was a .NET 4.5.1 class library.
This GitHub link has many people experiencing a similar issue
However, I managed to get everything to build and errors to go away by removing targeting for DNXCore50. This worked for me because I was just wanting to share some POCO data models between a Web API and WPF project. I'm not interested in targeting DNXCore at the moment.
I'm looking forward to the future of ASP.NET, but the newness is a bit challenging, at least for me.

Updating Files in Existing Nuget Package

I've got a multi-project ASP.NET Web Forms Application solution. I need to share a master page (3 files), some user controls and some images, scripts and CSS files out to the other projects in the solution.
I have already created a package using the NuGet Package Explorer per the documentation:
http://docs.nuget.org/docs/creating-packages/using-a-gui-to-build-packages
My current problem is this: I have updated the shared files in the root project and now I want to update the package before pulling it into the other projects (package currently in a local folder on my dev machine). How do I do this?
If anyone has some getting-started-quickly NuGet links, please share as the official docs just aren't doing it for me.
create the package again with a new version aka if the orginal is 1.0 make this 1.1 and NuGet will pick up the update.
The NuGet file is ultimately just a zip file. You can update entries using anything that can update a zip file. Such as something like
using System.IO.Compression;
using System.IO.Compression.FileSystem;
// EG: AddOrUpdateZipEntry("mypackage.nupkg", "my.dll", "bin/my.dll")
void AddOrUpdateZipEntry(string zipFilePath, string contentsFilePath, string entryPathInZip)
{
using (var zip = ZipFile.Open(zipFilePath, ZipArchiveMode.Update))
{
zip.GetEntry(entryPathInZip)?.Delete(); // Remove any existing entry first.
zip.CreateEntryFromFile(contentsFilePath, entryPathInZip);
}
}
Are you asking what you need to do to update projects that are already using the package?
The key thing in this kind of scenario is simply versioning. The new version of the file will be sub'd out. Bundle up the package again with a new version number and then run Update-Package from the package manager console in VS.
You may also want to consider a couple of discreet packages, rather than one straight one. If you want to update a couple images or a CSS file, but not the MasterPage, it might work best to have a couple smaller ones.
Use the PM Explorer (from the post you mentioned) to open a couple packages from the NuGet main repository, in particular, jQuery 1.5.1 and 1.6.x and have a look, as these will be doing very similar things. No real magic needed!
Cheers.
I've already done it before. You just have to increment the version of your package, inside metadata of .nuspec file.
In my case, my packages names are '[name].[version].nupkg' so I save my new package as '[name].[version +1].nupkg' as well.
The update apears in 'Manage nuget packages' updates section.

Resources