Find package for extension method? - visual-studio

I have two solutions. Both call extension method "AddDbContext" on an object of type IServiceCollection. Both solutions are .NET 5.
It works fine in one project, but the other project doesn't know about the extension method. Is there an easy way to determine which Nuget package I need to install? Short of Googling (which I can do), is there a simple way in Visual Studio to determine which package contains the extension method?
In the working solution I right click on the call to "AddDbContext" and go to definition. It's in namespace "Microsoft.Extensions.DependencyInjection" in file EntityFrameworkServiceCollectionExtensions.cs. Both solutions have package Microsoft.Extensions.DependencyInjection version 5.0.2 installed on the only project.
The solutions don't have all the same packages installed, but from their names I can't decipher which package installed might be adding IServiceCollection.AddDbContext.
I can Google this, of course, I just wonder if there is some menu in Visual Studio which can just tell me which package contains the extension method?
In this particular case, installing package Microsoft.EntityFrameworkCore v5.0.10 fixed the issue. But why was that package needed, and how could I have determined that using Visual Studio?

If you choose "Peek definition" instead of "Go to definition", then you see the full path of the assembly inside the region at the top of the definition. In the path you see what package it is from.
Some decompiler extensions (such as ILSpy or dotPeek) might also show it in the "Go to definition" view.

Related

Copy DLLs dependancy files to Main project

I have a visual studio solution (microsoft visual studio professional 2017) with a main project, that uses classes from a DLL project in the same solution.
The DLL is a "class Library (.NET framework).
Main project is (for my preliminary investigations) a console app. Future versions will be a WPF. I think that the problem appears on all applications that use DLLs that use other DLLs.
The DLL uses other DLLs from a Nuget package. In this case: SQLite. I want to hide to my main project that my DLL uses Sqlite. That makes it possible to change it in future versions to a different database, or maybe use entity framework to access the data.
So my main project only knows that it uses the DLL in the same solution. It does not know that this DLL uses other DLLs.
Problem: These other DLLS are not copied to my main application folder.
This has been asked before: Copying a DLL's dependencies in Visual Studio
The answer says that you should use classes from the Sqlite to make sure the DLLs are copied. That is just what I wanted to prevent: users of my DLL should not have to know that this DLL uses SQLite
That question is 11 years old.
I wonder if Visual Studio has now a better solution for this.
A method to solve the problem was given in Install and manage packages in Visual Studio using the NuGet Package Manager. It is not ideal, the main project still needs to know that the other projects use certain Nuget packages, but at least you don't have to edit the .csproj files, nor type command lines
MySolution
MyMainProject. Refers to MyDll
MyDll. Uses Nuget packages
In Visual Studio. Menu - Tools - Nuget Package Manager
The window that opens shows the installed Nuget packages. If you click on them, you can see on the right the projects that use these packages.
For all installed packages that your main program complains that it is missing at run time, check the box near MyMainProject. At the bottom click Install.
Alas, I'd rather have something in project MyDll that tells everyone who uses this DLL that they also need these Nuget packages. But for the time being I'll use this method.
Will check again in 11 years if there is any improvement.

Add references Visual Studio 2010 (NuGet)

I am trying to use some references in Visual Studio, I have installed NuGet to use some libraries.
Is there a way to use only part of the package installed with NuGet? For example, if I am using TeklaOpenApi and the following .dll files are installed with this package:
TeklaModel
TeklaDialog
TeklaDrawing
Use for example just TeklaModel, could I do this using NuGet?
Is there a way to use only part of the package installed with NuGet?
I am afraid that you cannot get what you want. It is designed by nuget package. Usually, when the nuget package contains other dlls which means they are probably depended on a master DLL, or used at runtime.
All of them play an important role in this nuget, so we cannot easily remove them.
Although we can use Assembly Reference format(Right-click on References-->Add Reference--> choose one Dll) to reference the specific dll, but there is a risk that if the DLL depends on other corresponding DLL, an error will be reported. So we don't recommend it.
The best way is to install the whole nuget package with all the related dlls.
Hope it could help you.

