Using Shared Libraries with .NET Core - .net-core

I wrote my open source library, LINQ to Twitter, with shared libraries to minimize deployment artifacts and handle platform specific features. I want to support .NET Core and am thinking that the fastest approach would be to reference the shared libraries. The Add References dialog didn't show the shared libraries, so I tried project.json:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"LinqToTwitter.Shared": "*",
"LinqToTwitter.Shared.net": "*"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I tried a few combinations of versions, but didn't get anywhere. The error messages contain something like this:
The dependency LinqToTwitter.Shared >= * could not be resolved.
Next, I opened the *.xproj and pasted the following Imports into the Project section:
<Import Project="..\LinqToTwitter.Shared\LinqToTwitter.Shared.projitems"
Label="Shared" />
<Import
Project="..\LinqToTwitter.Shared.net\LinqToTwitter.Shared.net.projitems"
Label="Shared" />
This doesn't show references in VS, nothing in metadata, and (as it would follow) can't reference any shared library types from a console app that references the .NET core app.

if library is in the same solution you can reference it using target:project something like
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"LinqToTwitter.Shared": {"target": "project"},
"LinqToTwitter.Shared.net": {"target": "project"}
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}

Good news - Visual Studio 2017 now allows .NET Core library projects to reference Shared projects.

Related

How to Reference ".NETPlatform,Version=v5.0" Framework in Xamarin.Forms Project?

I'm currently working on a Xamarin.Forms project named ABCD, using macOS Sierra v10.12.6 and Visual Studio (VS) for Mac v7.3.2 (the steps leading up to this problem are detailed here).
Having updated my NETStandard.Library package from version 1.6.0 to 2.0.1*, I notice that the project.json file still says:
"frameworks": {
"netstandard1.5": {}
}
*VS update this package to 2.0.1, despite Terminal showing that dotnet --version is 2.1.3.
I went ahead and replaced netstandard1.5 with netstandard2.0 and rebuilt the project. But was then immediately met with this error:
Error: Your project is not referencing the ".NETPlatform,Version=v5.0" framework. Add a reference to ".NETPlatform,Version=v5.0" in the "frameworks" section of your project.json, and then re-run NuGet restore. (ABCD)
I've tried to fix this in the following ways:
1 – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {}
}
2 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {
"imports": ["netstandard2.0"]
}
3 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
".NETPlatform,Version=v5.0": {
"imports": [".NETCore,Version=v5.0"]
}
4 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {
"imports": [".NETCore,Version=v5.0"]
}
5 (source) – Updating project.json:
"frameworks": {
".NETPlatform,Version=v5.0": {
"imports": ["netstandard2.0"]
}
6 (source, source) – Updating ABCD.csproj:
<PropertyGroup>
<NuGetTargetMoniker> .NETStandard,Version=v2.0 </NuGetTargetMoniker>
</PropertyGroup>
I thought I might find something helpful here and here but wasn't successful.
The same error from above still remains after having tried the above fixes. How do I fix this?
EDIT:
7 (source) – Updating project.json:
"frameworks": {
"netstandard2.0": {},
"netplatform50": {}
}
Still seeing the same error.
This post suggests not using project.json but instead to do a dotnet migrate in your project root folder to convert it so that it uses .csproj instead

Using reflection in net core packages targeting several frameworks

I am developing a library(nupkg) in .net core targeting both net45 and netstandard1.3 and at some point I need to use reflection so my project.json looks like this:
{
"version": "1.1.3",
"dependencies": {
common-dependencies
}
},
"frameworks": {
"net45": {
"frameworkAssemblies": {
"System.Reflection": "4.0.0.0"
},
"dependencies": {
"NLog": "4.3.5",
"Newtonsoft.Json": "6.0.4"
}
},
"netstandard1.3": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Reflection.TypeExtensions": "4.1.0",
"Newtonsoft.Json": "8.0.2",
"NLog": "4.4.0-*"
}
}
}
}
Most of the times after a mere install-package my-package everything works like a charm, however a couple of times I get this error on installing:
install-package : Failed to add reference. The package 'my-Package' tried to add a framework reference to 'System.Reflection' which was not found in the GAC. This is possibly a bug in the
package. Please contact the package owners for assistance.
I've found a workaround, which is to uninstall-package every other package on the project I want to install my package. This is very odd and undesired behavior I think.
I do have noticed however that on some of those project had different versions of Newtonsoft.Json installed on different projects. This is a warning I also get when installation fails:
Install failed. Rolling back...
Package 'my-package : Newtonsoft.Json [6.0.4, ), NLog [4.3.5, )' does not exist in project 'Target.Project'
I have no idea what could be going on here, and the fact that it happens randomly and the workaround to fix it don't help much.
Any ideas please?
System.Reflection is a reference assembly, but not a runtime assembly. So, you need to use it when building your code, but not reference it from your built assembly.
To do that, specify "type": "build", e.g.:
"frameworkAssemblies": {
"System.Reflection": { "type": "build" }
}

