vs编写windows服务和调用webservice

  1. 调用timer不能用 System.Windows.Forms.Timer而应该用System.Timers.Timer
  2.   System.Timers.Timer t = new System.Timers.Timer(interVal);//实例化Timer类,设置间隔时间为10000毫秒; 
      t.Elapsed += new System.Timers.ElapsedEventHandler(Tick);//到达时间的时候执行事件; 
      t.AutoReset = true;//设置是执行一次(false)还是一直执行(true); 
      t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;\
  3. timer函数主体
            public void Tick(object source, System.Timers.ElapsedEventArgs e)
            {
                string machineName = Dns.GetHostName();
                localhost.Service ipService = new MyService.localhost.Service();
                string status = ipService.AddValue(machineName, ips);  //调用webservice
            }  
  4. 项目右键-添加web引用,找到 webservice
  5. 在App.config中有节点,修改即可
  6.   <applicationSettings>
        <MyService.Properties.Settings>
          <setting name="MyService_localhost_Service" serializeAs="String">
            <value>http://localhost/MyWebService/Service.asmx</value>
          </setting>
        </MyService.Properties.Settings>
      </applicationSettings>
  7. 找到Service1.cs,设计窗口,右键,"添加安装程序"
  8. serviceInstaller1的serviceName为安装的服务
  9. serviceProcessInstaller1的Account修改为LocalSystem
  10. 开始-运行-CMD,输入命令"cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
  11. 输入命令"InstallUtil.exe 安装路径\MyService.exe"
  12. 在服务里面就能看到自己写的windows服务了,右键属性,能修改自动运行 还是手动运行
原文地址:https://www.cnblogs.com/threestone/p/1725549.html