Set configuration to "Release" when publishing - .net-core

I have a project that I'm publishing using the dotnet CLI's dotnet publish command.
Since I only ever publish with a release intent, I'd like to make it so that when I issue a dotnet publish the build and publishing happen with the "Release" configuration but still use the "Debug" inside Visual Studio when I'm debugging/testing.
I know I can manually change the configuration from Visual Studio's configuration/properties window and can specify the -c Release argument but is there some tag I can include in my .csproj file that says "when publishing use 'Release' by default"?

Create batch/powershell file for the command
dotnet publish -c Release
save it into release.cmd and run that when needed

There is an opened issue regarding this here.
You may check it, specially this #nguerrera comment.

Related

Nothing to do. None of the projects specified contain packages to restore dotnet restore

I am working on an ASP.NET Web API 2 project with .NET target framework 4.6.1. I am trying to setup github workflow for my repo. When the dotnet restore command is run, it throws an error like below.
I am getting the same error if I run the same command in from command prompt inside my project. Also if I run dotnet build, it shows below error.
The project builds fine from Visual Studio but not working from command line or github workflow yml. Can anyone please point me on what am I missing?
The project builds fine from Visual Studio but not working from command line
Check which sln file Visual Studio is using to build your project.
Since I don't see any sln/csproj in your GitHub repository, it is also possible that you have a .gitignore which would prevent adding those in the first place.
DOTNET Restore does not support pacakges.config https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-restore
So you have to move the nuget package references to csproj file itself
Here is a great comment on how to do that https://stackoverflow.com/a/65701746/8318698
Note: check that if multiple projectGuid is there on csproj at the end of the steps
After that you will be able to use dotnet restore without a hitch.

Publish project from cli

I'm wondering what is the difference between publishing project from cli and from Visual Studio.
I just experienced sometimes publishing from cli is not getting latest changes. I mean when we have a change or new method, after publishing the project it is not visible. But if I publish from VisualStudio everything is ok.
I'm using this command for cli. I have different apis to publish individuality.
dotnet publish -c Release -o "D:\Deploy\Test\test.api\" "D:\Development\Test\test.api\test.api.csproj"
Plus before that I clean and rebuild the project again from cli.
dotnet clean 'D:\Development\Test\test.api\test.api.sln' --force
dotnet build 'D:\Development\Test\test.api\test.api.sln' --force
Make sure to use the same configuration (-c Release) for all the dotnet commands you run.
However, dotnet publish command should normally be enough to get the latest changes.
Is there any chance you're running the commands without actually saving the files after editing them (VS probably does that automatically when publishing)? Have you tried editing the files with other text editors/IDEs?

What dotnet core cli command(or msbuild commands) does Visual Studio use when Building, Rebuilding and Cleaning the Solution?