Reverse NuGet Lookup

Given an assembly I need, how do I find the NuGet package that provides it?
For example, I'm using ASP.NET Web API and I need the definition of DelegatingHandler. In a painful manner, I determine that this is available in the NuGet package Microsoft.Net.Http. But then, by accident, I discover it is also available in the latest (4.0.0) version of System.Net.Http.
There should be an easier way to locate the NuGet package or packages that provide the type rather than having a stumbling and guessing exercise with google.
Right now I also need the type System.Net.Http.ObjectContent, which is found in assembly System.Net.Http.Formatting. However, I'm not sure which NuGet provides it. I should not need to guess, but I'm going to guess that the relevant package is Microsoft.AspNet.WebApi.Client. Unfortunately, that package lists Microsoft.Net.Http as a dependency, rather than the version 4.0.0 of System.Net.Http which I have chosen for my other Web API dependencies.
So I'm kinda stuck trying to figure out what NuGet package or packages to grab.
Is there a "Reverse NuGet Lookup" tool I'm overlooking that would simplify this search?
There's the Reverse Package Search website but it includes only some of the NuGet packages, mostly those from BCL and ASP.NET: http://packagesearch.azurewebsites.net/
In addition, if you are using Visual Studio with the latest tooling, you should be able type a type name and intellisense will tell you what package to add. For example, type IApplicationRuntime, press ctrl + . and you'll get a suggestion for Microsoft.Dnx.Runtime.Abstractions.

Adding System.Data.SQLite through NuGet doesn't add a reference to the project

I'm trying to make a very simple Xamarin.Forms test application to explore some features of the System.Data.SQLite package (namely encryption). However, I can't seem to get Visual Studio 2012 to add a reference to the package to the project.
Here are the steps I am taking:
Created a new Xamarin.Forms Portable blank app.
Right-click on TestApp (core project) and select Manage NuGet Packages
Search for "sqlite"
Install the "System.Data.SQLite (x86/x64)" package. (This package is version 1.0.97.0 and it is described as "the official SQLite database engine for both x86 and x64 along with the ADO.NET provider.")
According to NuGet, the package was successfully installed.
However, I cannot see the reference added under my project's "References" folder, and I also cannot write "using System.Data.SQLite" without errors ("cannot resolve symbol 'Data'"). Does anyone have any idea why this is happening?
On a side note, I can follow the exact same process described above except instead of creating a Xamarin.Forms Portable blank app, I choose a regular C# console app. This allows me to install the NuGet package fine and it shows up in references as expected.
Posting an update to share what I've learned.
So as Jason sort of alluded to in his answer, the reason I was not able to reference the package in my Xamarin.Forms project is because Xamarin.Forms is, by definition, a PCL project. It's meant to work on all platforms. The package I was referring to in my question is not a PCL, which means that anything in the package that is platform dependent is inaccessable (which is basically the entire package). The SQLite.Net-PCL package is basically the same thing as System.Data.SQLite, but in PCL form, which means it is compatible for use in a Xamarin.Forms project.
Following this logic, it makes sense that I was able to access System.Data.SQLite from a console project, since a console project is not a PCL project and it targets a specific platform.
For PCL projects, use SQLite.Net-PCL.

A way to clean up Nuget packages in the csproj file?

As I add and remove references sometimes I don't always "uninstall" a package which appears like a reference. So it remains "installed" in the Nuget package explorer. It seem like I could really use something that cleans up the .csproj and the packages.config file -- is there something that does this?
This would be useful for both Visual Studio (VS) 2010 and VS 2012.
Uninstalling a package should be done as an explicit gesture.
If you want to know if a particular reference is part of a package, you can check the Path of the reference. If the hint path is something like "..\Packages\.." , you will know that the reference is part of a package.
I'm also looking for a tool to help with cleaning up the packages and at least identify unused Nuget packages for me. For now the manual way to check that a package is used or not is to uninstall the package and compile the project. If everything is still passing that means the package is not used. If things failed you can simply revert the changes in your source control system. You will need to repeat these steps for each package installed in your Visual Studio solution.

Resources