Service Fails on Start

J

Jasonkimberson

Currently I am having a problem creating a windows service that
monitors a mapped drive for events (using the FileSystemEventHandler).
The service complies but fails to start:

"The XMLWatcher service on Local Computer started and then stopped.
Some services stop automatically if they have no work to do, for
example, the Preformance Logs and Alerts service."

The Service has no problem starting if i point the folder to be
monitored to my local C drive, and it had no problem monitoring the
mapped network drive as a console app or windows app.

Any Help would be greatly appreciated.

Installer:

private void InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
System.ServiceProcess.ServiceControllerPermissionAccess
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.User;
this.serviceProcessInstaller1.Password = "XXXXXXXX";
this.serviceProcessInstaller1.Username = "MyUserName";
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "XMLWatcher";
//
// ProjectInstaller
//
this.Installers.AddRange(new
System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});

}


Main Intializer


private void IntializeFileSystemWatcher()
{
//Create File System Watcher for XML files
fsWatcher=new System.IO.FileSystemWatcher("Y:\\","*.xml");
// Add event handlers for new XML files and change of existing XML
files.
fsWatcher.Changed += new FileSystemEventHandler(OnXMLFileChanged);
fsWatcher.Created += new FileSystemEventHandler(OnXMLFileCreated);

// Begin watching.
fsWatcher.EnableRaisingEvents = true;
fsWatcher1.EnableRaisingEvents = true;
}
 
B

Brian Pelton

My experience has been that services that start and stop are throwing
exceptions. Check the event log to see if there are any events with errors from
your service.

Also, it is usually easier / better to write the service in a library (dll) and
then test that using console or other applications, and then just make calls
into the dll from the service. This doesn't eliminate the chance of errors in
the service, but it reduces them and it makes development much easier.

--Brian
 
B

Brian Pelton

Rereading the post I'm thinking it is a security / rights issue. If it works
against C:\ but not against a network drive, that would point to security.

I haven't used it myself but you might look into impersonating another user when
your service runs. I see that you're trying to set a username and password but
I'm not sure that that is what the service is running under.

Try looking into the System.Security.Principal.WindowsIdentity class'
Impersonate method.

--Brian


Brian said:
My experience has been that services that start and stop are throwing
exceptions. Check the event log to see if there are any events with
errors from your service.

Also, it is usually easier / better to write the service in a library
(dll) and then test that using console or other applications, and then
just make calls into the dll from the service. This doesn't eliminate
the chance of errors in the service, but it reduces them and it makes
development much easier.

--Brian
Currently I am having a problem creating a windows service that
monitors a mapped drive for events (using the FileSystemEventHandler).
The service complies but fails to start:

"The XMLWatcher service on Local Computer started and then stopped.
Some services stop automatically if they have no work to do, for
example, the Preformance Logs and Alerts service."

The Service has no problem starting if i point the folder to be
monitored to my local C drive, and it had no problem monitoring the
mapped network drive as a console app or windows app.

Any Help would be greatly appreciated.

Installer:

private void InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
System.ServiceProcess.ServiceControllerPermissionAccess
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.User;
this.serviceProcessInstaller1.Password = "XXXXXXXX";
this.serviceProcessInstaller1.Username = "MyUserName";
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "XMLWatcher";
//
// ProjectInstaller
//
this.Installers.AddRange(new
System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});

}


Main Intializer


private void IntializeFileSystemWatcher()
{
//Create File System Watcher for XML files
fsWatcher=new System.IO.FileSystemWatcher("Y:\\","*.xml");
// Add event handlers for new XML files and change of
existing XML
files.
fsWatcher.Changed += new
FileSystemEventHandler(OnXMLFileChanged);
fsWatcher.Created += new
FileSystemEventHandler(OnXMLFileCreated);

// Begin watching.
fsWatcher.EnableRaisingEvents = true;
fsWatcher1.EnableRaisingEvents = true;
}
 
J

Jasonkimberson

Any idea why the mapped drive is invalid?

