C#使用windows服务定时调用api接口

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 = 3000;  //设置计时器事件间隔执行时间
            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服务程序修改了 需要更新成新版本 不用卸载服务再安装 只需先停掉该服务 然后把文件夹内的文件替换为新的文件 重新启动该服务即可

posted @ 2017-07-27 10:46 青衫仗剑 阅读( ...) 评论( ...) 编辑 收藏
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用 AWS API 接口使用 AWS 鉴权方式,可以通过 AWS SDK for .NET 来实现。以下是一个简单的示例: 1. 首先,需要安装 AWS SDK for .NET。可以通过 NuGet 包管理器来安装。 2. 在代码中引入 AWS SDK 的命名空间: ```csharp using Amazon; using Amazon.Runtime; using Amazon.Runtime.CredentialManagement; using Amazon.S3; using Amazon.S3.Model; ``` 3. 创建 AWS 访问凭证对象。可以使用以下代码从 AWS 访问密钥文件或凭证配置文件中读取访问密钥和秘密访问密钥: ```csharp var options = new CredentialProfileOptions { AccessKey = "YOUR_ACCESS_KEY", SecretKey = "YOUR_SECRET_KEY" }; var profile = new CredentialProfile("profile-name", options); var profileAWSCredentials = new CredentialProfileStoreChain().TryGetAWSCredentials(profile, out var awsCredentials); ``` 4. 创建 AWS S3 客户端对象,并使用上一步中创建的访问凭证对象进行身份验证: ```csharp var region = RegionEndpoint.GetBySystemName("us-west-2"); // 指定 AWS 区域 var s3Client = new AmazonS3Client(awsCredentials, region); ``` 5. 调用 AWS S3 API 接口。以下是一个示例: ```csharp var request = new PutObjectRequest { BucketName = "my-bucket", Key = "my-object", ContentBody = "Hello World!" }; var response = await s3Client.PutObjectAsync(request); ``` 以上代码演示了如何使用 AWS SDK for .NET 调用 AWS S3 API 接口使用 AWS 鉴权方式进行身份验证。根据实际情况,需要将代码中的访问密钥和秘密访问密钥替换为自己的凭证信息,并将区域、桶名和对象键等参数替换为实际的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值