Question

I build a windows service in c# .net. I added Pre-Build and Post-Build event to automatically deploy my service on the build. But sometime I got this error :

Unable to copy file "[CompletPath...]\bin\Debug\Business.Data.dll" to "bin\Debug\Business.Data.dll". The process cannot access the file 'bin\Debug\Business.Data.dll' because it is being used by another process.

In the Pre-Build event i'm closing the service, killing all task that using file in the Debug directory and uninstalling the service. There is the code in the .bat that i'm running in the Pre-Build event :

SET executionPath=%~dp0
SET serviceName=%1
SET frameworkPath=%2
SET targetServicePath=%3
SET targetBinPath=%~4
set targetBinPath=%targetBinPath:~0,-2%

net stop %serviceName%
powershell -NonInteractive -executionpolicy Unrestricted -file "%executionPath%\unlockfiles.ps1" "%targetBinPath%"
%frameworkPath%\installutil.exe /u %targetServicePath%

Exit /b 0

On the post-build event i'm installing and starting the service, there is the code even if this is not the problem because i'm gettring the error on the build, so the post-build event is not executing.

SET serviceName=%1
SET frameworkPath=%2
SET targetServicePath=%3

%frameworkPath%\installutil.exe /ShowCallStack %targetServicePath%
net start %serviceName%

I'm not always having the problem. I usually have the problem the first time i'm building, i'm cleaning the solution, build again and usually it's working after this.

Was it helpful?

Solution 2

If I were in your situation I would start to employ and more team oriented solution.

I would spend some time setting up a Continuous Integration system using something like Team City (this tool is free for upto 20 build configurations. I REALLY rate everything from JetBrains.

Then each time one of your team checks in new source code for the service it will be detected automatically by the build system (Team City) and a new set of builds will be triggered.

You could have a debug build that runs your unit tests against the code, a release build that also then includes a WiX Project that also then builds an installer for the service.

Once those processes are completed you can configure it to pop the installer in a known location on your network and it can also then email all the developers in your team to notify them that a new version of the service is available for installation.

WiX is a VERY mature installation authoring tool that is used by Microsoft for a lot of their products. There is a lot of support out there for the tool set and will make your entire process much tighter, repeatable and predictable.

It will take some time to get all of this completed but it really is worth the effort.

OTHER TIPS

I would separate these processes if I were you. You don't need to uninstall the service in order to update the files.

I'm not a great fan of pre/post build events for much more than moving some files around after the build has completed.

I use xcopy /y /c "$(TargetPath)" "location to copy to"

If memory serves I didn't even actually have to stop the service in order to update the dlls, however you may need to just stop the service in the post build before doing the xcopy commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top