最终效果直接运行.exe命令窗口程序启动服务
使用 owin 相关技术
owin使用 - 执念、旧时光 - 博客园 相关知识
项目结构
1.项目引用核心包
2.新建 Startup类
using Owin;
using System.Web.Http;namespace SyncApi
{public class Startup{public void Configuration(IAppBuilder appBuilder){HttpConfiguration config = new HttpConfiguration();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional });appBuilder.UseWebApi(config);}}
}
3.在 Program 类启动服务
using Microsoft.Owin.Hosting;
using System;
using System.Configuration;
using System.Net;
using System.Net.Mail;namespace SyncApi
{class Program{static void Main(string[] args){//我把地址放进App.config 配置文件中方便配置//发布地址如 localhost:8848string baseAddress = ConfigurationManager.AppSettings["loc"];if (!string.IsNullOrEmpty(baseAddress)){//数据库连接串if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["db"])){try{// 启动 OWIN host WebApp.Start<Startup>(url: baseAddress);SendWrite("启动成功");SendWrite("请勿关闭当前窗口");}catch (Exception ex){SendWrite("启动失败");SendWrite(ex.Message,true);}}else{SendWrite("请配置数据库信息");}}else{SendWrite("请配置服务路径");}Console.ReadLine();}public static void SendWrite(string msg, bool isEmail = false){Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " : " + msg);if (isEmail){sendEmail("接口异常"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), msg);}}private static void sendEmail(string title, string content){try{SmtpClient smtp = new SmtpClient();smtp.Host = "smtp.qq.com";smtp.Port = 587;smtp.Credentials = new NetworkCredential("配置好的邮箱", "配置好后的密码");MailMessage objMailMessage = new MailMessage();objMailMessage.Priority = MailPriority.High;objMailMessage.From = new MailAddress("配置好的邮箱", "系统提醒", System.Text.Encoding.UTF8);objMailMessage.To.Add(ConfigurationManager.AppSettings["emails"]);objMailMessage.Subject = title.Trim();objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;objMailMessage.Body = content.Trim();objMailMessage.IsBodyHtml = true;objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;objMailMessage.Headers.Add("X-Priority", "3");objMailMessage.Headers.Add("X-MSMail-Priority", "Normal");objMailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");objMailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");objMailMessage.Headers.Add("ReturnReceipt", "1");//发送smtp.Send(objMailMessage);}catch (Exception ex){SendWrite(ex.Message);}}}
}
4.api接口写法同webApi一致 Controllers文件下面新建控制器 继承自 ApiController 就好了
using SyncApi.Service;
using SyncApi.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;namespace SyncApi.Controllers
{public class IndexController : BaseController{private HYNLService _hYNLService = new HYNLService();[HttpGet]public async Task<IHttpActionResult> GetPageList(string pagination){try{var pageInfo = pagination.ToObject<Pagination>();var data = await _hYNLService.GetPageList(pageInfo);Program.SendWrite("响应成功:GetPageList");return Success(data);}catch (Exception ex){Program.SendWrite(ex.Message, true);return Fail("error");}}}
}