Could not add nuget package to class library - asp.net-core-mvc

I am using Visual Studio 2017, I created an empty .NET Core Class Library, I tried to add a package to my project and got the following error
Invalid portable frameworks for '.NETPortable,Version=v0.0'. A portable framework must have at least one framework in the profile
I searched internet for help, could not find anything. This class library is referenced in a ASP.NET MVC Core app which does not have a project.json file, I guess it is gone now. Any help is appreciated.

I suppose you have created a nuget package that you want to use in your projects. Mentioned nuget package is created as Portable Class Library and can be installed in regular Class Library projects.
If you want to be able to install this package to .NET Core class libraries or console apps (which includes web applications) you need to create it as .NET Standard Class Library. There is a walk through on how to do it: Create .NET standard packages with Visual Studio 2017.
UPDATE:
From what I have found C++ is not supported in .NET Core and there is no plans to support it right now Proposal: C++/CX support, Will CoreCLR support C++/CLI crossplat?, Using .Net (C++/CLI) legacy code in Net Core.
I am not an expert in C++, but maybe it is possible to create a library in C++ and P/Invoke it with C# and wrap both in NuGet package? Or rewrite it completely in C#?

Related

How to get NuGet options in Visual Studio 2017 Community?

I am struggling with creating NuGet packages. I am using Visual Studio 2017 Community edition.
I have seen a couple of videos that show a "Pack" option on the menu when right-clicking the project in Solution Explorer. However, I do not have that option. Is this one of the features in the other (non-Community) versions of Visual Studio? I believe I have also seen a "create NuGet package on build" option mentioned somewhere. I cannot find that either.
I have tried various ways of using nuget, dotnet, and msbuild from the command line(s), but haven't had much success. Very frustrating.
Any help is appreciated.
If you really want to use Visual Studio, I would recommend installing an extension that helps you with that problem. For example, this one. The options people have in videos depend on the extensions they have installed. For you, it is the same.
Alternatively, just use the command-line tooling for this as explained here or for .NET Core here or here.
dotnet/msbuild pack is only available for SDK-style projects, but I believe works for all versions of Visual Studio, as well as on the command line. .NET Core introduced these SDK-style projects, which can be identified by <Project Sdk="Microsoft.NET.Sdk">. If your project (.csproj if it's a C# project) doesn't have the Sdk property or import Microsoft.NET.Sdk in either of the two other ways, then it's not an SDK style project and doesn't support packing in this way. Another obvious difference between the two styles of projects is that SDK projects are only a few lines long from the new project template and don't list files in the project, whereas old style projects are typically a full screen long, even from a new project template with only a single class file, and it does list individual files in the project. If you want to continue with this project type, you'll need to use nuget.exe pack and you'll probably want to create a .nuspec file to define some of the package metadata.
However, using SDK style projects is the future, it just takes time for all of Microsoft's existing project types to migrate. It's much simpler to use, so personally I would avoid old style projects unless you're using a project type (like ASP.NET, not ASP.NET Core) that doesn't support it.
All of this is confusing for anyone new to the .NET ecosystem. My recommendation is 1. when you install Visual Studio, when making your workload selections, make sure in the component list that .NET Core is selected, whatever the newest version of .NET Core that is available at the time of installation. When creating a new project in Visual Studio, always select the .NET Core version, or .NET Standard version of any new project template, even if you want to target the (Windows) .NET Framework, in which case you edit the .csproj and change <TargetFramework>netstandard2.0</TargetFramework> to <TargetFramework>net45</TargetFramework>, although I would recommend multi-targeting possible by adding a s to the element name and using a semi-colon separated list: <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>. So, avoid the "Class Library (.NET Framework)" template, instead use "Class Library (.NET Standard)" and then change the target if you have to.
#zivkan led me down the right path. Changing my project types to .Net Core from .Net Framework made all the options I mentioned in my original post available. No extensions were needed.
My .Net Core class library project now has the Pack and Publish options available on the project's context menu. In addition, there is a another tab (Package) on the project properties page. On that page there is a "Generate NuGet package on build" option along with version, name, tags and other properties.
I have done much .Net framework development, but have been ignoring .Net Core and the newer options. I guess I need to dig in and learn about them.

Xamarin Forms - Targeting .NET Standard

I read this article a while ago:
https://blog.xamarin.com/net-standard-library-support-for-xamarin/
So, I converted all of our libraries from portable libraries to .NET Standard (1.4). I did this because the article says "This PCL now needs to be upgraded to target the .NET Standard Library, which can be found in the projects properties.".
However, I can't figure out how to build a Xamarin Forms project that targets .NET Standard. I cloned the Xamarin Forms samples, and opened up the MasterDetailPage project. I went in to the project properties and switched from portable to .NET Standard as per the instructions. Immediately, I get an error telling me that I need to opt in to NuGet 3.0 support. I'm fine with this, but how do I do it?
I found that if I remove the Xamarin Forms NuGet package, I am able to switch over to .NET Standard. However, once I have done this, I can't add the Xamarin Forms NuGet package back. It just keeps failing. Contrary to what the article says, I cannot add references to .NET Standard libraries. When I try to add a reference to existing .NET Standard libraries in my Solution, Visual Studio just gives me an error saying that the library is not compatible. Which version of .NET Standard should I be targeting for Xamarin Forms?
How do I get a .NET Standard library compiling with Xamarin Forms support?
If you wanna do it with the new Visual Studio 2017 release with csproj instead of project.json you can either use dotnet migrate CLI command or add/edit this code to your csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
</PropertyGroup>
</Project>
It turns out that you can get a .NET Standard project to reference the Xamarin Forms NuGet package, and therefore be able to leverage .NET Standard libraries in Xamarin Forms.
This article more or less explains how to do it. The trick is to import the portable framework in the project.json.
https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/
If you can't get it working, here is a Git sample that does work.
https://github.com/adamped/XamarinForms.NetStandard.git
I was eventually able to target .NET Standard 1.4 without issues.

Which Class Lib for ASP.NET Core tag helpers when .Net Core Web App targets Full .Net Framework?

I have created a Asp.Net Core Web Application that targets the full .Net Framework. This was accomplished by using the Visual Studio ASP.NET Core Web Application (.NET Framework) template.
I'd like to create some tag helpers that I use in this Web Application and I'd like to place them in a library dll. Initially I tried adding a new project to my solution of type Class Library (.NET Core) to place them in but I was unable to reference that library from the web project. When I tried to reference it via Visual Studio I received a dialog that said the project reference was not supported because the Class Library has a target framework that is incompatible with the targets in my Website Project.
So as an alternative, I added a regular Class Library project to the solution via the Visual Studio Class Library template. I am able to add a reference from my Web Project to this class library successfully. However, in this Class Library project I can't find a way to reference Microsoft.AspNetCore.Razor.Runtime which is necessary if I'm going to be able to put my tag helpers in the library.
So I'm at a dead end with both approaches. How can I put my ASP.NET Core tag helpers in a class library that I can reference from my ASP.NET Web Project that targets the full .net framework?
When I tried to reference it via Visual Studio I received a dialog that said the project reference was not supported because the Class Library has a target framework that is incompatible with the targets in my Website Project.
This usually means you both libraries target different things, i.e. if your Class library targets netstandard1.6 and your web application targets net452 its not going to work.
Either your web application needs to target netcoreapp1.0 or your class library has to target netstandard1.0. Use the .NET Standard Library matrix here to see which netstandard1.x moniker supports which platform.
So if your web application targets net452, you have to target netstandard1.0, netstandard1.1 or netstandard1.2. netstandard1.3 and higher don't work, because it requires .NET Framework 4.6.
If you use a library which requires netstandard1.3 or higher, you have to target multiple platforms. i.e. net452 and netstandard1.3.
Then net452 will be used in projects which target net452 and netstandard1.3 will be used in netcoreapp1.0 applications or other class libraries which target netstandard.

Is TPL DataFlow included with either .NET 4.5 or .NET 4.5.1?

I'm confused. We upgraded our project recently to .NET 4.5.1. We installed .NET 4.5.1 on our servers.
I'm referencing assembly System.Threading.Tasks.Dataflow from the GAC at C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Threading.Tasks.Dataflow\v4.0_4.5.9.0__b03f5f7f11d50a3a\System.Threading.Tasks.Dataflow.dll
CopyLocal is set to false as (I assume) it's part of the Framework, so should be already installed wherever the app is deployed.
The trouble is, when running on the server, it says it can't find this assembly.
I've read here that is is part of .NET Framework 4.5:
but the library is now built-in with .NET 4.5.
...and I've read here that it isn't!
The TPL Dataflow Library (System.Threading.Tasks.Dataflow namespace) is not distributed with the .NET Framework 4.5
What gives? It doesn't look like it is. Is it just part of the 'targetting pack'? Is it me, or are these in-place upgrades more agro than they're worth?!
I'm not sure why that page says it is part of .NET 4.5 -- it's still an out-of-band NuGet release, like Entity Framework, MVC, etc.
I recognize this question is pretty old, but I wanted to add my solution as well. I found adding the below two items to the Visual Studio 2019 install (run Visual Studio Installer > Modify > .Net Desktop Development) the problem went away and it was able to pull System.Threading.Tasks.Dataflow from the GAC.
Edit: Installing Visual Studio 2015 also seemed to be required. Obviously it isn't VS 2015 itself, just something it comes with. But there are too many packages for me to figure out which one easily.
Edit2: This isn't really the best solution, more of a stopgap. MS suggests downloading it through Nuget. https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.dataflow?view=netcore-3.1
The TPL Dataflow Library (the System.Threading.Tasks.Dataflow namespace) is not distributed with .NET. To install the System.Threading.Tasks.Dataflow namespace in Visual Studio, open your project, choose Manage NuGet Packages from the Project menu...

How do I reference System.Windows 5.0.5 assembly (Silverlight) from .NET 4.5 library project?

I have a project written for Silverlight 5, which references Silverlight's System.Windows assembly in version 5.0.5.
Now I want to create a unit test project for it using a usual .NET 4.5 library project. When I try to add a reference to the System.Windows assembly 5.0.5 from Silverlight (using the browse dialog), Visual Studio adds the version from .NET 4.5.
Hence the version number does not match.
Why does Visual Studio behave this way?
How do I fix this?
You can reference Silverlight assemblies from a .Net project, but once you have managed to add one, you will likely have problems with dependencies on other libraries.
It will be easier on you if you to try a different approach...
I would suggest using the Silverlight Unit Test Framework to deal with testing your Silverlight code.
If that doesn't fit your need, perhaps Portable Class Libraries will.

Resources