stopping the MongoDB service

115 views
Skip to first unread message

Andrew Heard

unread,
Jul 1, 2012, 8:10:12 PM7/1/12
to mongodb...@googlegroups.com
My C# WinForms program has code to install and/or start the MongoDB service before using the C# driver - this works fine. I would now like to be able to stop the MongoDB service when the program exits - the service is only used by this program, and users have noticed operating system messages about having to stop the service when uninstalling or upgrading the C# program which they shouldn't have to encounter. I know from the CSharp+Driver+Quickstart there is mention that "You do NOT need to call Connect or Disconnect" I agree which is generally true.

I have (roughly):
  var sc = new ServiceController("MongoDB");
  :
  server.Disconnect(); // may not do anything? or may have to wait?
  sc.Stop(); // ==> System.InvalidOperationException' occurred in System.ServiceProcess.dll

thanks in advance for any suggestions

Andrew Heard

unread,
Jul 1, 2012, 9:56:34 PM7/1/12
to mongodb...@googlegroups.com
This may *just* be a Windows Vista/7 permissions issue rather than a mongo c# driver usage issue. The inner exception indicates "access is denied", the outer exception is misleading. I now have

var scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, serviceName);
scp.Demand(); // no exception here
var sc = new ServiceController("mongoDB");
Debug.Assert(sc.CanStop); // no assert here
sc.Stop(); // ==> System.InvalidOperationException' occurred in System.ServiceProcess.dll, InnerException.Message.Equals("access is denied")

craiggwilson

unread,
Jul 2, 2012, 8:39:14 AM7/2/12
to mongodb...@googlegroups.com
If your application is the only one using this, then I would suggest NOT running it as a service.  Instead, just start the binary using a new Process() and then stop the process before closing down the application.  That will take a whole level of indirection out of your app.

craiggwilson

unread,
Jul 2, 2012, 8:39:48 AM7/2/12
to mongodb...@googlegroups.com
If your application is the only one using this, then I would suggest NOT running it as a service.  Instead, just start the binary using a new Process() and then stop the process before closing down the application.  That will take a whole level of indirection out of your app.

On Sunday, July 1, 2012 8:56:34 PM UTC-5, Andrew Heard wrote:

Andrew Heard

unread,
Jul 3, 2012, 8:11:01 PM7/3/12
to mongodb...@googlegroups.com
Thanks for the excellent suggestion Craig.

Ken Egozi

unread,
Jul 4, 2012, 1:42:08 AM7/4/12
to mongodb...@googlegroups.com

mongodb service is unstoppable. Even Chuck Norris cannot stop mongodb

Andrew Heard

unread,
Jul 4, 2012, 1:49:01 AM7/4/12
to mongodb...@googlegroups.com
LOL - I should have realized! thanks Ken for the words of Wizdom
Reply all
Reply to author
Forward
0 new messages