netcoreapp1.0 does not find net461 specific package - c#

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
}

Related

How to create a chocolatey package for a .NET Core cli application?

I have a very basic cli application(that basically prints "hello world") written in C# and that uses the .net core runtime.
I tried to create a chocolatey package by:
running choco new hcli
modifing the generated .nuspec file manually to supply info(version, author...)
running choco pack
This produced a .nupkg file, when I run choco install hcli.0.0.1.nupkg I get ERROR: This package does not support 64 bit architecture.
I am suspecting that chocolatey does not support project.json based projects, the documentation does not mention anything about .net core.
What am I doing wrong?
project.json file:
{
"version": "0.1.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "hcli"
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win7-x64": {}
}
}
Chocolatey does not support visual studio projects nor project.json at the time of this post.
Fix the error
What you are seeing is a pretty common error if you have not set up or adjusted any of the packaging.
Have you reviewed the contents of tools\chocolateyInstall.ps1 after you generated the packaging? I would review and adjust those automation scripts that were generated (and review the readme).
If you don't need the automation scripts, simply remove them and have your binaries in the package.
As you have indicated, there is a much more drawn out article at https://chocolatey.org/docs/create-packages
Alternative Option - Use NuGet to pack
You can always look to use NuGet to generate the package and then consume it with Chocolatey. As long as it is compatible with NuGet v2 (currently), you should be good to go. The other aspect of that is that if you have dependencies at the DLL level, please include them in the packaging - dependencies are really at the application level. Like a dependency on dotnetcore package.

.NET versions in ASP.Core

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?

Newtonsoft.JSON v9.01 + FileNotFoundException (.NET Core Class library)

(VS2015 Update 3 + Patch)
I have a plain .NET console application (.NET 4.6) and reference a .NET core class library that targets NetStandard v1.3.
The class library has a reference to Newtonsoft.JSON.
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"buildOptions": { "platform": "anycpu" },
"frameworks": {
"netstandard1.3": {
"imports": "dnxcore50"
}
}
}
The referenced NewtonSoft.JSON package is deployed here:
C:\Users\UserAccount\.nuget\packages\Newtonsoft.Json\9.0.1
The Exception:
An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in DotNetConsoleApplication.exe
Additional information: Could not load file or assembly
'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The
system cannot find the file specified.
I guess the .net core lib would reference the dll from the netstandard1.0 folder.
Creation of NuGet package is a solution but not the easiest one.
Microsoft finally admitted this is a problem and will fix it, expectantly, in NuGet version 4.0.1, the first update to NuGet 4 after VS 2017 ships.
The cleanest workaround now is to add <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to a legacy project. However according to Rob Relyea MS will ignore this property after RTM so another workaround is <PackageReference Update="PlaceholderToConvinceThisProjectToGetTransitivePackageReferenceFromProjectReferences"/>.
Solved 31.07.2016
Create a fresh plain .NET console app (not .NET Core) and a .NET Core class library, without doing any referencing between them upfront.
Scenario:
1. Console app based on .NET 4.6, which references a
2. .Net Core Classlibrary (has a reference to Newtonsoft.JSON v9.01)
The .NET core class library is configured as follows (project.json):
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1"
},
"buildOptions": { "platform": "anycpu" },
"frameworks": {
"netstandard1.3": {
"imports": "dnxcore50"
}
}
}
Solution
1.) Create a Nuget package from the .Net core class library project first.
Open the command line as admin.
Go (cd) to the project folder of the .NET core class library project (.xproj).
Run the following command:
dotnet pack
The "pack" parameter will create a nuget package out of the .NET Core class library and copy the package to the debug/release folder, depends on your project configuration.
Copy the nuget package files to a folder where you host your local nuget packages.
I have copied them to:
C:\Users\Admin.nuget\packages\LocalPackages\NetCore46ClassLibrary
Screenshot:
2.) If you don't have a local Nuget feed,you have to create one first!
The local Nuget folder (I named it "LocalPackages") will host your custom Nuget packages. The local Nuget Feed will point to "LocalPackages", which is the root folder for all local packages.
After you have created the local nuget feed and copied the nuget package of your .net core class library somewhere beneath the localPackages folder, you are ready to install your .net core class library nuget package.
3.) Install the .NET Core library Nuget Package into the .NET console app
you now have to open the Package Manager Console again.
Choose Package Source: Local Packages (This is my local feed name, may be different).
And the default project should be your .NET console app.
Install your .net core class library nuget package into the console app, in my case:
install-package NetCore46ClassLibrary
That's it !
My system:
dotnet --version
1.0.0-preview2-003121
Could be that the Newtonsoft assembly is 64 bit and your .Net Core project is 32 bit. Also could be that you have multiple versions of Newtonsoft referenced.
I had the same error recently, after including Newtonsoft.Json 6.0.8 in a dotnet core console app. The solution was to include the System.Runtime.Serialization.Primitives dependancy to the project.json config.
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"System.Runtime.Serialization.Primitives": "4.0.10-*",
"Newtonsoft.Json": "6.0.8"
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
}
}
As a workaround solution, In visual studio 2017, you can modify the the NetStandard project .csproj as multi-target:
<TargetFrameworks>netstandard1.3;net461</TargetFrameworks>
Rebuild the solution and the referenced dll (NewtonSoft.JSON) will be copied to bin folder of the console project.
Have a look to my implementation in :Workaround Solution

