Nant vs. msbuild: stopping a service

I am trying to decide which side I am participating in the MsBuild war against Nant. I start with: stop the service, deploy some files, restart the service. Just by looking at these two links, it's much easier to do in Nantes.

MSBuild: Service Usage Example Is there an MSBuild task in Microsoft.Sdc.Tasks?

<target name="service_exists"> <script language="C#"> <references> <include name="System.ServiceProcess.dll" /> </references> <code><![CDATA[ public static void ScriptMain(Project project) { String serviceName = project.Properties["service.name"]; project.Properties["service.exists"] = "false"; project.Properties["service.running"] = "false"; System.ServiceProcess.ServiceController[] scServices; scServices = System.ServiceProcess.ServiceController.GetServices(); foreach (System.ServiceProcess.ServiceController scTemp in scServices) { etc... 

Nantes: http://ryepup.unwashedmeme.com/blog/2007/01/04/restart-a-windows-service-remotely/

 <!-- Send the stop request --> <exec program="sc.exe"> <arg line="\\server stop shibd_Default"/> </exec> <!-- Sleep a little bit, to give the service a chance to stop --> <sleep seconds="5"/> <!-- Send the start request --> <exec program="sc.exe"> <arg line="\\server start shibd_Default"/> </exec> 

I wonder if the SO community agrees with me. Is it easier to get basic things like this in Nantes? Of course it looks like this. C # code in a CDATA block? WTF?

Our current build process: a) many bat files, b) many curses. I would love to find a good replacement, but the MsBuild material looks like a world of pain in front of my eyes. I think you need to build scripts in Nant, and then use MsBuild to execute any .NET assemblies that need to be executed.

One important question : which one is better to catch errors in the script before running the script? I thought about rolling on my own, and it was very important: compile all your data and make sure that it makes sense before trying to run.

+4
source share
4 answers

In msbuild, you can also use the ServiceController task, which is in the msbuild community tasks .

+8
source

You can execute sc.exe using MSBuild every bit is just as easy ...

 <Exec Command="sc.exe \\server stop shibd_Default" /> 

By default, it will fail if the exit code ( sc.exe ) is not zero, but can be configured.

+7
source

With Nant, there are 2 more ways to stop the service, and one can track the error.

First (using Net Stop):

 <exec program="net" failonerror="false"><arg value="stop"/><arg value="${serviceName}"/></exec> 

Second (much cleaner):

 <servicecontroller action="Stop" service="${serviceName}" if="${service::is-installed(serviceName,'.') and service::is-running(serviceName,'.')}" /> 

Please note that the second line checks that the service already exists and is running, which allows you to track any strange error.

+4
source

In addition to @nulpptr's answer for MSBuild, if you don't have the ability to use community tasks, you may need to hack into waiting for your service to stop before moving on. If you have a resource bundle, you can use the EXEC task with the sleep command.

No resource set? Use the ping trick ...

However, if you do not have a set of resources, you can use the ping trick to delay the delay. For example, the following will stop your service using the sc command, and then stop for about 5 seconds:

 <Exec Command="sc.exe \\server stop shibd_Default" ContinueOnError="true" /> <Exec Command="ping 127.0.0.1 -n 5 > nul" ContinueOnError="true" /> 
+1
source

Source: https://habr.com/ru/post/1300257/


All Articles