This commit is contained in:
suguo.yao 2021-10-14 22:22:25 +08:00
commit 6c5191d118
8 changed files with 154 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
.vscode/
bin/
obj/
*.exe

42
Program.cs Normal file
View File

@ -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<CarService>(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");
});
}
}
}

0
app.config Normal file
View File

12
hello.csproj Normal file
View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.90"/>
<PackageReference Include="MySql.Data" Version="8.0.26"/>
<PackageReference Include="System.Data.SqlClient" Version="4.6.1"/>
<PackageReference Include="Topshelf" Version="4.3.0"/>
</ItemGroup>
</Project>

14
model/cateory.cs Normal file
View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace model
{
public class Category
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
}
}

11
model/product.cs Normal file
View File

@ -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;}
}
}

49
service/CarService.cs Normal file
View File

@ -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);
}
}
}
}

22
service/MsSqlService.cs Normal file
View File

@ -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<model.Product> GetClient()
{
string connectionString = "";
using(var sqlconn=new SqlConnection(connectionString))
{
sqlconn.Open();
var products= sqlconn.Query<model.Product>("select * from product");
return products;
}
}
}
}