.NET versions in ASP.Core - c#

I have a asp core web app with
"frameworks": {
"net452": {}
},
And I have also Class Library (.NET Core).
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"net452": {}
}
}
}
The question is, how can I add web app project reference to class library? Should I import some .net framework or use .net standard ?
Now I have a error message when I was trying to add reference to class library project.
The following projects are not supported as references:
"webprojectname" has target frameworks that are incompatible with
targets in current project "classlibraryprojectname"
"classlibraryprojectname": .NETCoreApp, Version=v1.0 "webprojectname":
.NETFramework, Version=v4.5.2

EDIT: Updated in accordance with changes to the question.
So I don't keep filling the comments section, your project.json's should look like this:
Web App (you will also have other dependencies not shown here):
"dependencies": {
"YourClassLibrary": "1.0.0-*"
},
"frameworks": {
"net452": { }
}
Class Library:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.2": { }
}
}
Note: if your class library depends on something higher than netstandard1.2, then you will either have to increment the .Net Framework in your web app to match the NETSTandard you're using (as per previously linked docs), or you will need to cross-compile the class library like so:
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net452": { }
}
}
Using the first suggestion, your application produces one assembly for your class library, using this second method your class library will be compiled twice (one for netstandard1.5, one for net452). When your web app tries to resolve the class library, it realises it can't use netstandard1.5 (which targets net462), so falls back to the net452 assembly. Note however that using this second option is usually indicative of something not being right with your project structure, and should only be used if you know what you're doing and why (in my opinion).
The NETStandard.Library dependency shown is essential a NuGet bundle that pulls down the necessary CoreFX packages. If you get errors like "Cannot find Microsoft.CSharp" it's usually because you haven't pulled down the default packages. You can instead pull down only the CoreFX packages you want, such as System.Collections but this is a more advanced technique.
Hopefully that helps clear things up?

Related

adding class library to web application with net core

I'v created a web application with net core and with this project
"frameworks": {
"net461": {
"dependencies": {
"Core": {
"target": "project"
}
}
}
},
Then I've crated a class library net core with this project
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
but when I try to add the class project to web application, VS2015 tell me
The following project are not supported as reference.
WebApplication:
.NETFramework, Version=v4.6.1
ClassLibrary:
.NETStandard, Version=v1.6
How can I add class library to main project?
If you want to target .NET framework 4.6.1, your class library must be .NET Standard 1.4 or lower as suggested on official docs - How to target the .NET Standard
1) Create .NET core class library. project.json looks like this-
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.4": {
"imports": "dnxcore50"
}
}
}
2) In ASP.NET Core application (.NET framework), it can reference like below-
"frameworks": {
"net461": {
"dependencies": {
"ClassLibrary1": {
"target": "project"
}
}
}
},
You have to start your project as a dotnetcore project (see image below) because it seems like you want to add a framework 4.6 file to your dotnetcore application.
for more information about dotnetcore compatibulity check this post
Compatibility between dotnetcore and framework 4.5

netcoreapp1.0 does not find net461 specific package

I am using VS Code to build a net core app. I need to use a NuGet package that is not supported, and therefore changed my project.json file's framework part to the following:
"frameworks": {
"net461": {
"dependencies": {
"ScrapySharp": "2.6.2"
}
},
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
}
}
Restoring the project seems to work and the package (ScrapySharp) is installed. However when I use the package, it appears that both netcoreapp and net461 look for it. While net461 finds and references it properly, netcoreapp throws the following error:
The type or namespace name 'ScrapySharp' could not be found
Is there anything I can do to work around this?
If package available only for one framework of two - you should modify your program code and do not use this package when compiled under netcoreapp. Effectively, you will lost some functionality of your app when compiled under netcoreapp.
If this suitable for you, then use preprocessor directives like this:
public void function DoSomething()
{
#if NET461 then
// do something with ScrapySharp
#else
// Say to your user that this feature is not available
throw new Exception("This feature is not available on this platform");
#endif
}

Referencing .NetCore Class Library in .NetCore Web Application

