Debugging and rebuilding a .net core web app - asp.net

How do I actually run the debugger with a .net core web app using visual studio along side the dotnet watch tool?
I am using the dot net watch tool which greats work and picks up changes, however if I try to attach the debugger, the code doesn't match with what's compiled. So I have to ctrl-c and stop the hosted app, rebuild and rerun donet run and reattach the app to the dotnet process. Surely this can't be the workflow of .net core developers?

Related

Why all .NET Core applications are console applications?

I have a very basic question about why all .NET Core applications are console applications. I did some google search but don't find any appropriate answer.
It's an interesting question, and I got here because I had the same thought: Why do I seem to always see a console on asp.net core apps? Are they all now console apps?
But it's a mirage. While you are seeing a console window, it's only because the code is using the console for output. The code that starts the app governs where the output is going to. For example, instead, the output could be ported to the Windows Event Log, which is what happens when the site is published to IIS.
If you create a default .netcore web app, and you look in the output directory, you will see two files:
YourProject.exe
YourProject.dll
If you double click the exe, it will start the website. You will see the console window appear and the app will start listening on port 5000.
But now rename YourProject.dll and try to run the exe. It will fail because the exe is just the startup code. Your website is actually in the .dll.
The point is that the console window you see when you start an web app is coming from the exe, not the dll. So the console window has nothing to do with your web app, it's just a convenient way to start up the app in a dev environment. You can also start it from the command line with dotnet YourProject.dll and it will run exactly the same as if you started it from YourProject.exe
If you read #HansPassant's comment, he says it exactly correctly, my answer is just an expansion of it.
.NET Core provides more than console applications out of the box, it has supported both WPF and WinForms since version 3.x. In the below documentation links from Microsoft you can see .NET 5.0 (which is .NET Core) is listed. If you're using Visual Studio you can chose between ".NET" (which is .NET Core and what it will be referred to going forward) and ".NET Framework" (which won't have any versions past 4.8). It should be noted, WPF and WinForms although on .NET Core are only supported on Windows.
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/?view=netdesktop-5.0
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/?view=netdesktop-5.0

dotnet watch run blazor wasm loading pdb symbols

Should I be able to attach the debugger to a blazor wasm project using the visual studio 2019 professional edition? When I use dotnet watch run, I can't get the symbols to load to hit the breakpoints. It works fine with iisexpress but not when I use dotnet watch run. Any ideas or articles to look at would be appreciated.

Difference between two dotnet cli commands: dotnet "app.dll" vs dotnet run "app.dll"

I found little weird worth asking the community and get an understanding.
I have Dotnet Core 5.0 Web Application as well as Dotnet Core 5.0 Web API Project. Both are plain vanilla coming straight from Visual Studio 2019 boilerplate template.
I press both one after another from visual studio (press F5 by kipping as default) and make sure it works.
Web Application gives home page while
Web Api gives the swagger output for WeatherForecast API
Now, I want to test the same using Visual Studio Command Prompt through Dotnet CLI command.
First, I tried with Web Application.
I navigated to the path I have Web Application and I execute dotnet "bin\debug\net5.0\webapp01.dll" and it ran. I can see the web application in my browser.
So far so good.
Second, I tried with Web API.
I navigated to the path I have Web API and I executed dotnet "bin\debug\net5.0\webapi01.dll" and it ran. I opened the browser and check the same swagger page and OOPS. 404 error page. nothing is coming. I checked the command prompt and it's still running but no output.
Alternatively (while I troubleshooting), I gave tried to execute dotnet run command for Web API
dotnet run "bin\debug\net5.0\webapi01.dll" - interestingly it prints an additional line in the command prompt than the previous (without run command) one - the line was "Building..." rest everything as is.
I checked the browser with the same swagger link and voila - it's giving me an output.
Well - to check I did the same with Web Application dotnet run "bin\debug\net5.0\webapp01.dll" - that also works with the correct output.
This leads me to ask the questions?
If we should use dotnet "dll" then why is it not working for the web API project?
What is so different running dotnet "dll vs dotnet run "dll"?

How to run ASP.NET Core Web application on Linux?

I have an ASP.NET Web application that was successfully running on Linux with the help of dnx-mono NuGet package. I used to publish it by using dnx publish tool and run it with the command:
/opt/dnx-mono.1.0.0-rc1-update1/bin/dnx --appbase /path/to/json Microsoft.Dnx.ApplicationHost kestrel
Since dnx is obsolete, I want to get rid of it and migrate to .NET Core, but it does not work anymore on Linux.
Important: I cannot make my application .NET Core app because it uses some libraries (like MySQL.Data) that are still not available for .NET Core. Therefore, I create ASP.NET Core Web Application (notice .NET Framework):
I migrated the whole application and run it successfully on Windows, but my target is Linux. No more dnx publish, no problem - there is dotnet publish which works well and produces binaries. What now? Dnx-mono package does not have a "dotnet-something" successor, and using dotnet command with published binaries produced errors that I was not able to resolve (The library 'libhostpolicy.so' required to execute the application was not found), but I don't think that I should even use dotnet command with standalone binaries. Googling didn't help as most tutorials are focused around ASP.NET Web applications for .NET Core, not for .NET Framework. Version of Linux is Debian 8, dotnet command was obtained by following this guide. Any clue?
You cannot run an application on Linux that targets the full .Net Framework, it has to be Core only.

How to run ASP.NET MVC app using MSBuild command line

How do I compile and run an ASP.NET MVC app using nothing other than the MSBuild command line? My Visual Studio is super slow and I just want to be able to run an app quickly for bugfixes and showcasing etc. Is this possible? What's the command line for it?
MSBuild is for building your application. If you want to run your app outside of Visual Studio, you need a web server for it. I'd recommend you to publish your app on local IIS. Just point your IIS website to your Web project folder. After that you will be able to access your application from the browser without running it from Visual Studio.
You can build your project from command line. Use msbuild.exe utility for it:
msbuild.exe projectname
You can find it in one of your .NET Framework folders (I recommend you to use the latest one):
C:\Windows\Microsoft.Net\Framework\v2.0.50727\MSBuild.exe
C:\Windows\Microsoft.Net\Framework\v3.5\MSBuild.exe
C:\Windows\Microsoft.Net\Framework\v4.0.30319\MSBuild.exe
C:\Windows\Microsoft.Net\Framework64\v2.0.50727\MSBuild.exe
C:\Windows\Microsoft.Net\Framework64\v3.5\MSBuild.exe
C:\Windows\Microsoft.Net\Framework64\v4.0.30319\MSBuild.exe
When IIS is set up, you can build your app and see changes in your browser. Visual Studio is not necessary at this point.
Instead of IIS you can use IIS Express. Here is an article of how to run application on IIS Express from command line.

Resources