Unable to use nuget packages - c#

I have written some code that uses the following packages:
using System;
using System.IO;
using System.Reflection;
using Microsoft.Azure.WebJobs;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
But when I compile the code there is a few errors related to the following code, that I suppose it is written by the "Microsoft.Data.SqlClient" package:
using Microsoft.Data.Tools.Schema.Sql.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
namespace GetDatafromBlob0201
{
[TestClass()]
public class SqlDatabaseSetup
{
[AssemblyInitialize()]
public static void InitializeAssembly(TestContext ctx)
{
// Setup the test database based on setting in the
// configuration file
SqlDatabaseTestClass.TestService.DeployDatabaseProject();
SqlDatabaseTestClass.TestService.GenerateData();
}
}
}
Where "using Microsoft.VisualStudio.TestTools.UnitTesting" throws an error.
I have tried installing the NuGet package, but that didn't solve the error.
These are the errors I get:
Errors after compiling
Mostly CS0246.
The last one indicates that the reference could not be used.
I am also noticing a warning sign under my proyect, on "Packeges" and also on "Assembly":
The one on Assembly its on Microsoft.Data.Tools.Components, Version= 16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL. It doesn't give me details on the warning.
The one on Packages is on Microsoft.Data.Tools.UnitTest and it says: Package 'Microsoft.Data.Tools.UnitTest' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead the project target framework '.NETCoreApp,Version=v3.1'. This package may not be fully compatible with your project.

Test with your csproj file and install any nuget packages, I did not get any build errors.
These are the nuget packages which I have installed.
Microsoft.Data.SqlClient
Microsoft.Azure.WebJobs
Microsoft.Data.Tools.UnitTest
So please make sure that there is no red error lines on your code editor.
Then, close VS, delete .vs hidden folder under the solution folder, bin and obj folder.
After that, restart VS and your project, then, run update-package -reinstall command under Tools-->Nuget Package Manager-->Package Manager Console
Update 1
I think you have created a MSTest test project(net core) project rather than a unit test project(net framework) project.
In my side, I used unit test project(net framework), and all work well.
Actually, some of your nuget packages are for net framework. That is what the warning NU1701 did. But you can ignore that warning since it is normal that you have install the package into net core rather than net framework. And the package can be used under net core. And it is just a little warning rather than a error.
Due to that, update-package -reinstall cannot work for new-sdk project(your situaiton) and only for packages.config with net framework.
Maybe try these:
1)Besides, I think Microsoft.Data.Tools.Components.dll is from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB. You have used some dlls from there. And due to some situations, these do not work and you should readd them on your current environment.
And first, please delete any references from here:
Then, readd these dlls from C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\Extensions\Microsoft\SQLDB to check if your project still have these errors.
Or you could remove Microsoft.Data.Tools.Components.dll from there to fix it.
If these do not help, maybe try nuget packages like what I said above:
1) clean all nuget caches first or just delete all files under C:\Users\xxx\.nuget\packages
2) to remove that warning, please add this xml node under csproj file of the unit test project.
3) Or you could try to use unit test project(net framework) project and then add your code there and I ensure that could work.

Related

Missing assembly reference - ensure VS creates a .config file for my nuget package