I want to know the exact dotnet cli commands that Visual Studio uses when I Build/Rebuild and Clean solution in my dotnet core application?
I know that the dotnet core cli was build on top of msbuild so when you run Build/Rebuild or Clean Solution Visual Studio uses
msbuild commands directly and not the ones from dotnet core cli?
Is that correct?
If this is correct I would like to know which msbuild command or commands it uses with the three actions:
Build Solution
Rebuild Solution
Clean Solution
And which dotnet core cli commands would be equivalent to that?
I know from this post(Relationship between the dotnet cli and the new vs2017 msbuild)
that the following commands do the build, rebuild and clean in dotnet and msbuild.
Dotnet cli:
Build: dotnet build
Rebuild: dotnet build --no-incremental
Clean: dotnet clean
Msbuild:
Build: msbuild /t:build
Rebuild: msbuild /t:rebuild
Clean: msbuild /t:clean
I guess this is not all? This is fine but I would like to see what Visual Studio produces for the actions?
And I am wondering if Visual Studio behavior can be changed so it runs dotnet cli commands instead of msbuid?
Research:
I was building a asp.net core web api project in Visual Studio(Visual Studio 2017 Enterprise Version 15.9.11)
I was looking in Visual Studio Output when I Build/Rebuild and Clean the solution but I could not find anything related to
dotnet core cli or msbuild. Then I went to VisualStudio Tools/Option/"Project and Solution"/"Build and Run" and changed the options:
MSBuild project build output verbosity: tried both "Detailed" and "Diagnostics" options
MSBuild project build log file verbosity: tried both "Detailed" and "Diagnostics" options
The outcome was that the log that was produced in the Output window of Visual Studio was huge and it was difficult to find
the exact command which would be used for the actions. I can see msbuild used in many places in the output but it is a little confusing
to find the exact command.
I also saw this question (Does Visual Studio use MSBuild internally, and what is the exact command?)
This answer says that:
Quote:
"It appears that the MSBuild command line options are not specified,
but rather the MSBuild APIs are called within Visual Studio. Unless
you have the Visual Studio source code to reverse engineer, you cannot
get an equivalent command line."
Is that the same case for dotnet core cli msbuild as well?
Any help or clarification on this is appreciated.
I know that the dotnet core cli was build on top of msbuild so when
you run Build/Rebuild or Clean Solution Visual Studio uses msbuild
commands directly and not the ones from dotnet core cli?
For VS2017, I would think the VS IDE calls msbuild.exe directly when Clean, Build and Rebuild.You can easily check this point by Task Manager or Process Monitor.
As for what you mentioned above:It appears that the MSBuild command line options are not specified, but rather the MSBuild APIs are called within Visual Studio.
I think it's right but only for the eariler vs versions(2010,2013). I've tested with VS2010, when doing building-related actions in VS, it doesn't call MSBuild.exe. So the msbuild in VS2010 is not executed as a separate process.
But for VS2017, when I create projects which target .net core, when doing building-related actions(click the build, clean, rebuild button), it obviously calls the msbuild.exe like below:
About what msbuild commands VS actually executes:
Since now the VS2017 calls msbuild.exe to build .net core or .net fx projects.
In my opinion:
For the solution which only contains a project:
Build the Solution=> msbuild xxx.sln /t:build /p:Configuration=xxx;Platform=xxx
Rebuild the Solution=>msbuild xxx.sln /t:rebuild /p:Configuration=xxx;Platform=xxx=>msbuild xxx.sln /t:clean;build /p:Configuration=xxx;Platform=xxx
Clean the Solution=>msbuild xxx.sln /t:clean /p:Configuration=xxx;Platform=xxx=>msbuild xxx.sln /t:clean
I think every time when we click Build button in VS, it will pick the value of Configuration and Platform from this box, because these two parameters are sure to be passed to MSBuild.exe.
Also, one thing we can discover is that IDE has a check process before start build: It will check if the file is out-of-date and then determine if it need to build or not. But this is not what you ask in your issue and it not affects the command you want, so I skip it.
Also, see this page we can find there are some msbuild-related settings here:
So actually I think the command above should add some parameters like:msbuild ... -m:8 -v:M.
In addition: Though I find building-related action in VS will call msbuild.exe directly. I'm not certainly sure that my command above is 100% correct. I'm afraid no one can ensure that except the guys who develop the menu command in VS IDE. So if i misunderstand anything please feel free to correct me:)
And if you just want to get the exactly same thing like what in VS, you can also have a try devenv.exe. This is the only place in official document which confirms the build switch performs the same function as the Build Solution menu command within the integrated development environment (IDE).

Compile a .NET Core application as an EXE file using Visual Studio 2017

