dotnet run command does not find user secrets - asp.net

I have a asp.net core project running on a macbook. I'm using visual studio code as my code editor. When I run the project from visual studio code the application boots fine however when I execute dotnet run from the terminal in the project's working directory I get an error that the database hostname value is null. This value is defined in user-secrets. Is there an extra setup I'm missing from the command line?

If it is loading the user secrets only in Development as is normally done, you have to remember to set the environment variable ASPNETCORE_ENVIRONMENT to Development.

Related

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 do I run an ASP.Net Core executable file?

I am new to ASP.Net Core. I have been following a tutuorial. I got to the point at which Javascript code was added to my project. I ran my program, and nothing in the script worked. After adding a few alert() calls, it was apparent that my program was not finding my script. People here and at Reddit suggested I look at the network tab of DevTools. I saw that my program was not able to find any js or css files.
Being an old-school C++/C# developer, I was running the program from a command window by going to the folder containing the executable file and typing the name of the file.
I went back to the beginning. The tutorial directs me to use the dotnet command, which is brand new to me. The first time it tells me to run my program, it tells me to go to the project directory and enter "dotnet run". It just hit me like a ton of bricks that that was what I should have done the first time. I went back to that project, used "dotnet run", and it worked.
So, my question is this: is it possible to run MyProject.exe by itself? If so, how? Copy it into my project folder? Or is the "dotnet run" command the standard way of executing an ASP.Net Core application?
I think it won't work if you directly run it (not sure if we can run MVC project using dotnet-cli (dotnet run will try to load only MSIL to CLR)).
DotNet core console application is different from .net core MVC projects
you can run your project by following ways.
1. Run using visual studio
when you create an MVC application, in the visual studio there will be multiple options to run it, you can choose IISexpress one, it will open in the default browser.
for step by step guide -> https://learn.microsoft.com/en-us/visualstudio/ide/quickstart-aspnet-core?view=vs-2019
2. publish it in IIS
For this you need to ensure few things, like hosting bundle is installed or not, IIS is correctly configured for core application or not.
for step by step guide -> https://stackify.com/how-to-deploy-asp-net-core-to-iis/
3. Use VSCode
for step by step guide -> https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-azure-webapp-using-vscode?view=aspnetcore-3.1

How to build .NET CORE project in VSCode or command line?

I tried to switch from Visual Studio to Visual Studio Code with my CORE project. I can build the C# project in Visual Studio without any problems. When I tried VSCode, I see that I need to define tasks. Fortunately, VSCode offers default tasks definition (it generates tasks.json) for me, so I tried them (there are variants for CORE and .NET, as I understand). But both don't work. I tried to make build from command line, but get error (I translate the error to English, it's not original text):
C:\Program Files\dotnet\sdk\2.1.700\Microsoft.Common.CurrentVersion.targets(3046,5): error MSB4216: can not execute task "GenerateResource", because MSBuild cannot create tasks server with execution environment "CLR4" and architecture "x86" or to connect to it. Verify that, (1) required execution environment and architecture do exist and (2) required executable file "C:\Program Files\dotnet\sdk\2.1.700\MSBuild.exe" exists and can be ran. [D:\prj\...\Test.csproj]
and when I tried to find MSBuild.exe in this folder, I found MSBuild.dll only, no any MSBuild.exe. So, I tried to run it with dotnet after entering this folder - it works. But if I try to call dotnet build or to run it with full path to MSBuild.dll from my project folder, I always get this error. Seems, that dotnet command calls MSBuild.dll multiple times and always hit this problem. May be I should setup some paths or similar. But where to do it (without to break ability to build the project in Visual Studio)? What is the convenient way to build CORE projects in Visual Studio Code? How to build them in command line?
PS. May be all documentation which I found was outdated...

Which process Visual Studio has to attach to run DNX process?

I ran a Web API that I created with the command dnx web. The service is running fine and I get the result too.
I want to attach the Visual Studio Process to this dnx process and debug the API call. Which process I need to attach to?
Use dnx-watch. this show the dnx process.
It also automatic restart the server after code change
A fast way to debug is to insert Debugger.Launch(); in your code, on the place where you want to start to debug.
It is faster than attach to the right process
dnx-watch can be installed by
dnu commands install Microsoft.Dnx.Watcher

Same MSBuild file, different results on local machine and CCNet

Background
I am utilizing the following:
VS 2012
.NET 4.5 apps including an MVC4 app, a C# Domain app, and NUnit projects.
A custom MSBuild File
CruiseControl.NET running on a build server
I have the following build targets:
Clean: deletes the buildartifacts directory
Init: creates a blank buildartifacts directory
Compile: compiles the solution
Test: runs NUnit Tests
Package: creates a ZIP package for deployment
Deploy: Deploys package to a remote IIS Server
Problem
When I run the build from my local machine, it packages and deploys perfectly.
When CCNet runs the same build target, the files appear to be deployed
Attempted Resolutions / Potential Leads
I have run the same target in PowerShell from both machines and have seen no errors or warnings on either.
Because I tell CCNEt to override the framework to point it to the .NET 4.5 files I've copied to the build server, I made my local machine point to the same files when running MSBuild but my local build still worked and deployed.
In Visual Studio, I have ensured that all content files are marked with a Build Action of "Content" and a Copy to Output setting of "Copy Always".
The Code
Available in a gist at https://gist.github.com/53ef2a63931d190593f6
(file has been scrubbed, app name & credentials replaced, etc.)
It turns out, this was a completely unrelated issue.
After doing a diff of both build logs, I noticed that the build server working copy contained old files that no longer existed. This led me to discover that the issue ended up being that the repository wasn't being updated.
CCNet was receiving a subversion error that wasn't registering as an error
As a result, I was never getting the latest version of the repository, and so all files were broken.
The error is related to this StackOverflow question, regarding a sqlite error when attempt to use tortoisesvn to update. The solution in my case was to simply delete the build server's working copy, recreate the folder, and perform an svn checkout.

Resources