How do I use System.Data in a .NET Core RC2 console app (Linux, Debian 8)?

I've installed .NET Core RC2 on a Debian 8 amd64 system and would like to test if it's possible to query an instance of Microsoft SQL Server.
So I'd like to add to my project a dependency on the System.Data.SqlClient assembly.
Presently my project file created by running the dotnet new CLI tool looks like this:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Using this answer to a similar query, I was able to add a reference to System.Data.Common changing the
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
fragment to
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.Common": "*"
}
}
}
which made dotnet restore use NuGet to download a bunch of stuff.
I then tried to change that fragment to read
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.SqlClient": "*"
}
}
}
but NuGet says it's
Unable to resolve 'System.Data.SqlClient' for '.NETCoreApp,Version=v1.0'.
If I change the version string to read "4.1.0-rc3-*" the error message just gets more specific:
Unable to resolve 'System.Data.SqlClient (>= 4.1.0-rc3)' for '.NETCoreApp,Version=v1.0'.
What I'm puzzled about is that the NuGet package gallery dedicated to .NET Core explicitly lists System.Data.SqlClient as available.
So what could I do to add a reference to System.Data.SqlClient assembly to my project and have NuGet download it?
On a side note, I'm currently playing around in a plain console with only the dotnet CLI tool. Is there any way to manage project dependencies for a .NET Core project without resorting to installing IDEs?
Like poke already annotated in the comment is correct. Specify a version to System.Data.SqlClient makes your restore happy ;)
Why is that? System.Data.SqlClient exists in the http://nuget.org gallery. Not specifying a version ("") is not allowed outside of the boundaries of a project (like a nuget feed package) and specifying solely an star "*" (you should never do that, it allows breaking changes) restore the highest available version. Since there is no stable, star will not find anything (there is some magic with the dashes behind). The RC2 version of that library is the mentioned 4.1.0-rc2-24027 and when you ask with 4.1.0-rc2-* it will take the highest of the RC2 builds (but there is only one). In comparison System.Data.Common has a public release on nuget.org for the Universal Windows Platform and is found for that reason.
The RC3 is the next release and only available on developer feeds from the .NET Core and ASP.NET Core team and not the public nuget feed. You should not play with them.
if you are in project.json file, the intellisense guide you now if you have updated the Visual studio with the latest tooling avaiable..
I added the following in the dependencies element and it works perfectly..
"System.Data.SqlClient": "4.1.0-rc2-24027",
On my MINT 19 Tara I do not use System.Data.SqlClient but the newer version Microsoft.Data.SqlClient.
Check nuget
After typing the .NET-cli command, the package was downloaded and the reference was added to the project csproj file.
In the code, use the newer namespace:
using Microsoft.Data.SqlClient;
The "regular" code works fine:
public static void CreateCommand(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader() ;
while( reader.Read() ) {
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine( "Column name={0}, Value={1}",
reader.GetName(i),
reader.GetValue(i) );
}
}
}
}

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