Add DLL reference in dotnet cli - c#

Is it possible to reference a c#/.net nuget package or dll using .net core cli tools?
https://github.com/dotnet/cli
None of the documentation seems to explain how to do it.

You add your references by simply editing the project.json file. Just add all of your references and nugets there. Then you can use the cli command to download them into your project by using the
dotnet restore
Here is an example project.json file. Under dependencies, you can see some nugets. Under framework assemblies, you can see a DLL reference, System.Data.
{
"dependencies": {
"EntityFramework": "6.1.3",
"Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
"Microsoft.Dnx.Runtime": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"PimProject.Common": "1.0.0"
},
"frameworks": {
"dnx451": {
"dependencies": {
},
"frameworkAssemblies": {
"System.Data": "4.0.0.0"
}
}
},
"dotnet": {
"dependencies": {
"System.Data.SqlClient": "4.0.0-rc2-23530",
"System.Data": "4.0.0"
}
},
"version": "1.0.0-*",
"description": "Class Library",
"authors": [ "sadams" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": ""
}

Starting on version 1.0.1, dotnet cli allows to add references to packages and projects using add command:
dotnet add package EntityFramework
To add a reference to project Proj2 in Proj, move to Proj folder and use
dotnet add reference "..\Proj2\Proj2.csproj"

Related

create a nuget to share between linux and windows

I have an asp.net form application using .net 4.6.1 running on windows
and a linux application using .netcore 1.1
I want to make a nuget that can be shared between both application.
I created a .net core library and it works pretty fine with my linux application
here is its project.jso
{
"version": "0.1",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netstandard1.4": {
"imports": "netcoreapp1.0"
}
}
}
I can add my nuget package to the windows application and build it with no error, but when I want to run the application I'm getting all erros like:
Could not load file or assembly 'System.Diagnostics.DiagnosticSource,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
i'm using .net 4.6.1 so I believe it's .net core that is nagging
i can check in here and confirm that it's a .net core library.
I also setup the .net library in the production server. but still i'm getting the same error.
I tried all possible combination and version that i thought it would be logical. and have no clue how to solve it.
Im not sure why you say you are using net 4.6.1. Because you are using netstandard1.4 as framework, looking at your project.json file.
Try the following project.json file:
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.6.0",
"Newtonsoft.Json": "9.0.1",
"StackExchange.Redis": "1.2.0"
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
}
}
}
Make sure the production server has the runtime installed and Microsoft.NETCore.App-1.1.0

Reference .NET Core 1.0 library from WPF

I am currently developing a desktop application in WPF, which uses an .NET Core library to make porting to different platforms easier. However, I cannot seem to reference the .NET Core library from the WPF app.
I tried the follwing solutions:
Reference the project:
Visual Studio complains about the project not being an .exe or .dll even though it is.
Reference the compiled .dll: This is really ugly, but it seems to work at first. Intellisense is OK with it and the WPF project compiles just fine. But as soon as I want to use any functionality from the .NET Core project a BadImageFormatException is thrown.
dotnet pack the project and reference the .nupkg: Installs a bunch of additional packages and throws a BadImageFormatException when any functionality is used.
From what I can gather there are two options here:
Do something really hacky like making a .NET Core Console project and passing all objects as strings between the two programs
Or:
Just give up on .NET Core and use EF6.
Here's my project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
},
"dnx451": {}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
I tried both dnx451 and net451. The WPF project is also targeting .net 4.5.1. I am using "Visual Studio 2015 Update 3" with ".NET Core 1.0.1 VS 2015 Tooling Preview 2".
Your project.json isn't correct for a library. A library project should look like:
{
"dependencies": {
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
"Microsoft.EntityFrameworkCore.Design": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
"frameworks": {
"net451": { },
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
},
"tools": {
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
}
}
When you run dotnet pack, two DLLs will be produced: one for .NET 4.5.1 and one for .NET Standard 1.3 (or whichever netstandard you want to target). The .NET 4.5.1 DLL should be compatible with your WPF project.

C# Visual Studio .NET core - Not finding Nuget-packet type

I am creating a .NET core (version 1.0.0-rc1-update1) console application with VS 2015 community (latest version). Building and running the app went fine until I wanted to use any System.IO type (can not use System.Threading types either).
The name 'File' does not exist in the current context
I added the System.IO package with NPM and it added it to the dependency in the project.json. The project itself is running with dnx but Visual Studio can not build it.
project.json
{
"version": "1.0.0-*",
"description": "",
"authors": [ "Widi" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"OfcCore": "1.0.0-*",
"System.IO": "4.0.11-beta-23516",
"System.Text.RegularExpressions": "4.0.11-beta-23516"
},
"commands": {
"Ofc": "Ofc"
},
"frameworks": {
"dnx451": {
},
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Console": "4.0.0-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}
}
}
How do I get my project to also build and run in VS?
edit: If I remove dnxcore50 VS builds like normal. It also seems like it can only not find the System.IO and System.Threading packages. System.Text.RegularExpressions works fine. Is there any special dependency I am missing?
Notes:
VS Community 14.0.24720.00 Update 1
ASP .NET and Web Tools 2015 (RC1 Update 1)
Whenever a class is missing, please use http://packagesearch.azurewebsites.net to locate the package you should add to project.json. Also you should only put NuGet dependencies under dnxcore50.

