我只想创建一个程序,以便在每次重新启动后通过Form1发出问候消息。我知道我可以使用注册码(CurrentVersion \ Run)来做到这一点,但是我需要通过学习该部分的服务来做到这一点。
我这样制作了Form1
无法从命令行或调试器启动服务。必须首先安装winwows服务(使用installutil.exe),然后使用ServerExplorer,Windows Services Afministrative工具或NET START命令启动。
那我应该在哪里修理?
答案 0 :(得分:2)
要调试Windows服务, 来自Microsoft : How to: Debug Windows Service Applications
-向您的服务添加运行OnStart和OnStop方法的方法:
/**
* @param Request $request
* @return Response
*/
public function run(Request $request): Response
{
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
$method = $request->getMethod();
if ($method === 'OPTIONS') {
return new Response(json_encode([]));
}
$uri = $request->getRequestUri();
// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
if (isset($_SERVER['REDIRECT_BASE'])) {
$uri = str_replace($_SERVER['REDIRECT_BASE'], '', $uri);
}
$routeInfo = $this->dispatcher->dispatch($method, $uri);
try {
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
throw new HttpNotFoundException('404 Not Found');
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
throw new HttpForbiddenException('Method not allowed : ' . prettyArray($allowedMethods));
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
AnnotationRegistry::registerLoader('class_exists');
if (isset($this->requestInterceptor)) {
return $this->requestInterceptor->handleRequest($request, $routeInfo);
}
$vars = $routeInfo[2];
//var_dump($vars);
$response = null;
if (count($vars) > 0) {
$args = [];
foreach ($vars as $k => $v) {
$args[] = $v;
}
$args[] = $request;
$response = call_user_func_array($handler, $args);
} else {
$response = call_user_func($handler, $request);
}
return $response;
break;
}
} catch (QueryException $e) {
return new Response(json_encode($e->getTrace()));
} catch (HttpExceptionInterface $e) {
return $e->asResponse();
}
// return new Response();
return null;
}
-重写Main方法,如下所示:
internal void TestStartupAndStop(string[] args)
{
this.OnStart(args);
Console.ReadLine();
this.OnStop();
}
答案 1 :(得分:0)
据我了解,在尝试调试时遇到错误。
在调试模式下与发布模式下运行时,您需要以不同的方式运行服务,以下代码是我使用的:
static void Main()
{
#if !DEBUG
ServiceBase[] servicesToRun = new ServiceBase[]
{
new Scheduler()
};
ServiceBase.Run(servicesToRun);
#else
Scheduler service = new Scheduler();
service.ScheduleService();
Thread.Sleep(Timeout.Infinite);
#endif
}