I created a .NET Core application (v1.1) in Visual Studio 2017. When I compile it, I get a DLL file produced instead of the expected EXE file for the built project. I did check the csproj file and confirmed the output type is set to exe, but no dice.
Why is Visual Studio 2017 is still producing a DLL file?
I'm sure it's a quick setting somewhere that I forgot...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Core.EF.SqlServer\Core.EF.SqlServer.csproj" />
</ItemGroup>
</Project>
Update 2019:
.NET Core 3.0+ projects will now include an executable for the platform you build on by default. This is just a shim executable and your main logic is still inside a .dll file.
But .NET Core 3.0 also introduced single-file deployments so deploying with
dotnet publish -r win-x64 -p:PublishSingleFile=True --self-contained false
will create a single .exe file containing all your dependencies. You can change --self-contained to true to also include the .NET Core Runtime as well so .NET Core does not need to be installed globally on the target machine.
Original
.NET Core applications are supposed to be .dllfiles. OutputType set to Exe in this case means "executable" and does everything necessary to ensure that the output is runnable (entry point from Main() method, .runtimeconfig.json file). The resulting DLL file is meant to be run using:
dotnet yourapp.dll
This DLL file works across all platforms that are supported by the .NET Core runtime (Windows, Linux, and macOS). This is called a "portable" or "framework dependent" deployment.
If you want really a .exe file, consider self-contained deployments. This will create an output that contains its own copy of the .NET Core runtime and an yourapp.exe file - but it also increases the size of the published application and it needs to be updated when new versions of the runtime are released.
Also, the resulting application only works on the operating system published for.
Refer to .NET Core application deployment for more details on the deployment options and how to set them up.
In Visual Studio 2017:
Right click on your project and select Publish (In Visual Studio 2019, click on menu Build → Publish <projectName>)
Select 'Folder' and create a new profile
In tab 'Publish', click 'Configure...'
Select Deployment Mode: Self-contained, Target Runtime: win-x86 (or win-x64)
Save
Publish
In the folder <Your project>\bin\Debug\netcoreapp2.1\win-x86\ you will see the EXE file:
Starting with .NET Core 2.2 you can build framework-dependent executables
Although building a self-contained deployment can be a good solution, it has its own drawbacks. (See R.Titov and Martin Ullrichs' answers on SCD-s.)
Fortunately, .NET Core 2.2 supports the building of so called framework-dependent executable-s, that are essentially a wrapper binary (.exe on Windows) around the standard dll-s.
This way you have all the advantages (and disadvantages) of the standard framework-dependent deployment (again, see Martin's answer), but you have a convenient way to launch it, without having to call it through the dotnet CLI.
You can publish your app as a Framework-Dependent Executable using the following syntax:
dotnet publish -c Release -r <RID> --self-contained false
Where RID is the usual runtime identifier, e.g. win-x64 or whatever platform you wish to build for (see the catalog here).
That's how you do a self-contained publish with command-line in any OS:
dotnet publish C:\src\App\App.csproj -c release -r win-x64 -o output-win-x64
Besides, you might want to get the output decreased from typical ~60 MB for a simple Hello World app to ~30 MB by using ILLink.
Also, you might want to go further and get a single .exe file of a size at around 5 MB and use ILCompiler. See this reply.
The other answers are good, but what I find sometimes convenient is:
Not have it self-contained because the target machine is likely to have .NET Core of the correct version installed. This cuts on number of the DLL files I need to ship.
Not have to specify dotnet on the command line
For this, a bat file wrapper can be used, similar to these lines:
#ECHO OFF
REM see http://joshua.poehls.me/powershell-batch-file-wrapper/
SET SCRIPTNAME=%~d0%~p0%~n0.dll
SET ARGS=%*
dotnet "%SCRIPTNAME%" %ARGS%
EXIT /B %ERRORLEVEL%
If your application ends up in yourapp.dll, name the bat file yourapp.bat and place it along side the DLL file. Now instead of dotnet yourapp.dll params you can call yourapp params.
Note that the context of this answer is in-house tooling, so all the developers using the utility will have a pretty standard development machine setup. If this is to be distributed to an external customer who is running who knows what on their boxes, the self-contained option is far superior.

Publishing a dotnet application that's already running

I'm attempting to create a script to simplify the process of publishing a .NET Core website. I'm running into an issue when I run dotnet publish against an already running server. The server is IIS with the dotnet bundle installed, so IIS uses its app pool to start dotnet.
Here's my batch file. I'm happy to use another script type:
cd src/app
dotnet build --no-incremental
dotnet publish --framework netcoreapp1.0 --configuration Release --output ../../dist
When I run the script I get this error:
"The process cannot access the file 'C:\inetpub\wwwroot\app\dist\app.dll' because it is being used by another process."
This makes sense, it appears I need to stop, deploy, and restart dotnet. Can I do this from the script? Or is my approach to this problem wrong?
The best way is to drop an app_offline.htm file to your application folder. This will make IIS stop your application and serve the contents of the app_offline.htm file to the user while you are copying the new version. Once you complete copying the new version of your application remove the app_offline.htm file and IIS will start your application.
You can find more details on running ASP.NET Core applications with IIS in my post.
Based on Pawel's answer, I have a deploy folder containing my app_offline.html file and multiple deploy scripts to IIS. Here's a sample script I use to deploy:
copy .\app_offline.htm C:\hosting\my-project\app_offline.htm
dotnet publish ../MyProject.csproj -r win-x64 -f netcoreapp2.1 --self-contained -c Release -o C:\hosting\my-project
del C:\hosting\my-project\app_offline.htm
I think this is a valid solution, but doesn't help when I want to script the build process.
Stop-Website "xxx"
Stop-WebAppPool "xxx"
Start-Sleep -Seconds 5
dotnet publish --output d:\publocation
Stop-WebAppPool "xxx"
Start-Website "xxx"
if you've created a published profile in Visual Studio and you're using IIS, then you can use that profile instead of writing directly to the destination directory:
dotnet publish /p:PublishProfile=Properties\PublishProfiles\IISProfile.pubxml

Categories

Resources