.NET5 on OSX - C# errors in DNVM shell (so close..) - asp.net

I'm trying to get an old project to run on the new .NET VM, I've gotten through a few obstacles at this point. For a while I couldn't get DNVM to recognize my project as a .NET project, until I added a couple missing files from a sample project, and used dnvm . kestrel instead of dnvm . web and then I got back about 80mb worth of C# errors...
So I went from not being recognized as a project, to being recognized as a shitty one :/
Anybody beat this level yet? Thank you in advance.

Two questions for you
When you say that you tried dnvm . kestrel, do you mean that you tried dnx . kestrel?
Did you run dnu restore to download dependencies before running dnu . kestrel?
Three command line programs
There are three command line programs that you will use to run a .NET 5 app, and it's easy to confuse them. dnvm is the version manager, dnu is the utility, and dnx is the runtime. It's the runtime not the version manager that you use from your project's root folder to start the kestrel web server.
Startup up an app
Here's the very short version of how to start up a .NET 5 app. After using dnvm to install .NET 5, and after creating a project (and optionally a solution,) you need to use both dnu and dnx in this sequence.
From your solution's folder, run dnu restore. This will download your project's dependencies. If you have only a project and no solution, run this from your project's folder instead.
From your project's folder, run dnx . kestrel to run the web application in the browser.
It sounds like you did number (2) without having first done number (1).
Let me know whether you're able to reach the next level. Also, have you read these articles?
http://docs.asp.net/en/latest/getting-started/installing-on-mac.html
http://docs.asp.net/en/latest/tutorials/your-first-mac-aspnet.html

Related

dotnet build stopped working after VS 2022 upgrade

I upgraded Visual Studio Community to the final release, and it stopped working. Specifically, I could generate a new project, but when I try to load it, it gave me an error
Critical Project 'SdkTest' load failed| [MSB4236] The SDK 'Microsoft.NET.Sdk.Web' specified could not be found. ...\SdkTest.csproj
Even though build and run worked fine
I ran "repair" on VS2022, and it seems to be working now from Visual Studio. However, dotnet --info doesn't show any SDKs; only runtime; and dotnet build or dotnet new give an error:
C:\Code>dotnet new console -o myapp
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
* You intended to execute a .NET program:
The application 'new' does not exist.
* You intended to execute a .NET SDK command:
It was not possible to find any installed .NET SDKs.
Install a .NET SDK from:
https://aka.ms/dotnet-download
I even reinstalled .NET 6 and I see it in the list of applications (along with older SDKs). I checked the path, and I see everything that I expect to see:
C:\Program Files (x86)\dotnet\
C:\Program Files\dotnet\
C:\Users\me\.dotnet\tools
I am running Windows 11.
It's known bug in VS2022 installation.
Problem is that dotnet you access is located inside Program Files(x86)/dotnet, but all sdk's are located in Program Files/dotnet. You simply need to edit PATH environment variable so Program Files/dotnet goes before Program Files(x86)/dotnet. If you don't see Program Files(x86)/dotnet in user environment variable (which is likely to happen) then edit system environment variable (located below)
Example with screenshots: https://stackoverflow.com/a/44272417/10339675

dotnet publish sln having projects with multiple target frameworks fails