Restore package failed for `Microsoft.Dnx.Compilation` in asp.net 5 rc1-final

I am using Dnx compilation engine to compile a c# package class library.but when i add a reference to preceding package in project.jsonfile i end up with following message :
The dependency Microsoft.Dnx.Compilation 1.0.0-rc1-final in project
[x] doest not support framework .Net Framework Version v4.5.1
But i toke a look at DNX source code and saw a reference for framework v4.5.1 there.following picture is an evidence for what i've mentioned.
Any idea why this happens?
{
"version": "1.0.0-*",
"description": "Tourism.Framework Class Library",
"authors": [ "Behnam" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"frameworks": {
"net451": {
"frameworkAssemblies": {
"System.Data": "4.0.0.0"
}
}
},
"dependencies": {
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-rc1-final",
"Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final",
"Autofac": "4.0.0-rc1-177",
"Microsoft.AspNet.Mvc.Razor": "6.0.0-rc1-final",
"AutoMapper": "4.1.1",
"log4net": "2.0.4",
"Microsoft.Dnx.Compilation" : "1.0.0-rc1-final"
}
}
The Microsoft.Dnx.Compilation package targets DNX 4.5.1 (dnx451) and DNX Core 5.0 (dnxcore50) as it requires DNX. It does not target the standard, full .NET framework v4.5.1 (net451).
If you want to use it, you should target dnx451 in your project.json. This is the preffered target framework: Apps should target dnx451 and class libraries should target net451 (see this issue).

Reference to DNXCore5 Error, StringComparer .NET5

New to .NET5, so not sure if this is something simple or not. I have 5 other projects in my solution, which all have this in the project.json file
"frameworks": {
"net5": { }
}
I needed to reference net5 because i am using EntityFramework, and for some reason it wouldnt work if i had the default "dotnet".
Now my website project has a slightly different frameworks tag. I have included all of it encase i am missing something else, but as you can see it is referencing DNX5 and DNXCore5 (Not sure why)
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta5",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta5",
"Microsoft.AspNet.Mvc": "6.0.0-beta5"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --config hosting.ini"
},
"frameworks": {
"dnx50": { },
"dnxcore50": { }
},
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
],
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
]
}
But i have an error when i am trying to reference use
StringComparer.InvariantCultureIgnoreCase
See the screenshot below
If i look under References in the project, it appears that the DMXCore is referenced correctly and appears, as you can see here
See this question for full details.
dnxcore50 - DNX SDK running on CoreCLR/CoreFx
dnx451 - DNX SDK running on .Net 4.5.1 (Desktop CLR / Full BCL and FCL)
net46 - .Net Framework SDK running on .Net 4.6 (Desktop CLR / Full BCL and FCL).
uap10.0 - UWP SDK running on .Net Native/CoreFx
dotnet - any pure IL code which declares its dependencies (instead of a PCL contract). Framework dependencies are available for .Net 4.6, DNX or UWP.
For .NET 4.5 you need to use dnx45 for ASP.NET projects and net45 for other projects to target .NET 4.5 which is what I think you are doing according to your other question.

Categories