I recently upgraded an application to dotnet core 3 and started using the PublishSingleFile flag during the build process. With these two changes the way the executable path is found has changed. Now instead of getting the path where the executable file is, I get redirected to a random directory in /var/tmp/.net/
where as I used to get /opt/appdir/
.
Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
Edit: So it seems like the random dir it is spitting out actually points to an unpacked version of my application which does not contain vital resources that are in the other directory.
The Application Base (Root) path is available in the ContentRootPath property of the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .
Add <PublishSingleFile>true</PublishSingleFile> to your project file. On the Solution Explorer pane, right-click on the project you want to publish. Select Publish. If you don't already have a publishing profile, follow the instructions to create one and choose the Folder target-type.
The following seems to give the path you're after, to the original executable:
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
You were getting the actual executable path. You just didn't understand what was going on.
Using the PublishSingleFile is not generating a single file that when executes does exactly what the software is programmed to do. What is actually happening is that a publish with this flag generates a file that contains a packaged version of all your files, which when executed unpacks them into the a subdirectory of /var/tmp/.net
and then executes it. So if you are moving configs into the publish dir after the publish process, they will not automatically make it to the executable location when you deploy and try to run it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With