src: dotnet: Service testing

This commit is contained in:
Bill Zissimopoulos 2017-04-07 10:29:50 -07:00
parent b45cff2881
commit 964f2eed69

View File

@ -33,8 +33,9 @@ namespace Fsp
/* ctor/dtor */ /* ctor/dtor */
public Service(String ServiceName) public Service(String ServiceName)
{ {
_CreateResult = Api.FspServiceCreate(ServiceName, OnStart, OnStop, null, out _Service); Api.FspServiceCreate(ServiceName, OnStart, OnStop, null, out _Service);
Api.SetUserContext(_Service, this); if (IntPtr.Zero != _Service)
Api.SetUserContext(_Service, this);
} }
~Service() ~Service()
{ {
@ -48,33 +49,32 @@ namespace Fsp
/* control */ /* control */
public void Run() public void Run()
{ {
if (0 > _CreateResult) if (IntPtr.Zero == _Service)
{ {
Log(EVENTLOG_ERROR_TYPE, Log(EVENTLOG_ERROR_TYPE,
String.Format("The service {0} cannot be created (Status={1:X}).", String.Format("The service {0} cannot be created (Status={1:X}).",
GetType().FullName, _CreateResult)); GetType().FullName, unchecked((Int32)0xc000009a)/*STATUS_INSUFFICIENT_RESOURCES*/));
return; return;
} }
Api.FspServiceAllowConsoleMode(_Service); Api.FspServiceAllowConsoleMode(_Service);
Int32 Result = Api.FspServiceLoop(_Service); Int32 Result = Api.FspServiceLoop(_Service);
UInt32 ExitCode = Api.FspServiceGetExitCode(_Service); if (0 > Result)
if (0 > _CreateResult)
{ {
Log(EVENTLOG_ERROR_TYPE, Log(EVENTLOG_ERROR_TYPE,
String.Format("The service {0} has failed to run (Status={1:X}).", String.Format("The service {0} has failed to run (Status={1:X}).",
GetType().FullName, _CreateResult)); GetType().FullName, Result));
return; return;
} }
} }
public void Stop() public void Stop()
{ {
if (0 > _CreateResult) if (IntPtr.Zero == _Service)
throw new InvalidOperationException(); throw new InvalidOperationException();
Api.FspServiceStop(_Service); Api.FspServiceStop(_Service);
} }
public void RequestTime(UInt32 Time) public void RequestTime(UInt32 Time)
{ {
if (0 > _CreateResult) if (IntPtr.Zero == _Service)
throw new InvalidOperationException(); throw new InvalidOperationException();
Api.FspServiceRequestTime(_Service, Time); Api.FspServiceRequestTime(_Service, Time);
} }
@ -82,13 +82,13 @@ namespace Fsp
{ {
get get
{ {
if (0 > _CreateResult) if (IntPtr.Zero == _Service)
throw new InvalidOperationException(); throw new InvalidOperationException();
return (int)Api.FspServiceGetExitCode(_Service); return (int)Api.FspServiceGetExitCode(_Service);
} }
set set
{ {
if (0 > _CreateResult) if (IntPtr.Zero == _Service)
throw new InvalidOperationException(); throw new InvalidOperationException();
Api.FspServiceSetExitCode(_Service, (UInt32)value); Api.FspServiceSetExitCode(_Service, (UInt32)value);
} }
@ -144,7 +144,6 @@ namespace Fsp
} }
private IntPtr _Service; private IntPtr _Service;
private Int32 _CreateResult;
} }
} }