Cannot find Convert.ToInt32 method for using with .NET Standard

I am trying to update project from netcoreapp framework to netstandard and looking for a way to replace conversion from object to int:
int value = Convert.ToInt32(rawObject);
as Convert class is defined in CoreCLR, and cannot be used with .NET Standard framework.
project.json:
{
"version": "1.0.0-*",
"dependencies": {
"System.Runtime": "4.1.0",
"System.Linq": "4.1.0",
"System.Linq.Expressions": "4.1.0",
"System.Reflection": "4.1.0",
"System.Threading.Tasks": "4.0.11",
"System.Reflection.TypeExtensions": "4.1.0",
"System.Runtime.Extensions": "4.1.0"
},
"frameworks": {
"netstandard1.6": {
}
}
}
Update:
Thanks to #Lex-Li and #svick: Convert.ToInt32(object) method is really defined in the System.Runtime.Extensions reference assembly.
In my case I had the following error even after updating dependencies:
"The name 'Convert' does not exist in the current context".
Deleting project.lock.json and restoring from scratch have solved the issue.
Add System.Runtime.Extensions 4.1.0 to your project.json.
A quick search on the following site can show you why,
http://packagesearch.azurewebsites.net/
Your code compiles just fine for me with your project.json.
Convert.ToInt32(object) is defined in the System.Runtime.Extensions reference assembly.
Implementation assemblies for System.Runtime.Extensions forward the System.Convert to mscorlib (using [TypeForwardedTo]).

Mocking framework for ASP.net 5.0 vnext class library project

I have created a ASP.net vnext class library project in Visual Studio 2015. Now I want to test my project. So, I want to use mocking stuffs in my test project.
Here is my project.json file
{
"version": "1.0.0-*",
"dependencies": {
"xunit": "2.0.0-rc3-build2880",
"xunit.runner.aspnet": "2.0.0-rc3-build52",
"My project name with version",
"Moq": "4.2.1502.911"
},
"commands": { "test": "xunit.runner.aspnet" },
"frameworks": {
"aspnet50": {
"dependencies": {
}
}
}
}
But I am getting error :Dependency Moq with the Version 4.2.1502.911 could not be resolved.
I also tried different version of Moq, but results the same: dependency error.
Without Mock I can't move further in testing.
Any help?
based on comment, the error is incorrect package manager settings :
the correct feeds to use are : nuget.org/api/v2 and myget.org/F/aspnetrelease for the asp ones

Legacy assembly reference error in asp.net-vnext

I am using VS2015 CTP5 and I am referencing a legacy class library compiled with 4.5.1. During compile, I get this warning:
The primary reference "D:\components.dll" could not be resolved because it was built against the ".NETFramework,Version=v4.5.1" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
Here is my project.json after adding the reference
"frameworks": {
"aspnet50": {
"dependencies": {
"components": "1.0.0-*"
}
}
},
Since the "component" library is build for .net 45 and assuming that you build that library in an older version of visual studio, it will not work in aspnetcore5 but will work on aspnet5 (these are the new version for .net). if you want to get rid of the error and still use your component library, you will need remove the aspnetcore5 json node from the project.json file but the project that you building will not be compatible with aspnetcore5. So your project.json file for the frameworks section should look like this.
"frameworks": {
"aspnet50": {
"frameworkAssemblies": {
"System": "4.0.0.0"
},
"dependencies": {
}
},
"net45": {
"dependencies": { "components": "1.0.0"},
"frameworkAssemblies": { }
}
}
And your reference should look like, I have warning sing next to the component library because I don't have that in my code.
You can look at this question to get more information.
Question 1,
Question 2
Add the library to frameworkDependencies not dependencies
"net45": {
"frameworkAssemblies": {
"components": "1.0.0"
},
"dependencies": {
// NuGet packages go here
}
For me, none of the above worked and after spending a lot of hours investigating... I finally found a solution!
I have to create a new package in the NuGet Package Explorer for my dll, save and export it to a local folder (use the File->Save and File->Export commands). Then declare my local repository(folder) to Visual Studio, go to Tools->Options->NuGet Package Manager->Package Sources and insert a record for my local repository - see image below.

Resources