From 6c5191d1180338ec61ce259f3803d20ab0dd2693 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Thu, 14 Oct 2021 22:22:25 +0800 Subject: [PATCH] init --- .gitignore | 4 ++++ Program.cs | 42 +++++++++++++++++++++++++++++++++++ app.config | 0 hello.csproj | 12 ++++++++++ model/cateory.cs | 14 ++++++++++++ model/product.cs | 11 +++++++++ service/CarService.cs | 49 +++++++++++++++++++++++++++++++++++++++++ service/MsSqlService.cs | 22 ++++++++++++++++++ 8 files changed, 154 insertions(+) create mode 100644 .gitignore create mode 100644 Program.cs create mode 100644 app.config create mode 100644 hello.csproj create mode 100644 model/cateory.cs create mode 100644 model/product.cs create mode 100644 service/CarService.cs create mode 100644 service/MsSqlService.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..129766a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vscode/ +bin/ +obj/ +*.exe \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..a2ec941 --- /dev/null +++ b/Program.cs @@ -0,0 +1,42 @@ +using System; +using Dapper; +using System.Data.SqlClient; +using System.Configuration; +using Topshelf; + +namespace hello +{ +class Program +{ + static void Main(string[] args) + { + // 配置和运行宿主服务 + HostFactory.Run(x => + { + // 指定服务类型。这里设置为 CacheService + x.Service(s => + { + // 通过 new CacheService() 构建一个服务实例 + s.ConstructUsing(name => new CarService()); + // 当服务启动后执行什么 + s.WhenStarted(tc => tc.Start()); + // 当服务停止后执行什么 + s.WhenStopped(tc => tc.Stop()); + }); + + // 服务用本地系统账号来运行 + x.RunAsLocalSystem(); + + // 服务描述信息 + x.SetDescription("车辆记录服务"); + // 服务显示名称 + x.SetDisplayName("CarService"); + // 服务名称 + x.SetServiceName("CarService"); + }); + } + } +} + + + diff --git a/app.config b/app.config new file mode 100644 index 0000000..e69de29 diff --git a/hello.csproj b/hello.csproj new file mode 100644 index 0000000..deb9d94 --- /dev/null +++ b/hello.csproj @@ -0,0 +1,12 @@ + + + Exe + netcoreapp3.1 + + + + + + + + \ No newline at end of file diff --git a/model/cateory.cs b/model/cateory.cs new file mode 100644 index 0000000..d1dd3a4 --- /dev/null +++ b/model/cateory.cs @@ -0,0 +1,14 @@ + +using System.ComponentModel.DataAnnotations; + +namespace model +{ + public class Category + { + [Key] + public int Id {get;set;} + public string Name {get;set;} + } + + +} \ No newline at end of file diff --git a/model/product.cs b/model/product.cs new file mode 100644 index 0000000..0e6d4c9 --- /dev/null +++ b/model/product.cs @@ -0,0 +1,11 @@ +namespace hello.model +{ + public class Product + { + public int Id {get;set;} + public string Name{get;set;} + public int Quantity{get;set;} + public int CategoryId{get;set;} + } + +} \ No newline at end of file diff --git a/service/CarService.cs b/service/CarService.cs new file mode 100644 index 0000000..e37d694 --- /dev/null +++ b/service/CarService.cs @@ -0,0 +1,49 @@ +using System; +using System.Configuration; +using System.Threading; + +namespace hello +{ + public class CarService + { + // private readonly string host = ConfigurationManager.AppSettings["Host"]; + // private readonly string port = ConfigurationManager.AppSettings["Port"]; + + private Thread instance; + + public void Start() + { + try{ + isalive=true; + instance=new Thread(new ThreadStart(CarImpel)); + instance.Start(); + }catch(Exception ex){ + Console.WriteLine(ex.Message); + } + } + public void Stop() + { + try + { + if(instance!=null) + { + isalive=false; + instance.Join(500); + } + }finally{ + instance=null; + } + } + + static bool isalive=false; + + static void CarImpel() + { + while(isalive) + { + Console.WriteLine(DateTime.Now.ToString()); + Thread.Sleep(1000); + } + } + } +} \ No newline at end of file diff --git a/service/MsSqlService.cs b/service/MsSqlService.cs new file mode 100644 index 0000000..18f4442 --- /dev/null +++ b/service/MsSqlService.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data.SqlClient; +using Dapper; + +namespace hello.service +{ + public class MsSqlService + { + public IEnumerable GetClient() + { + string connectionString = ""; + using(var sqlconn=new SqlConnection(connectionString)) + { + sqlconn.Open(); + var products= sqlconn.Query("select * from product"); + return products; + } + } + } +} \ No newline at end of file