How to create Cross Platform .net core library? - asp.net-core

How do i create C# library in .Net core that can be shared between classic .Net 4.6.2 and .Net core projects using Nuget packages. I read the documentation here about developing cross platform libraries. So i setup my project.json as below
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net462": {
}
}
}
Then i created nugect pckage by executing the following command
dotnet pack -c Release
and then published the nuget package. My package name is DTO.
In my traget .net core project i was able to install nuget package without any issue. However when i try to install package in classic .net 4.6.2 project i get error
install-Package : Could not install package 'DTO 1.0.0'. You are
trying to install this package into a project that targets
'.NETFramework,Version=v4.6.2', but the package does not contain any
assembly references or content files that are compatible with that
framework. For more information, contact the package author. At line:1
char:1 + Install-Package DTO + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo :
NotSpecified: (:) [Install-Package], Exception + FullyQualifiedErrorId
:
NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
Note that the nuget package is hosted on my local server.

You took the right direction. If you want compatibility across multiple frameworks, targeting .NET Standard is the right move.
However, you should refer to this matrix for the .NET Standard versions supported by each framework.
https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md#mapping-the-net-platform-standard-to-platforms
According to this table, .NET Framework 4.6.2 supports .NET Standard 1.5 (not 1.6 as you specified). So here is your answer.
For more details about the assemblies supported in .NET Standard 1.5, please follow this link:
https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md#list-of-net-corefx-apis-and-their-associated-net-platform-standard-version

Related

What am I missing to upgrade asp.net mvc .net core 2.2.0 apps to .net core 2.2.1

I have the latest version of visual studio, the sdk and runtimes for x64 and x86 version 2.2.1, but my asp.net mvc .net core applications will only recognize being 2.2.0. I can install 2.2.1 into .net core libraries.
What do I need to change to allow my web projects to be 2.2.1 instead of 2.2.0?
(It can't be done through nuget, it provides the statement: Implicitly refrenced by an SDK. To update the package, update the SDK to which it belongs
Since the project is referencing a library with 2.2.1, I see the error: Error NU1605 Detected package downgrade: Microsoft.AspNetCore.App from 2.2.1 to 2.2.0. Reference the package directly from the project to select a different version.)
According to Microsoft documentation packages Microsoft.AspNetCore.App and Microsoft.NETCore.App have special versioning semantics which is handled outside of NuGet.
Also note that specifying a version number on Microsoft.AspNetCore.App package does not guarantee that desired version will be chosen. In general, you should not explicitly specify Microsoft.AspNetCore.App version unless you have a good reason to do so (applies to .NET Core 2.1 and later).
You can read more about that here: Microsoft.AspNetCore.App metapackage
If you want to target specific SDK used in your project or solution, add global.json file to project folder (if you want to target only that specific project) or solution folder if you want to target all projects in the solution.
global.json example:
{
"sdk": {
"version": "2.2.103"
}
}

How can I add .NET framework 4.5.2 to Asp.Net Core

If I click add reference I get
The following projects are not supported as references... AspNetCore version 1.0 and .NETFramework version 4.5.2
I follow this article but no success.
I changed the project.json with
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
},
"net452": {
"dependencies": {
"T4Generator": {
"target": "project"
}
}
}
},
And doesn't work, my .NET framework 4.5.2 is named T4Generator.
Is possible to add this project to Core?
There are two versions of .NET Core, .NET Standard version (Portable version, which you use in your project) and full .NET framework version.
If you choose portable version of .NET Core, then all the packages and libraries should be in .NET standard in order to use them. Unfortunately most of .NET framework packages are not ported to .NET standard yet.
Best way to solve your issue is converting your application to use full .NET Framework version of .NET Core. If not you can't use most of packages like T4Generator.
I would suggest you you to upgrade your project to use VS 2017 with csproj rather than project.json which is obsolete. Then convert.
Convert .Net Core to .Net Framework

.Net Core basic issue

