Salut
Je suis tout nouveau en .net, je suis entrain de suivre le un kit de Microsoft.
dans un des exercice(Lab), on m'a demander de faire un service qui se connecte a www.microsoft.com et qui écrits quelque chose un fichier log.
J'ai suivie toutes les étapes a la lettre, j'ai lancé le service a partir de "services.msc", mais je ne trouve nulpart mon fichier log.
voici le code, je ne sais pas s'il y'a une erreur
Service1.cs
ProjectInstaller.cs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Timers; using System.IO; using System.Net; namespace MonitorWebSite { public partial class MonitorWebSite : ServiceBase { private Timer t = null; public MonitorWebSite() { InitializeComponent(); t = new Timer(10000); t.Elapsed += new ElapsedEventHandler(t_Elapsed); } void t_Elapsed(object sender, ElapsedEventArgs e) { try { // Send the HTTP request string url = "http://www.microsoft.com"; HttpWebRequest g = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse r = (HttpWebResponse)g.GetResponse(); // Log the response to a text file string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log.txt"; TextWriter tw = new StreamWriter(path, true); tw.WriteLine(DateTime.Now.ToString() + " for " + url + ": " + r.StatusCode.ToString()); tw.Close(); // Close the HTTP response r.Close(); } catch (Exception ex) { System.Diagnostics.EventLog.WriteEntry("Application", "Exception: " + ex.Message.ToString()); } } #region OnStart, OnStop, OnContinue, et OnShutdown méthodes protected override void OnStart(string[] args) { t.Start(); } protected override void OnStop() { t.Stop(); } protected override void OnContinue() { t.Start(); } protected override void OnPause() { t.Stop(); } protected override void OnShutdown() { t.Stop(); } #endregion } }
program.cs
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; namespace MonitorWebSite { [RunInstaller(true)] public partial class ProjectInstaller : Installer { public ProjectInstaller() { InitializeComponent(); } private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e) { } } }
Merci.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; namespace MonitorWebSite { static class Program { /// <summary> /// Point d'entrée principal de l'application. /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MonitorWebSite() }; ServiceBase.Run(ServicesToRun); } } }
Partager