So I have built my .NET 8 Windows Service, and used the "Publish" option before deploying the
Service to my local - using the Single File
"self-contained" option. I then copied that folder to one of our servers for deployment.
In PowerShell as follows:
sc.exe create "Mqtt Service" binpath="C:\dev\projects\..\NotificationServiceCore\bin\Release\net8.0-windows\publish\MyServiceCore.exe"
But in my code, I need to get the path to that deployed service - NOT the Windows temp folder.
This for example does NOT give it to me - System.AppContext.BaseDirectory;
- it returns c:\windows\temp\.net\..
But I have caught the exception when trying to load my certificate file:
2024-06-14 12:59:35 (null) [5] ERROR Certificate Exception in WsSecureClientOptions(). Check app config settings. The system cannot find the file specified.
2024-06-14 12:59:35 (null) [5] DEBUG Path to Certificate File - C:\Windows\TEMP\.net\NotificationServiceCore\pxsSvFJpErHm9e18vOpihtHD3i7O1g4=\certificate2024.pfx
Why the file exception? It's because we do NOT include that certificate.pjx in our project build. We have a post-build
event which copies that file to $(OutDir)
cd $(ProjectDir)
xcopy "..\certificate\*.*" "$(OutDir)" /F /Y
The issue here is that the pfx certificate file is not in that `c:\Windows\Temp.net..." folder.
And this is how I resolve to the Base Directory in my code:
string appBaseDir = System.AppContext.BaseDirectory;
Bottom line: Do NOT choose the "self-contained" and "Produce single file" options.
Instead, choose "Framework-dependent" and "win-x64" (or whatever's appropriate for your envir).
FYI: Now System.AppContext.BaseDirectory
will indeed return the directory in which you have installed your Win service (i.e. and not the temp folder that I had previously seen with the self-contained option).
Your build files (.dll, .exe, .dll.config) will be deployed to ..\publish\win-x64
folder.