Reference .NET Core 1.0 library from WPF - c#

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.

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

Build .Net Core as an EXE not a DLL

I want to build a .NET Core project as a EXE and not a DLL so it can be executed.
The answer here did not work: How to run a .Net Core dll?
Here is the sample code:
using System;
namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Here is my project.json:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
}
I'm currently using VSCode, whenever I build the project with the build task, or run dotnet restore I just get a .dll in my bin/Debug folder.
How do you build a .NET Core application as an exe?
Bonus: I do, will that run on a mac or other device?
I think most people got to this page because they picked .net core and they can't get an executable .exe file from their VS 2017/VS 2019 build. vs 2015 always used to make a .exe file for a console application. With vs 2017/vs 2019 when you create the project you get 2 choices for a Console application. One is Console App (.NET Core) and the other choice is Console App (.NET Framework). If you pick the .NET Core option you are going to have to move heaven and earth to get a .exe file from your Build. The (.NET Core) option creates a .dll file from a Build. If you pick the (.NET Framework) option it will build a xxxx.exe executable for you by default.
To produce an EXE instead of a DLL, you need a self-contained deployment. What you are currently doing is a framework-dependent deployment.
To convert yours to self-contained, take the following steps in your project.json file.
Remove "type": "platform".
Add a "runtimes" section for the operating systems your app supports.
When you build, pass in the target operating system. E.g. dotnet build -r osx.10.10-x64.
This is the resultant project.json
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win10-x64": {},
"osx.10.10-x64": {}
}
}
See also: https://learn.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd

Why does my .Net Core console application have the wrong build target? [duplicate]

Every time I build a project using the new .NET Core RC2 templates I am not provided with a runnable .EXE file. If I hit F5 for debugging my console application it runs fine through the
C:\Program Files\dotnet\dotnet.exe
application. And if I use the
dotnet run
command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.
The contents of my
bin\Debug\netcoreapp1.0\
folder looks like this:
As you can see there is no .EXE file available. Just the dll.
Am I overlooking something? Or is there something wrong with my project.json file?
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks!
There are actually 2 app models in .NET Core:
Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exe files and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.App package, thanks to its special type: platform attribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.
Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exe files. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.
You're currently using the first model. To use the standalone model, you need to tweak your project.json:
Add a runtimes section to list the environments your app will target (e.g win7-x64 or ubuntu.14.04-x64). You can find the complete list here.
Remove the Microsoft.NETCore.App dependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
Here's an example of a standalone app:
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"net451": { },
"netcoreapp1.0": {
"dependencies": {
"System.Net.Ping": "4.0.0-rc2-24027"
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net451+win8"
]
}
},
"runtimes": {
"win7-x64": { }
}
}
The answer is in the documentation with complete steps now.
You can create two types of deployments for .NET Core applications:
Framework-dependent deployment
Self-contained deployment
For a runnable .EXE file, the Publish self-contained should be used.
To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:
dotnet publish --runtime win7-x64
This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64 or ubuntu.16.04-x64.
If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.
step 1: remove "type": "platform", from Project.json under frameworks section
step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.
"runtimes": {
"win10-x64": {}
}
Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)
step 4: dotnet pack
step 4: dotnet build -r win10-x64 - or just build.
Step 5: you can notice .exe created under debug/netcore/win10/
In ASP.NET Core try changing your app type to default, in project.json:
"Microsoft.NETCore.App": {
"type": "default",
"version": "1.0.0-*"
}

What's difference between .NetCoreApp and .NetStandard.Library?

.Net Core 1.0 has been released couple days ago, and i've started playing with it. I've created simple solution, with one project (class library => .NetStandard.Library) and second, console application (.NetCoreApp). The point is, console application has reference to library, but i cant use types form that library. Are those two frameworks incompatible? Am i missing something?
project.json for console application:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"ConsoleApplicationLibrary": "1.0.0-*",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
project.json for library:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}
I've figured it out, that it works, and code compiles, but visual studio still highlights types from library as unknown.
The most likely issue is that .Net Core is expecting ConsoleApplicationLibrary to be a NuGet package. If you want to reference a project, use "ConsoleApplicationLibrary": {"target": "project", "version": "1.0.0-*"}.
After you do that, don't forget to restore packages.
Ok, it's strange, but after disabling resharper, restarting VS and rebuilding solution, it works fine. It seems there is prolem with resharper support for .net core. (resharper v.9.1.3). From this: resharper ultimate blog i understand that only ultimate version supports .net core for now.
.NetCoreApp is a platform and .NetStandard.Library is a library supposed to be cross platform (portable class library) for various .NET platforms runtimes.
You can include a direct reference (package) of NetStandard.Library in any of your .NET platform project that is supported, for ex .NETCoreApp (Dot Net Core 1.X)
Reference: https://learn.microsoft.com/en-us/dotnet/articles/standard/library
I had the same issue and it turned out I needed to update Resharper to it's latest version. I had v9.1.1 so I updated it to 2016.3.2 and that fixed the issue.

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.

Categories