.NET Core: alter build process to execute a script - .net-core

How to instruct the build process to run a cmd or PowerShell command/script before or after specific project in .NET Core solution?
I want this command to be executed regardless of the build environment (Visual Studio, dotnet command, or TFS builder).

https://learn.microsoft.com/en-us/dotnet/articles/core/tools/project-json#scripts is the extension point. I did not see the documentation page before asking the question.

Related

How do I integrate exe publishing with the VS2019 build of the console .net core 2.2 application?

I saw a couple of similar questions but so far I found no single answer to the problem of integrating the publishing step with my build process. Unfortunately the dotnet publish command rebuilds the project again meaning that if I put the "dotnet publish" command in the project's Post-Build steps I get an infinite loop of building retries.
What I want to achieve is to get an exe built for my .NET Core 2.2 Console App for a few selected environments eg. osx and windows-10, possibly linux too, each in its own folder. Obvious condition is that it has to be integrated with the build, so no extra manuals steps (commands) are required. This has to work from within VS2019 Pro as well in CI (like AzureDevOps).
Is this basic step achievable or .Net core was a major step-back in the progress of software development?
I hope I just miss something and I am just grossly exaggerating. :)
Thanks, Radek
How do I integrate exe publishing with the VS2019 build of the console
.net core 2.2 application?
Actually, I think you do not need to worry about this.
dotnet publish already contains the build process. Publish process will first execute Build and then run publish. In a word, Build is a part of Publish process.
So when you input dotnet publish under Build, you will get an infinite loop of building retries.
Solution
----- Just delete post-build event in xxx.csproj file and just dotnet publish directly and it will run the build process first.
You can test in the local VS and when you right-click on your project-->Publish, it will show the step in the output windwow.
In addition, as far as I know, Azure DevOps has a task called dotnet publish which contains Build.
And if you want to do some msbuild custom target only for publish step, you can add a condition like Condition="'$(DeployOnBuild)'=='true'", it will execute for Publish process rather than the normal build step(right-click on your project-->Build).
<Target Name="testone" Condition="'$(DeployOnBuild)'=='true'" AfterTargets="Build">
xxxxxxxxxxxxxx
</Target>
---------------Update 1----------------
First question
Actually, the build of the publish is the pure process and then publish will copy the content of the build output into publish folder. So the execute program is just from the build output folder.
See the publish log:
So you should not specify a publish target under the build process. This is superfluous.
Second question
To generate this program for Window-10, linux or osx, you can try these command line to publish your project:(Release is the standard release build configuration)
For Win-10:
dotnet publish -r win10-x64 -c Release --self-contained
Linux:
dotnet publish -r linux-x64 -c Release --self-contained
For osx:
dotnet publish -r osx.10.12-x64 -c Release --self-contained
In this way, the project is first built according to the specified runtime and then published.
More info about .NET Core RID Catalog, you can refer to this document.
Update 2
I think you should change the configuration in this package UI:
Then click Save.
Also, when you publish this web project, please try to delete bin and obj folder and then publish it.
Debug: bin\Debug\netcoreapp2.1\publish
Release: bin\Release\netcoreapp2.1\publish
Or you should use dotnet command as I described to publish the project. The path is under Debug or Release folder.
What I want to achieve is to get an exe built for my .NET Core 2.2
Console App for a few selected environments eg. osx and windows-10,
possibly linux too, each in its own folder. Obvious condition is that
it has to be integrated with the build, so no extra manuals steps
(commands) are required. This has to work from within VS2019 Pro as
well in CI (like AzureDevOps).
Is this basic step achievable or .Net core was a major step-back in
the progress of software development?
It's a good idea but as I know what you want is not 100% supported for now.
It seems that your expected scenario is:
Click the Build(F5) button=>Project will be built in different platforms win-x64,win-x86,linux-x64...,also will be published in different platforms automatically with self-contained mode.
But the fact is:
1.Click the Build button(Build(F5) equals to the Build button in project context) will run the Build Target (A default built-in target for each project for build). => dotnet build in command-line.
2.Click the Publish button will run the Publish Target (A default built-in target for each project for publish). => dotnet publish in command-line.
3.If you have any build/publish command in post-build event, it will result in an expected loop. So it's hard to combine publish with build perfectly since they're two actions in VS with different button/behavior/corresponding command. (Only dotnet publish command can recognize --self-contained)
4.To build/publish the projects in parallel, the batch file or msbuild targets file is a good choice.
#1.Build different platforms using one build command see this. #2.Publish different platforms using one command see this. (They both use custom .targets to do the job)
Suggestions:
According to your scenario, I think you can consider using #2. It's not necessary for you to build with different platforms during your normal development.
1.In local VS when you're developing and debugging:
The default build button/option is enough for you to debug.
2.In local VS when you want to publish the project in different platforms:
Use #2 above so that you can publish them using cmd.exe or PS.exe. (Via command dotnet msbuild -restore -t:PublishAllRids)
3.When you're automating the CI pipeline in AzDeops(Also use #2):
A CMD/PS task with dotnet msbuild -restore -t:PublishAllRids command is enough. Dotnet publish will build automatically if we don't pass --no-build argument.
4.Azure Devops Service suggests separate the jobs in different tasks, it's not recommend to Integrate Publish with Build heavily, especially for .net core projects. (Here's official sample about CI for .net core projects.)

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

Precompiled in asp.net application not required

I have a website project and some other projects that are tied up via a solution file. When I build the solution using visual studio, it does not create precompiled version of website but using msbuild to build the solution creates precompiled version.
We don't use precompiled code to deploy on servers so this version is not required in our process.
And this takes a lot of time to get created.
So how can I avoid creation of pre compiled version? Is there any switch or task that I can use in msbuild scripts to build the solution.
I need msbuild to simply build the solution.
currently the command that I am using is:
msbuild "ABC.sln"
MSBuild on the command line features a /t switch, which allows you to define build targets. You would use this switch to target all the other subfolders in the solution, leaving the website untouched.
MSBuild Command Line arguments on MSDN

Resources