I've created a nuget package including just 1 .dll.
To test installation of the .nupkg, I
create a C# console application in VS
add a local feed pointing to my package directory
install my package from there
start typing in main...
The problem is, if I do this:
using System;
using MyNamespace;
namespace tester
{
class Program
{
static void Main(string[] args)
{
MyClass.MyStaticMethod()
Console.WriteLine("Hello World!");
}
}
}
the line MyClass.MyStaticMethod() says I'm missing an assembly reference, and using MyNamespace; is an unnecessary declaration.
Reading around, this seems to originate from the fact that my nuget package installs to the global packages location: %Current_User%.nuget.\packages, and the .csproj file includes:
<ItemGroup>
<PackageReference Include="MyPackage" Version="1.0.0" />
</ItemGroup>
Is there a way I can ensure all consumers of my package create a packages.config file, and not install my package to the global packages location?
Can I specify this in the .nuspec file somewhere?
The packages.config way is basically the classic (read: "old") way to reference NuGet packages and has since been superseeded by the PackageReference way of referencing packages. You should make sure your package is compatible with PackageReference.
If you don't increase the version number of your package every time you build it and want to try it out in a project, it won't see the update.
Either increase the version or clear your local NuGet packages cache using dotnet nuget locals clear all.
If the DLL is still not being referenced (check the dependencies node that VS shows you in the solution explorer), then your package may not contain the DLL in the expected place (e.g. lib\netstandard2.0 folder if your source project was a .NET Standard 2.0 library)

What is wrong with this code. Why can't I use SqlConnection?