I have a solution with many projects. Some target frameworknetcoreapp2.1, some other target framework netstandard2.0 and one project has a double target framework
<TargetFrameworks>netstandard2.0;net471</TargetFrameworks>
I'd want to have a artifact for win10 with a single command:
dotnet publish MySolution.sln -c Release -o "targetFolder" -r win10-x64
With this command I have this error while building the project with double target framework. Here's the errors:
C:\Program Files\dotnet\sdk\2.1.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.CrossTargeting.targets(31,5) error : The 'Publish' target is not supported without specifying a target framework. The current project targets multiple frameworks, please specify the framework for the published application.
The error is clear. At the end I find that dll compiled in the output directory and it seems like it is a netstandard2.0 dll because my application still works.
I don't like dirty things so, how can I solve my problem?
I would avoid to call N times the "dotnet publish" command if possible.
Don't use dotnet publish with the same output directory on a solution. Especially not with the "-r" argument.
It is dangerous because:
libraries don't have the right trimming behaviour for netstandard facade packages
libraries may have odd behaviour when publishing with "-r", especially for netstandard<2.0 dependencies. (they'd end up copying the .NET Core 1.0/1.1 implementation(!) assemblies)
you may end up with different NuGet dependencies in the output (transitive dependencies)
Copy-to-output/publish-directory items may end up overwriting each other, it may even lead to build failures
Call it individually for all application (console app, web app) projects or create an MSBuild file that publishes these applications.

Self-contained deployment .NET Core app in Ubuntu

I wrote a test project using .NET Core and assembled the self-contained deployment for Ubuntu 16.04 as described here (see Self-contained deployment without third-party dependencies).
But when I run the app I get the following error:
An assembly specified in the application dependencies manifest (Test.deps.json) was not found:
package: 'runtime.linux-x64.Microsoft.NETCore.App', version: '2.0.0-preview2-25407-01'
path: 'runtimes/linux-x64/lib/netcoreapp2.0/Microsoft.CSharp.dll'
I am using .NET Core 2.0 Preview 2, VS2017 Preview.
I will be grateful for any help!
This is an old question, but I just ran across this when I was trying to run a .Net Core application on Linux and wanted to share the solution. If you are getting the error above, you are likely trying to execute the wrong binary. For those following along from scratch, follow these steps:
On Windows, open a command prompt in the directory of the project you want to run on Linux.
Build the project for Linux using dotnet publish -r linux-x64
I chose to target linux-x64, but you can target a specific runtime if you'd like. Runtime identifiers can be found here.
Copy the published files to the Linux workstation. Because the above command omitted the configuration flag -c, the configuration defaulted to debug. The published files will be in Debug\netcoreapp2.0\linux-x64\publish
Note: there will be binaries in Debug\netcoreapp2.0\linux-x64\ too. These are not the binaries you want to copy to your Linux workstation. If you run these binaries, you will get the error described in the OP. Copy all the files in the publish directory instead. Ignore whatever files might be in linux-x64.
On the Linux workstation, give execute permission to the binary file. My project was named ConsoleUI, so I used chmod 764 ConsoleUI
Execute the binary using ./ConsoleUI
Keep in mind that you will need to at least have the .Net Core runtime installed on your Linux workstation.

.NET versions in ASP.NET 5

In the new ASP.NET 5 projects, there are multiple ways / places to control the .NET versions:
In global.json
In Project -> Properties, Application tab, Solution DNX SDK Version (this is the same as the global.json)
In Project -> Properties, Debug tab, Use Specific Runtime
In Package Manager Console, using dnvm list
In a normal console in the application root, using dnvm list
Which of these are the same (apart from the first two) and what do they all do?
The dnx in global.json is only used by VS. No one else uses it and if you run the application outside of VS, there is no guarantee that it'll use that version.
The dnx used to run a particular application is set in two ways:
Either pass the full path to a particular dnx. E.g: C:\dnx\dnx.exe . run
The dnx on the PATH resolved according to your OS' PATH resolution (we don't control that).
When you run dnvm use <version>, that particular version is added to the path and it will be used by that particular process and it's child processes. If you run dnvm use -p <version>, that version of dnx is added to the user's PATH in addition to the process' PATH.
For VS, if no version is specified in global.json by default it uses the dnx under the default alias. The default alias is updated when you run dnvm upgrade or dnvm use -p

dnx and asp.net 5 compiling for distribution

When I use dnx I can run the command dnu build which generates a DLL from my Console Application. How can I re-use that in another computer e.g. linux? As I understood, I must include the project.json so that in the linux I will run dnu restore but I can't understand how do I run it without the code because when I run dnx . run I get
'ConsoleApplication' does not contain a static 'Main' method suitable
for an entry point
so apparently you can use dnx --lib path/to/output/dir . run with your project.json of course and it will load your dll's from that folder, without the source code available on the server.
posting this so it might help someone

Resources