I am new to this area. I already upgraded my VS2015 with update 3. So now I have: .Net Core 1.0.0 with preview tool 2 installed.
Then I use VS2015 to create a new .Net Source project (class library). I have copied some existing .net 4.6.1 code to the new place and compiled error as expected. Then I use "porting analyzer tool"(https://learn.microsoft.com/en-us/dotnet/articles/core/porting/index) to compare and it did give some suggestion how to use the new way in .Net Core.
But there are still few class missing and cannot compile, like 'TypeDescriptor' or 'NullableConverter' etc. Then I googled and someone from Github say they already added these feature but I still not sure why I still get compilation error.
I also noticed that my class libary project has a project.json file as:
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
I foundn if I remove the 'netstandard1.6' one but change to:
"frameworks": {
"net461": {}
}
All the compilation error gone, but I could guess it's NOT .Net core project anymore, and more likely as a standard .Net 4.6.1 project.
So basically I'd like to ask:
Why developer from Github saying that is fixed already but I still couldn't find/compile those classes in my project (e.g. TypeDescriptor : https://github.com/dotnet/corefx/issues/8262)?
Some sample also puts both 'net461' & 'netstard1.6' or 'netcoreapp1.0' under 'frameworks' in project.json file. What's the purpose for that?
If I just use 'net461' for the only framework, there seems no difference to traditional .Net Framework 4.6.1 project. Am I correct?
Thanks a lot!
Install package System.ComponentModel.TypeConverter and you are good to go with .NET Core project.
For building different output (binaries) each of which targeting different platform. net461 for applications which should run on (full) .NET Framework (under Windows or Mono). netcoreapp1.0 used for app which should be run under .NET Core. netstandard1.6 (avoid using netstandard1.5 and netstandard1.6 by the way - use previous versions) used for portable class libraries, which are supposed to run under different platforms: (full) .NET Framework, .NET Core, Xamarin, Universal Windows App, etc. You may read more about here and here.
Absolutely.

Asp.net Core RC2 not detecting Reference to nuget package

Im using Clean RC2 Template for web application.
after adding Reference to System.linq it gives me this :
Im using 461 as framework.
It's automatically restore back the package version to match the targeted framework version (net461).
So actually it's "detecting" nuget packages.
You trying to target 461 in the image, so the packages rolling back from .net core preview1 to .net framework 4.6.1 .
If you want target multiple framework (or as I see you want RC2 packages, meaning want .net core), your config.json file's frameworks section should look like this for example:
"frameworks": {
"net461": { // old .net framework
"dependencies":{
"System.LINQ": "4.0.0.0"
}
},
"netstandard1.5":{ // .net core
"dependencies":{
"System.LINQ": "4.1.0-RC2-*"
}
}
},
Here is a good info about targeting platforms
If you just want use .net core, remove the net461.
I see you want ASP.net core. Here is some cli ASP MVC samples.
Here is a really nice ASP.net core sample project (using latest .net core, you have to update to RTM/preview2)
You should already be able to use LINQ in your code (using System.Linq) without having to add a reference.
Since you are targeting .NET 461, you can add any additional GAC references in the frameworkAssemblies section in your project.json, e.g.
"frameworks": {
"net461": {
"frameworkAssemblies": {
"System.ServiceProcess": "4.0.0.0",
"System.Configuration": "4.0.0.0"
}
}
},
See this question for an explanation between dependencies and frameworkassemblies.

Unable to install StructureMap.MVC4 on .NET 4, MVC 4, VS 2012

I am trying to install StructureMap.MVC4 for a project, created in Visual Studio 2012, .NET 4.0 and ASP.MVC 4.
But I am getting below error:
Could not install package 'Microsoft.AspNet.WebApi.Client 5.1.2'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
At line:1 char:16
+ install-package <<<< structuremap.mvc4 WebApi_StructureMap_DotNet4
+ CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
+ FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
Any suggestions on what I am missing please!
Microsoft.AspNet.WebApi.Client 5.1.2 can only be installed into a project that targets .NET 4.5 or is a portable class library. It does not have any assemblies for .NET 4.0.
The latest version of StructureMap 3.0 depends on Microsoft.AspNet.WebApi.Client 5.1.2 so if you want to that the latest version of StructureMap 3.0 you would need to change your project so it targets .NET 4.5.
Older versions of StructureMap.MVC4 may work with .NET 4.0. Version 2.6.4.3 for example depends on an older version of Microsoft.AspNet.WebApi which supports .NET 4.0. Even older versions of StructureMap.MVC4, such as 2.2.0 do not depend on Microsoft.AspNet.WebApi.