C#使用windows服务定时调用api接口
使用VS创建windows服务项目:
创建好项目 会出现一个设计界面 右键弹出对话框 选择添加安装程序
名字什么的自己可以改:
项目目录:
打开项目中的ProjectInstaller.Designer.cs 修改windows服务名称描述以及启动方式等:
partial class ProjectInstaller
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container(); // 创建ServiceProcessInstaller对象和ServiceInstaller对象
this.spInstaller = new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller(); // 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null; // 设定服务名称
this.sInstaller.ServiceName = "CallApiExeTask"; //服务描述
this.sInstaller.Description = "定时调用api接口,获取任务后操作数据库"; // 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic; //
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.spInstaller,
this.sInstaller}); } #endregion private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
}
打开Service1 写入想要执行的操作等:
public partial class Service1 : ServiceBase
{
System.Timers.Timer timer1; //计时器
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
//服务开启执行代码
//定时调用接口 timer1 = new System.Timers.Timer();
timer1.Interval = ; //设置计时器事件间隔执行时间
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
timer1.Enabled = true; }
/// <summary>
/// 定时器 调用的方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.10.239:9000/");//接口url
string data = client.GetStringAsync("ZyTest").Result;//接口action
} protected override void OnStop()
{
//服务结束执行代码
this.timer1.Enabled = false;
} protected override void OnPause()
{
//服务暂停执行代码
base.OnPause();
}
protected override void OnContinue()
{
//服务恢复执行代码
base.OnContinue();
}
protected override void OnShutdown()
{
//系统即将关闭执行代码
base.OnShutdown();
}
}
Program.cs中可以设置主方法调用的service服务:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
生成解决方案,以上就完成了windows服务的编写
下面需要把服务安装到服务器或者pc上:
首先在网上下载一个installutil.exe文件(百度直接搜索可以下载) 放到...\bin\Debug文件夹下:
用管理员身份打开命令提示符:
输入 cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 回车
如果你的程序是2.0的framework则需要输入 cd C:\Windows\Microsoft.NET\Framework\v2.0.50727
输入 InstallUtil.exe D:\work\windows服务\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe 回车 完成安装
说明: D:\work\windows服务\WinService4ExeApi\WinService4ExeApi\bin\Debug\WinService4ExeApi.exe表示项目生成的exe文件位置(也可以把debug文件夹单独copy到其他地方重命名)
安装完成后:打开服务就可以看到了
如果需要卸载此服务:打开cmd 直接输入 sc delete CallApiExeTask便可 CallApiExeTask为你的服务名称
ps: 如果你的windows服务程序修改了 需要更新成新版本 不用卸载服务再安装 只需先停掉该服务 然后把文件夹内的文件替换为新的文件 重新启动该服务即可
C#使用windows服务定时调用api接口的更多相关文章
- 玩转Windows服务系列——无COM接口Windows服务启动失败原因及解决方案
将VS创建的Windows服务项目编译生成的程序,通过命令行 “服务.exe -Service”注册为Windows服务后,就可以通过服务管理器进行管理了. 问题 通过服务管理器进行启动的时候,发现服 ...
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- Python调用API接口的几种方式 数据库 脚本
Python调用API接口的几种方式 2018-01-08 gaoeb97nd... 转自 one_day_day... 修改 微信分享: 相信做过自动化运维的同学都用过API接口来完成某些动作.AP ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...
- vs 2010创建Windows服务定时timer程序
vs 2010创建Windows服务定时timer程序: 版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处: http://www.cnblogs.com/2186009311CFF/p/ ...
- 调用API接口,查询手机号码归属地(3)
从mysql数据库获取电话号码,查询归属地并插入到数据库 #!/usr/bin/python # -*- coding: utf-8 -*- import json, urllib, sys, pym ...
- 调用API接口,查询手机号码归属地(2)
使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- 使用C#开发windows服务定时发消息到钉钉群_群组简单消息
前言:本提醒服务,是由C#语言开发的,主要由windows服务项目和winform项目组成,运行服务可实现功能:向钉钉自定义机器人群组里,定时,定次,推送多个自定义消息内容,并实现主要功能的日志记录. ...
随机推荐
- Qt 出现“undefined reference to `vtable for”原因总结
http://blog.csdn.net/chenlong12580/article/details/7431104
- 微信小程序调微信支付
今天写小程序的支付接口,参照的当然是微信支付API了.(结尾附上第二步全部代码php版) 另外,我也参照了简书上的这篇文章,浅显易懂:https://www.jianshu.com/p/72f5c1e ...
- ubuntu 上查看文件的内容,二进制形式展现
Vim 可以用来查看和编辑二进制文件 vim -b egenea-base.ko 加上-b参数,以二进制打开 然后输入命令 :%!xxd -g 1 切换到十六进制模式显示
- Fedora14使用yum安装mysql
linux下使用yum安装mysql 1.安装 查看有没有安装过: yum list installed mysql* rpm -qa | grep mys ...
- opencv之图像阈值化处理
一.函数简介 1.threshold-图像简单阈值化处理 函数原型:threshold(src, thresh, maxval, type, dst=None) src:图像矩阵 thresh:阈值 ...
- SQLite3的运用
1.导入库文件:libsqlite3. 2..m文件的代码: // // ViewController.m // SQLite3的运用 // // Created by PengYunjing on ...
- FIR滤波器的FPGA实现方法
FIR滤波器的FPGA实现方法 2011-02-21 23:34:15 来源:互联网 非常重要的基本单元.近年来,由于FPGA具有高速度.高集成度和高可靠性的特点而得到快速发展.随着现代数字 ...
- sqlalchemy 学习(二)scoped session
数据库设计的难点之一,是session生命周期的管理问题.sqlalchemy提供了一个简单的session管理机制,即scoped session.它采用的注册模式.所谓的注册模式,简单来说,是指在 ...
- jenkins 重置密码
说明 最近在折腾jenkins,配置用户权限时点错了,选择了安全矩阵后没有添加用户,就保存配置了,然后就报错了,提示没有Overall/Read权限.还有另外一个问题,用户的密码忘记了怎么办? 一 ...
- numpy之初探排序和集合运算
排序 排序 numpy与python列表内置的方法类似,也可通过sort方法进行排序. 用法如下: In [1]: import numpy as np In [2]: x = np.random.r ...