System Events Log:


The following information is part of the event: Service cannot be
started. System.ArgumentException: The directory name Y:\ is invalid.
at System.IO.FileSystemWatcher..ctor(String path, String filter)
at XMLFileWatcher.XMLWatcher.IntializeFileSystemWatcher() in
c:\documents and settings\desktop\service\xmlfilewatcher.cs:line 147
at XMLFileWatcher.XMLWatcher.OnStart(String[] args) in c:\documents
and settings\desktop\service\xmlfilewatcher.cs:line 610
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback
 
W

Willy Denoyette [MVP]

Jasonkimberson said:
Any idea why the mapped drive is invalid?

System Events Log:


The following information is part of the event: Service cannot be
started. System.ArgumentException: The directory name Y:\ is invalid.
at System.IO.FileSystemWatcher..ctor(String path, String filter)
at XMLFileWatcher.XMLWatcher.IntializeFileSystemWatcher() in
c:\documents and settings\desktop\service\xmlfilewatcher.cs:line 147
at XMLFileWatcher.XMLWatcher.OnStart(String[] args) in c:\documents
and settings\desktop\service\xmlfilewatcher.cs:line 610
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback

You cannot access a mapped drive that is mapped into another login session.
The drive Y: is mapped in an interactive users logon session, right?
All you can do to solve this is to map the drive in the logon session of the
service, the easiest wa to do is by executing (using
System.Diagnostics.Process.Start) a "net use..." command from your service
OnStart metod.

Willy.
 
B

Brian Pelton

I was thinking it had to do with the permissions the service was running, but if
it is a drive mapping issue, could a UNC path be used instead?

--Brian

Any idea why the mapped drive is invalid?

System Events Log:


The following information is part of the event: Service cannot be
started. System.ArgumentException: The directory name Y:\ is invalid.
at System.IO.FileSystemWatcher..ctor(String path, String filter)
at XMLFileWatcher.XMLWatcher.IntializeFileSystemWatcher() in
c:\documents and settings\desktop\service\xmlfilewatcher.cs:line 147
at XMLFileWatcher.XMLWatcher.OnStart(String[] args) in c:\documents
and settings\desktop\service\xmlfilewatcher.cs:line 610
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback


You cannot access a mapped drive that is mapped into another login session.
The drive Y: is mapped in an interactive users logon session, right?
All you can do to solve this is to map the drive in the logon session of the
service, the easiest wa to do is by executing (using
System.Diagnostics.Process.Start) a "net use..." command from your service
OnStart metod.

Willy.
 
J

Jasonkimberson

Hi Willy,

Do you have any code samples of this?

thanks a lot for all the help!
 
W

Willy Denoyette [MVP]

Brian Pelton said:
I was thinking it had to do with the permissions the service was running,
but if it is a drive mapping issue, could a UNC path be used instead?

--Brian


Sure, UNC paths are a valid option, but this has it's own issues, services
in general don't have network access privileges (and they better don't have
them), so the service user must have access privileges to the UNC anyway, or
you need to impersonate.

Willy.
 
W

Willy Denoyette [MVP]

Jasonkimberson said:
Hi Willy,

Do you have any code samples of this?

thanks a lot for all the help!

In OnStart() ...