I have created a .NetCore Class Library which I have tested in same solution with .NetCore Console Application (and it works just fine), however when I try referencing it inside another (ASP.NET Core Web Application) solution as a NuGet package I always get a error
NU1002 The dependency YoutubeExtractorCore 0.0.6 does not support framework .NETCoreApp,Version=v1.0.
I have added the netcoreapp1.0 in the class library project.json, which looks like this:
{
"title": "YoutubeExtractorCore",
"description": ".NET Core library used for extracting information about Youtube videos",
"version": "0.0.6-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
},
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I spent this afternoon trying to fix the problem but I cannot even understand why this error occurs.
I finally figured out what was causing the problem. The NuGet package I created was packed with nuget.exe manually, and for some reason did not have the frameworks and their dependencies listed. I fixed the package by executing dotnet pack on the class library and this produced correcly packed NuGet package.

Portable Class Library has no DataContract or Serialization functions with .NET 4.6

I just took out a brand new copy of Visual Studio 2015 on a brand new copy of Windows 10. I tried to create a simple Portable Class Library (PCL) and tried to add a simple data contract:
namespace ClassLibrary1
{
using System.Runtime.Serialization;
[DataContract]
public class Class1
{
}
}
And the compiler tells me:
The type or namespace name 'DataContract' could not be found (are you missing a using directive or an assembly reference. It appears the namespace System.Runtime.Serialization is missing when .NET 4.6 is selected as the target.
There appears to be no serialization available when .NET Framework 4.6 is selected for the Targets. If I drop back to .NET 4.5.1, then the same code compiles (and runs in a more complicated project). What's going on here? Is .NET 4.6 not ready for Prime Time in Visual Studio? Anyone else run into this?
Had the same problem here, and it appears the solution is to add the relevant NuGet package(s) to the project that contains the functionality that has been moved out of Core. Specifically, you need the Serialization Primitives, but I've included the project.json file below that is probably closer to what you want in terms of actual configuration (dependencies, etc.)
This site also has a "search engine" of sorts for .NET 5 packages, which is basically what you're doing here.
{
"supports": {
"net46.app": {},
"uwp.10.0.app": {},
"dnxcore50.app": {}
},
"dependencies": {
"Microsoft.NETCore": "5.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.0",
"System.Collections": "4.0.10",
"System.Collections.Specialized": "4.0.0",
"System.Linq": "4.0.0",
"System.Linq.Expressions": "4.0.10",
"System.Linq.Queryable": "4.0.0",
"System.Net.Requests": "4.0.10",
"System.Runtime": "4.0.20",
"System.Runtime.Serialization.Primitives": "4.0.10",
"System.Runtime.Serialization.Json": "4.0.0",
"System.Runtime.Serialization.Xml": "4.0.10"
},
"frameworks": {
"dotnet": {
"imports": "portable-net452+win81"
}
}
}

System.IO.Compression in ASP.NET VNext full CLR

I'm trying to use System.IO.Compression.ZipArchive in a ASP.NET VNext class library in VS2015 Preview. I added the System.IO.Compression package using NuGet, and it added it to my project.json as a aspnetcore50 dependency.
When I try to use the ZipArchive, the intellisense says is not available in ASP.NET 5.0 but it is available in ASP.NET Core 5.0. If I switch to use ASP.NET Core using the drop down in the top bar, then my code works as expected, but when I choose normal ASP.NET it doesn't work.
I tried manually adding it as a dependency to aspnet50 in the project.json, but that didn't fix it.
I need to use the full CLR over the Core CLR as I need to load assemblies into the AppDomain at run time, and I believe this isn't supported in the Core CLR.
Please could someone explain what's going on here, maybe point me to some articles or blog posts, show me how to fix this.
Update:
I guess a better way or wording this is - the ZipArchive is not available in aspnet50, but it is available in aspnetcore50 when I add the System.IO.Compression NuGet package. Why is this?
They only way that I get the project to compile and work was doing the following in the project.json. I'm not too familiar with the compression library so I did not spend time trying to compress a file. Below you will a sample code that will compile with no issue.
{
"version": "1.0.0-*",
"dependencies": {
},
"frameworks": {
"aspnet50": {
"dependencies": {
},
"frameworkAssemblies": {
"System.IO.Compression": "4.0.0.0"
}
},
"aspnetcore50": {
"dependencies": {
"System.Runtime": "4.0.20-beta-22231",
"System.IO.Compression.ZipFile": "4.0.0-beta-22231",
"System.IO": "4.0.10-beta-22231",
"System.IO.FileSystem": "4.0.0-beta-22231"
}
}
}
}
Sample Code
public static void ZipFile(string path)
{
var data = new MemoryStream(File.ReadAllBytes(path));
var zip = new ZipArchive(data, ZipArchiveMode.Create,false);
zip.CreateEntry(path + ".zip");
}

Categories

Resources