I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this
Severity Code Description Project File Line Suppression State
Error
CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'.
This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12
Active
i don't get why it doesn't work
Here's my code
using System;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string connectionString;
SqlConnection cnn;
}
}
}
If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.
If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.
This Microsoft blog explains the change.
Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient
Your .csproj might look similar to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
</ItemGroup>
</Project>
Most likely the System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project. You have missed the database choice when You install visual studio. And now You just manually add references named "System.Data.SqlClient".
1.right click you project name and choose nuget package options.
2.search "System.Data.SqlClient" and install it.
Hope this is fix Your Error
The specific way to fix this from within VS Code is to
Open a terminal by going to Terminal -> New Terminal
Run dotnet add package System.Data.SqlClient
Run dotnet restore
That last command might not be necessary, but doing this made it work for me. It seems that console app templates don't come prepared for a reference to SqlClient.
Update
I think moving forward projects should use Microsoft.Data.SqlClient and not System.Data.SqlClient: https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/.
As #Mmm mentioned in the comments if you are using .NET Core and have already installed the System.Data.SqlClient package, closing and reopening the project resolved the issue for me too.
I had that error today and what's happening its that VS CODE its no recognizing System.Data.SqlClient;, that's why you can't call the variable...
How did I fix it??
I installed:
SQL server compact toolbox
Nuget Package version updater
Close visual studio/Open it again and your program should be rocking and popping!!
[!["Manage Nuget Package for Solution"]
it fixed my issue.
[1]: https://i.stack.imgur.com/hpk2e.png
I just installed nuget package from the "Manage Nuget Package for Solution". Previously I had used through command but that did not work so I used the UI to install this package.
NuGet Package
Install-Package System.Data.SqlClient
solve my problem.
For me System.Data.SqlClient was already installed. What i had to do was, I went to the Nuget package manager and downgrade the version to 4.8.2 from 4.8.3 and it worked.
It's pretty simple
Go to solution explorer > right click & click manage NuGet packages > and install System.data.SqlClient 4.5.1. or later.
Click here to screenshot

My NuGet package doesn't seem to update in projects that use it as a dependency

Forgive what must be a very simple oversight on my part but I simply cannot get this to work as expected.
I am building a .NET Framework 4.6 Class Library that needs to be downloaded by other applications from a private nuget repository. Here is my process:
I have a test project, ConsoleApp5. It has a simple main function in it.
I download my NuGet package, MyPackage (right click on project and click Manage NuGet Packages, select appropriate repo and package, click install.)
I run my test. In this case, I'm simply trying to instantiate a class, something not difficult. I'm getting an error about class accessibility.
Back in MyPackage, I replace all instances of the word protected with public. I add a new class as well by right-clicking on the project, selecting Add Item, targeting a class and giving it the name TestClass.
I go to Project > MyPackage Properties... > Application (tab) > Assembly Information, and I manually update the version patch number by one. So now it is 1.0.5.
I go to the project directory in the Developer Command Line and run the following command: C:\Users\jptak\Downloads\nuget.exe pack -Version 1.0.5
Then I run the following command to push the update (Success message: Your package was pushed.): C:\Users\jptak\Downloads\nuget.exe push MyPackage.1.0.5.nupkg MySecretCodeGoesHere -src http://my.nuget.server.goes.here.com
I go back to ConsoleApp5, Manage NuGet Packages, see the update in the appropriate package, and click the update arrow. The download completes and the new version number is there.
I go to use new classes or check that old errors are fixed and all of my tests fail. TestClass is not there.
This leads me to believe that the code I am writing is not making it to the NuGet server. Does anyone know what I am missing from this process to make the most recent version of the code get updated on the server?
This process is foreign to me as I am not a .NET developer (I work with PHP mostly).
Here are some more pieces of information about my IDE settings:
In Build > Configuration Manager... > Debug has been switched to Release
I have tried building, rebuilding, and cleaning my project. I haven't seen that any of these processes has had any effect on the code being pushed at all.
Here is the code from my ConsoleApp5:
namespace ConsoleApp5
{
class Program
{
static void Main(string[] args)
{
string testclient = "testclient";
string testpass = "testpass";
string scope = "validate-contract";
string accessToken = "";
string retrievedScope = "";
MyPackage.TestClass test = new MyPackage.TestClass();
}
}
Error: TestClass does not exist in namespace. Did you forget an Assembly Reference?
Here's the TestClass code in MyPackage:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyPackage
{
public class TestClass
{
public TestClass()
{
}
}
}
The NuGet server I'm using is: https://github.com/Daniel15/simple-nuget-server
EDIT 1
Upon inspecting the .nupkg file, I can confirm that the old code is still there and that the new code has not made it into the .nupkg. This seems to support the idea that the problem is with the packaging process and not on the nuget server. How does one refresh the dll files before each nuget pack / nuget push?
Bit long for a comment, not exactly an answer but here we go:
So top debugging tips for NuGet packages are:
Download the package manually. A .nupkg file is just a zip file that can be opened with 7Zip or any other archive tool.
Once inside you can inspect both the .nuspec file that has been used to pack the package, and inside the lib folder are the dlls that have been supplied.
A decompiler like DotPeek or ILSpy can be used on the dll to see exactly what is going on inside the dll.
Possible causes of your symptoms could include the NuGet server giving a false success, or your application failing to update the package and still refering to the old one.

References Assembly EntityFramework

I'm working on one of those new "Class Library (NuGet Package)" projects in Visual Studio. Everything was going fine until the other day it started raising an error about a:'assembly 'EntityFramework.Core, Version=7.0.0.0 reference??
How can I install the 7.0 version of EntityFramework into my Nuget packages??
"This issue" says that you need to add a reference to:
assembly 'EntityFramework.Core, Version=7.0.0.0
You can solve it by running the following command in the Package Manager Console:
Install-Package EntityFramework.Core -Pre
The issue is the EF version is different and following 2 assemblies are not supported.
using System.Data.Objects;
using System.Data.Objects.DataClasses;
Comment the above 2 lines and the following line in the code behind of your .edmx
using System.Data.Entity.Core.Objects;

The type or namespace IAppBuilder could not be found(missing using a directive pr an assembly reference)

I am working on an Asp.Net MVC 4 Application in which I am using SignalR 2.0.1 and I Mapped it using Owin Startup class and it worked fine at first.
All of a sudden when I tried to rebuild my app it said that the type are namespace IAppbuilder could not be found.
Following is my start up class
using Microsoft.Owin;
using Owin;
using WhiteBoardApp;
namespace WhiteBoardApp
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
I have installed Owin package too, and for some reasons I could not find Owin Startup class so I just added a normal class and included all the references that are needed.
May I know where I am making a mistake
Try to use Package Manage Console and do
Update-Package Owin -Reinstall
I was having similar issue. But instead Owin, problem was causing Microsoft.Owin, obviously
Update-Package Owin -Reinstall
Didn't work, neither did Update-Package Owin
BUT
Install-Package Microsoft.Owin
did work fine for me, thanks.
The IAppBuilder interface is found under Owin package. Just add a reference in your class file:
using Owin;
And rebuild. Your project will pick this up.
I have no idea why VS didn't pick this up, but it didn't. Once I added this reference to my project, then everything fell into place.
I encountered the same problem while building my project. Here are the steps that helped fix my problem:
Go to Solution Explorer and look for your project
Under your project, expand the References; You should see warnings on the problematic reference
Right click References and open Manage NuGet Packages
Search the name of problematic reference i.e. Microsoft.Owin; After loading it shows that it is already installed (It is, but it installed incorrectly. Checking the properties > version at step 2 shows 0.0.0.0)
Check Force uninstall, even if there are dependencies on it
Uninstall
Install
Build and run the project
Problems
Cannot install Microsoft.Web.Infrastructure because it already exists in the packages folder. Rolling back...
Go to your project folder and look for packages
Find the problematic package i.e. Microsoft.Web.Infrastructure
Delete the folder
Resume from step 7
Alternatives
Here are the alternatives I've read about to fix this kind of problem.
Clean and Rebuild Project / Solution
Restart Visual Studio
Restart PC
Good luck.
My Visual Studio 2013 for some reason didn't realize that the references paths existed. The yellow exclamation mark in front of the references was shown for all the added packages. I checked ../packages/ but all files existed, i also opened the .csproj file which referenced the correct paths.
Closing and opening the solution returned quite a lot of errors, and could not load the projects included in the solution.
Restarting Visual Studio 2013 saved the day for some unexplained reason.
My following using's equivalent in F# present a problem of hiding the IAppBuilder. It turns out that the Owin stipulation was being interpreted as an incomplete System.Web.Http.Owin reference, even though the Owin.dll providing the Owin namespace was referenced.
open System.Net.Http
open System.Web.Http
open Microsoft.Owin
open Owin
The problem was resolved by rearranging the usings as follows:
open Microsoft.Owin
open Owin
open System.Net.Http
open System.Web.Http
...granted, this may be a bug peculiar to the F# compiler and name conflicts are handle better in C# and elsewhere.
In my case, I had moved around the project folders and the location of the vs solution file (.sln). Once I was done with re-adding the projects, there was a packages folder on the solution level and one was left in a project sub folder.
This way, in that project, the relative package folder links in the .csproj file got messed up.
The reinstallation or other tips regarding the nuget package manager in this thread were helpful. I noticed, that after I reinstalled a few packages, in my git source code diff, the path of the packages folder was changed within the csproj file.
Before
<HintPath>packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
After
<HintPath>..\packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
So, if you run in the same issue and you have a lot of nuget packages, it might be easier to close the whole solution, open the csproj file(s) in a text editor like vscode and fix the relative links with search and replace. Then just save, close, reopen solution in VS and restore nuget packages. That should do the trick.
(In any case, you should delete the local packages folder on the project level, so that the project really fails, if it does not get the right packages.)
It's an ordering issue.
using Microsoft.Owin;
using Owin;
Leads to Microsoft.Owin to be defined first, then Owin is found under already imported Microsoft namespace. If you mouse over Owin of using Owin you should see it was resolved to Microsoft.Owin again and furthermore IDE will gray out using Owin as redundant unused reference.
Do:
using global::Owin;
Which clarifies for the compiler not to look for Owin under already defined namespaces (e.g. Microsoft. namespace).
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Check for the visual studio you are using
You can find the following comment
Note: If you are using Visual Studio 2012, the SignalR Hub Class (v2) template will not be available. You can add a plain Class called ChatHub instead.
Also
Note: If you are using Visual Studio 2012, the OWIN Startup Class template will not be available. You can add a plain Class called Startup instead.

Categories

Resources