// user credentials with access privs. to remote resource, don't
hardcode if you care about security ;-)
string user = "user account";
string passwd= "user pwd";
System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo();
psi.FileName = @"C:\WINDOWS\system32\cmd.exe";
// The command Arguments, get servername and sharename from a config
file or hardcode...
psi.Arguments =String.Format( "/c net use \\\\servername\\sharename
/USER:\"{0} {1}\" user, passwd);
psi.WindowStyle =System.Diagnostics.ProcessWindowStyle.Hidden; //Hide
the cmd window
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
//Run
p.WaitForExit();
if(p.ExitCode != 0)
// throw an exception to exit OnStart and signal failure to
start to the SCM
throw...
else
// Go on and start your service thread

Willy.
 
J

Jasonkimberson

What is the proper syntax for this:

/c net use \\\\servername\\sharename /USER:\"{0} {1}\" user, passwd

if i typed it into a cmd prompt? =

net use Y: \\server\folder\ /USER:domain/username password


is that correct?
 
J

Jasonkimberson

psi.Arguments =String.Format( "/c net use \\\\servername\\sharename
/USER:\\{0} {1}\\", user, passwd);
 
J

Jasonkimberson

psi.Arguments =String.Format( "/c net use \\\\servername\\sharename
/USER:\\{0} {1}\\", user, passwd);
 
J

Jasonkimberson

I was testing out the map drive before i added it to code

NET USE U: \\master\testshare /User:domain\usernamei passwd
/PERSISTENT:YES

but i get a error

"System error 67 has occurred.

The network name cannot be found."

I am on Windows 2003, and i have already gone to MSDN =
http://support.microsoft.com/?kbid=843156

as for their solution 1: my network driver seems to be working fine.

as for their solution 2: i dont have a IP Network Address translator in
the Non-Plug and play drivers
 
W

Willy Denoyette [MVP]

Jasonkimberson said:
I was testing out the map drive before i added it to code

NET USE U: \\master\testshare /User:domain\usernamei passwd
/PERSISTENT:YES

but i get a error

"System error 67 has occurred.

The network name cannot be found."

I am on Windows 2003, and i have already gone to MSDN =
http://support.microsoft.com/?kbid=843156

as for their solution 1: my network driver seems to be working fine.

as for their solution 2: i dont have a IP Network Address translator in
the Non-Plug and play drivers

this...

net use u: \\master\testshare pwd /user:domain\username
should work, unless \\master\testshare is not existing, are you sure master
is a machinename?

Willy.
 
J

Jasonkimberson

that didnt work for me

but what did work was

using IWshRuntimeLibrary;


public static void createmappeddrive(string driveletter, string
username, string pass)
{
bool blnUpdateProfile = false;
bool blnForce = true;
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
object objstrUserName;
object objstrPassword;
string strMapDriveLetter = driveletter + ":";
WshNetworkClass wshNET = new WshNetworkClass();
//"cbcg\\rwma"
objstrUserName = (object)username;
objstrPassword = (object)pass;
wshNET.MapNetworkDrive(strMapDriveLetter, @"\\networklocation\", ref
objblnUpdateProfile,ref objstrUserName, ref objstrPassword);
}


public static void removemapdrive(string driveletter)
{
bool blnUpdateProfile = false;
bool blnForce = true;
WshNetworkClass wshNET = new WshNetworkClass();
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
string strMapDriveLetter = driveletter + ":";
wshNET.RemoveNetworkDrive(strMapDriveLetter,ref objblnForce,ref
objblnUpdateProfile);
}


i wrote those to open and close a mapped drive before and after a
service is started and stopped
 
J

Jasonkimberson

that didnt work for me

but what did work was

using IWshRuntimeLibrary;


public static void createmappeddrive(string driveletter, string
username, string pass)
{
bool blnUpdateProfile = false;
bool blnForce = true;
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
object objstrUserName;
object objstrPassword;
string strMapDriveLetter = driveletter + ":";
WshNetworkClass wshNET = new WshNetworkClass();

objstrUserName = (object)username;
objstrPassword = (object)pass;
wshNET.MapNetworkDrive(strMapDriveLetter, @"\\networklocation\", ref
objblnUpdateProfile,ref objstrUserName, ref objstrPassword);
}


public static void removemapdrive(string driveletter)
{
bool blnUpdateProfile = false;
bool blnForce = true;
WshNetworkClass wshNET = new WshNetworkClass();
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
string strMapDriveLetter = driveletter + ":";
wshNET.RemoveNetworkDrive(strMapDriveLetter,ref objblnForce,ref
objblnUpdateProfile);
}


i wrote those to open and close a mapped drive before and after a
service is started and stopped
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top