Verify variable replacement for Windows Service?

I’m trying to verify that JSON variable replacement in my appsettings.json file was done correctly, as I am new to both Octopus Deploy and .NET Core development.

I see in the Task Log that Octopus failed to start my application as a Windows Service.

21:54:40 Info | Starting the HydratorService service
21:54:40 Info | Attempt 1 of 3 failed: Service ‘HydratorService (HydratorService)’ cannot be started due to the following error: Cannot start service HydratorService on computer ‘.’.
21:54:40 Info | Waiting for 1 seconds before retrying…
21:54:42 Info | Retrying…
21:54:42 Info | Attempt 2 of 3 failed: Service ‘HydratorService (HydratorService)’ cannot be started due to the following error: Cannot start service HydratorService on computer ‘.’.
21:54:42 Info | Waiting for 1 seconds before retrying…
21:54:43 Info | Retrying…
21:54:43 Error | Start-Service : Service ‘HydratorService (HydratorService)’ cannot be started due to the following error: Cannot start
21:54:43 Error | service HydratorService on computer ‘.’.
21:54:43 Error | At D:\APPS\DEV\DevPortalCache\2021.5.25.175222\Octopus.Features.WindowsService_BeforePostDeploy.ps1:200 char:3
21:54:43 Error | + Start-Service $psServiceName
21:54:43 Error | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21:54:43 Error | + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
21:54:43 Error | ServiceCommandException
21:54:43 Error | + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
21:54:43 Verbose | Deleting ‘D:\APPS\DEV\DevPortalCache\2021.5.25.175222\Octopus.Features.WindowsService_BeforePostDeploy.ps1’

In my Worker Service code I have Serilog LogError calls for failure to start the service.

What I am trying to do is make sure the service start is not failing because of my incorrect usage of Octopus Deploy’s JSON variable replacement feature. I presume I have to get those Serilog statements to appear in the task log? What do I need to do to make this happen? I am also open to other means of verifying that I used JSON variable replacement correctly.

Attached is the raw task log.ServerTasks-81215.log.txt (17.8 KB)

Hi Paolo,

Thanks for getting in touch, and welcome to Octopus!

It does seem possible the service is failing to start due to an incorrect value in your appsettings.json, which could be due to the substitution Octopus is performing on it. The first thing I’d like to check is how these variables, those meant to apply to the JSON file, are defined in Octopus to rule out any possible syntax issue. Using this doc section as an example, a variable with name app:port with value 4444 will target {"app":{"port":80}} in the appsettings file and output {"app":{"port":4444}}.

What I’d suggest doing from here is turning on verbose logging temporarily, which is done by creating two variables in your project: OctopusPrintVariables and OctopusPrintVariables, both with a value of True. This will output a lot more information in the resulting task log, including every variable before and after evaluation on each step, and that should help point us in the right direction.

I look forward to hearing back!

Best regards,

Kenny

Thank you! I added the two suggested project variables. I can see the Octopus read the WorkerInterval and Serilog file output path in the verbose log. However I still cannot see if these values were successfully uses in variable replacement on the appsettings.json.

Is there a way to see what appsettings.json looks like after the variable replacement?

Attached is the raw task log with verbose output.ServerTasks-81331.log.txt (46.0 KB)

I logged onto the machine that Octopus tried to run the deployment on.

I opened the appsettings.json file and it does not look like the variables were changed in the json file. Is this the expected behavior?

Or does Octopus make a copy of the original appsettings.json file, and work off of that copy?

Hi @Paolo.Valladolid,

Sorry for taking so long to get back to you, I’m going to step in for Kenneth here.

We see that in your log we see: Performing JSON variable replacement on 'D:\APPS\DEV\DevPortalCache\2021.5.25.175222_1\appsettings.json'

This indicates that replacement is happening but maybe the variables might be mismatched between the JSON and the Project Variables in Octopus. Is it possible to provide us a screenshot or example of Project > Variables you’ve defined within Octopus versus some that might be in your appsettings.json file?

If the appsettings.json file where variables weren’t changed is located at D:\APPS\DEV\DevPortalCache\ under the release number, then it’s likely variable replacement didn’t find any variables to replace.

Looking forward to hearing back from you.

Regards,
Garrett

Hi Garrett,

Here are the task log file from the last attempted deployment, and the screenshot of the project variables.

The variable I’m trying to substitute is “WorkerInterval”. I had another variable, but I took it out because it had a more complicated JSON path. I thought it would be best to simplify the problem and solve that.

ServerTasks-82730.log.txt (45.8 KB)

Hi @Paolo.Valladolid,

Thanks for the additional information and the logs.

I can see Octopus trying to do a Variable Replacement but I’m not seeing the typical ‘successful’ lines. I’m wondering if I can get a copy of your appsettings.json to compare against your Octopus Project variables? I’ve created a secure Upload link for you here if you prefer, or you can Message me directly.

Looking forward to hearing back from you.

Regards,
Garrett

Thanks, Garrett! I uploaded the file using the secure link.

Hi @Paolo.Valladolid,

Thank you for the JSON file. I am going to attempt to reproduce your exact situation in a test environment. Just to confirm, the service starts correctly if you do not use variable replacement?

The final appsettings.json file should be located, according to your logs, at: D:\APPS\DEV\DevPortalCache\2021.5.25.175222_3\ could you check to see if WorkerInterval was ever replaced with your Octopus Variable?

Looking forward to hearing back and I will update you with anything I find on my end.

Regards,
Garrett

Hi Garrett,

I just verified that the WorkerInterval variable was replaced as expected in the appsettings.json file. I think I had trouble verifying the replacement because I used the same value in both Octopus and the original JSON. Sorry about that!

Next I’ll try putting the more complicated variable back in my Octopus Variables config. The variable name is:
Serilog:WriteTo:Args:path

Hi @Paolo.Valladolid,

I’m glad to see you could verify that the Variable replacement was happening.

Please let us know if you have any issues with the more complex variables or have any more questions.

Regards,
Garrett

1 Like

Thanks!

I’m struggling with the WriteTo node, in the Serilog section of my appsetting.json, having two children whose names are both “name”. Let me know if we can talk about it in this thread or if I should start a new one.

Hi Paolo,

Thanks for keeping in touch! Let me first offer my sincere apologies for allow your thread to be dropped. I should have kept a closer eye on this one.

I appreciate all the detailed info you’ve provided. So it looks like in your appsettings.json file, the WriteTo is an array. You can certainly substitute values into a JSON array, but to do so would be a bit different where you’d need to specify the zero-based index to target. Using your JSON, this is how my variable looks to target the 2nd “Name” in the WriteTo array.

After deployment, here’s the resulting appsettings.json file contents.

This variable value format is required so that it can be successfully parsed as JSON. Let me know if this helps, or if we can try to help with anything more going forward!

Best regards,

Kenny

1 Like