From 23461b15a9fe6ac13e180fae9e1d2253fcc06562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 10 Oct 2021 19:10:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=90=84=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.ApiMicroservice/YIDB.db | Bin 0 -> 20480 bytes .../Yi.Framework.ApiMicroservice.csproj | 4 + .../appsettings.json | 3 + Yi.Framework/Yi.Framework.Core/MD5Helper.cs | 2 +- .../Yi.Framework.Core/RabbitMQInvoker.cs | 4 +- .../Yi.Framework.Core.csproj | 31 ++ .../Yi.Framework.Interface/IBaseService.cs | 77 +++++ .../{Models => }/DataContext.cs | 9 +- .../20211010110842_yi-1.Designer.cs | 39 +++ .../Migrations/20211010110842_yi-1.cs | 30 ++ .../Migrations/DataContextModelSnapshot.cs | 37 +++ .../Yi.Framework.Model/Models/user.cs | 17 ++ .../Yi.Framework.Service/BaseService.cs | 119 ++++++++ .../Yi.Framework.Service.csproj | 5 + .../Yi.Framework.WebCore/CommonExtend.cs | 68 ++--- .../CustomAction2CommitFilterAttribute.cs | 4 +- .../CustomExceptionFilterAttribute.cs | 2 +- .../FilterExtend/LogActionFilterAttribute.cs | 2 +- .../MiddlewareExtend/StaticPageMiddleware.cs | 288 +++++++++--------- .../Yi.Framework.WebCore.csproj | 7 + 20 files changed, 560 insertions(+), 188 deletions(-) create mode 100644 Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db create mode 100644 Yi.Framework/Yi.Framework.Interface/IBaseService.cs rename Yi.Framework/Yi.Framework.Model/{Models => }/DataContext.cs (82%) create mode 100644 Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs create mode 100644 Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.cs create mode 100644 Yi.Framework/Yi.Framework.Model/Migrations/DataContextModelSnapshot.cs create mode 100644 Yi.Framework/Yi.Framework.Model/Models/user.cs create mode 100644 Yi.Framework/Yi.Framework.Service/BaseService.cs diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db b/Yi.Framework/Yi.Framework.ApiMicroservice/YIDB.db new file mode 100644 index 0000000000000000000000000000000000000000..777030cef8929691d0551b4f660c32838251c7e6 GIT binary patch literal 20480 zcmeI&zfaph6bJA-J3xtK(uGDy9ZpsUP-tW)t)&41WS!|AUTw&W{!%XX^s`zW9{$$@hHk<1Clt;A=a|Lh&O`&Vx)e*-NG5~wKvp3KmY;|fB*y_009U<00Izz zz<&_vm6ofu8t-Sp@mZK&oYD5qPs58*I6Rp@UD|b}6a+Y;^eY$P + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json index d9d9a9bf..49492726 100644 --- a/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework/Yi.Framework.ApiMicroservice/appsettings.json @@ -6,5 +6,8 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + "SqliteConn": { + "Url": "Filename=YIDB.db" + }, "AllowedHosts": "*" } diff --git a/Yi.Framework/Yi.Framework.Core/MD5Helper.cs b/Yi.Framework/Yi.Framework.Core/MD5Helper.cs index 52d7c582..e0cc0551 100644 --- a/Yi.Framework/Yi.Framework.Core/MD5Helper.cs +++ b/Yi.Framework/Yi.Framework.Core/MD5Helper.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Security.Cryptography; using System.Text; -namespace CC.ElectronicCommerce.Core +namespace Yi.Framework.Core { /// diff --git a/Yi.Framework/Yi.Framework.Core/RabbitMQInvoker.cs b/Yi.Framework/Yi.Framework.Core/RabbitMQInvoker.cs index 3e7e6e2c..0b7da2cd 100644 --- a/Yi.Framework/Yi.Framework.Core/RabbitMQInvoker.cs +++ b/Yi.Framework/Yi.Framework.Core/RabbitMQInvoker.cs @@ -1,5 +1,4 @@ -using CC.ElectronicCommerce.Common.IOCOptions; -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Options; using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; @@ -7,6 +6,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.IOCOptions; namespace CC.ElectronicCommerce.Core { diff --git a/Yi.Framework/Yi.Framework.Core/Yi.Framework.Core.csproj b/Yi.Framework/Yi.Framework.Core/Yi.Framework.Core.csproj index f208d303..5af9d499 100644 --- a/Yi.Framework/Yi.Framework.Core/Yi.Framework.Core.csproj +++ b/Yi.Framework/Yi.Framework.Core/Yi.Framework.Core.csproj @@ -4,4 +4,35 @@ net5.0 + + + + + + + + + + + + + + + + Library\Microsoft.Bcl.AsyncInterfaces.dll + + + Library\ServiceStack.Common.dll + + + Library\ServiceStack.Interfaces.dll + + + Library\ServiceStack.Redis.dll + + + Library\ServiceStack.Text.dll + + + diff --git a/Yi.Framework/Yi.Framework.Interface/IBaseService.cs b/Yi.Framework/Yi.Framework.Interface/IBaseService.cs new file mode 100644 index 00000000..6e7e8a86 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Interface/IBaseService.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Interface +{ + public interface IBaseService where T : class, new() + { + #region + //通过id得到实体 + #endregion + Task GetEntityById(int id); + + #region + //得到全部实体 + #endregion + Task> GetAllEntitiesAsync(); + + #region + //通过表达式得到实体 + #endregion + Task> GetEntitiesAsync(Expression> whereLambda); + + #region + //通过表达式得到实体,分页版本 + #endregion + Task GetCountAsync(Expression> whereLambda); + + #region + //通过表达式统计数量 + #endregion + IQueryable> GetGroup(Expression> whereLambda, Expression> groupByLambda); + + #region + //通过表达式分组 + #endregion + Task, int>> GetPageEntities(int pageSize, int pageIndex, Expression> whereLambda, Expression> orderByLambda, bool isAsc); + + #region + //添加实体 + #endregion + Task AddAsync(T entity); + + #region + //添加多个实体 + #endregion + Task AddAsync(IEnumerable entities); + + #region + //更新实体 + #endregion + Task UpdateAsync(T entity); + + #region + //更新实体部分属性 + #endregion + Task DeleteAsync(T entity); + + #region + //删除实体 + #endregion + Task DeleteAsync(int id); + + #region + //通过id删除实体 + #endregion + Task DeleteAsync(IEnumerable ids); + + #region + //通过id列表删除多个实体 + #endregion + Task DeleteAsync(Expression> where); + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Models/DataContext.cs b/Yi.Framework/Yi.Framework.Model/DataContext.cs similarity index 82% rename from Yi.Framework/Yi.Framework.Model/Models/DataContext.cs rename to Yi.Framework/Yi.Framework.Model/DataContext.cs index ce008f16..7c2effbc 100644 --- a/Yi.Framework/Yi.Framework.Model/Models/DataContext.cs +++ b/Yi.Framework/Yi.Framework.Model/DataContext.cs @@ -6,9 +6,12 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Model.Models; -namespace Yi.Framework.Model.Models +namespace Yi.Framework.Model { + //Add-Migration yi-1 + //Update-Database yi-1 public class DataContext : DbContext { private readonly IOptionsMonitor _optionsMonitor; @@ -31,6 +34,6 @@ namespace Yi.Framework.Model.Models optionsBuilder.UseSqlite(_connStr); } } - //public virtual DbSet TbBrand { get; set; } - } + public virtual DbSet user { get; set; } + } } diff --git a/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs b/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs new file mode 100644 index 00000000..aceef25a --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.Designer.cs @@ -0,0 +1,39 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Yi.Framework.Model; + +namespace Yi.Framework.Model.Migrations +{ + [DbContext(typeof(DataContext))] + [Migration("20211010110842_yi-1")] + partial class yi1 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.10"); + + modelBuilder.Entity("Yi.Framework.Model.Models.user", b => + { + b.Property("id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("age") + .HasColumnType("INTEGER"); + + b.Property("name") + .HasColumnType("TEXT"); + + b.HasKey("id"); + + b.ToTable("user"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.cs b/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.cs new file mode 100644 index 00000000..36b28f2e --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Migrations/20211010110842_yi-1.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace Yi.Framework.Model.Migrations +{ + public partial class yi1 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "user", + columns: table => new + { + id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + name = table.Column(type: "TEXT", nullable: true), + age = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_user", x => x.id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "user"); + } + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Migrations/DataContextModelSnapshot.cs b/Yi.Framework/Yi.Framework.Model/Migrations/DataContextModelSnapshot.cs new file mode 100644 index 00000000..76f38a1f --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Migrations/DataContextModelSnapshot.cs @@ -0,0 +1,37 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Yi.Framework.Model; + +namespace Yi.Framework.Model.Migrations +{ + [DbContext(typeof(DataContext))] + partial class DataContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "5.0.10"); + + modelBuilder.Entity("Yi.Framework.Model.Models.user", b => + { + b.Property("id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("age") + .HasColumnType("INTEGER"); + + b.Property("name") + .HasColumnType("TEXT"); + + b.HasKey("id"); + + b.ToTable("user"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Yi.Framework/Yi.Framework.Model/Models/user.cs b/Yi.Framework/Yi.Framework.Model/Models/user.cs new file mode 100644 index 00000000..f541a63b --- /dev/null +++ b/Yi.Framework/Yi.Framework.Model/Models/user.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Model.Models +{ + public class user + { + [Key] + public int id { get; set; } + public string name { get; set; } + public int age { get; set; } + } +} diff --git a/Yi.Framework/Yi.Framework.Service/BaseService.cs b/Yi.Framework/Yi.Framework.Service/BaseService.cs new file mode 100644 index 00000000..dabf9017 --- /dev/null +++ b/Yi.Framework/Yi.Framework.Service/BaseService.cs @@ -0,0 +1,119 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Interface; + +namespace Yi.Framework.Service +{ + public class CCBaseServer : IBaseService where T : class, new() + { + public DbContext _Db; + public CCBaseServer(DbContext Db) + { + _Db = Db; + } + + public async Task GetEntityById(int id) + { + return await _Db.Set().FindAsync(id); + } + + + public async Task> GetAllEntitiesAsync() + { + return await _Db.Set().ToListAsync(); + } + + public async Task> GetEntitiesAsync(Expression> whereLambda) + { + return await _Db.Set().Where(whereLambda).ToListAsync(); + } + + public async Task GetCountAsync(Expression> whereLambda) //统计数量 + { + return await _Db.Set().CountAsync(whereLambda); + } + + public IQueryable> GetGroup(Expression> whereLambda, Expression> groupByLambda) //分组 + { + return _Db.Set().Where(whereLambda).GroupBy(groupByLambda).AsQueryable(); + } + + public async Task, int>> GetPageEntities(int pageSize, int pageIndex, Expression> whereLambda, Expression> orderByLambda, bool isAsc) + { + int total = await GetCountAsync(whereLambda); + + IEnumerable pageData; + if (isAsc) + { + pageData = await _Db.Set().Where(whereLambda) + .OrderBy(orderByLambda) + .Skip(pageSize * (pageIndex - 1)) + .Take(pageSize).ToListAsync(); + } + else + { + pageData = await _Db.Set().Where(whereLambda) + .OrderByDescending(orderByLambda) + .Skip(pageSize * (pageIndex - 1)) + .Take(pageSize).ToListAsync(); + } + + return Tuple.Create, int>(pageData, total); + } + + public async Task AddAsync(T entity) + { + _Db.Set().Add(entity); + return await _Db.SaveChangesAsync() > 0; + } + + public async Task AddAsync(IEnumerable entities) + { + _Db.Set().AddRange(entities); + return await _Db.SaveChangesAsync() > 0; + } + + public async Task UpdateAsync(T entity) + { + _Db.Set().Update(entity); + return await _Db.SaveChangesAsync() > 0; + } + + public async Task DeleteAsync(T entity) + { + _Db.Set().Remove(entity); + return await _Db.SaveChangesAsync() > 0; + } + + public async Task DeleteAsync(int id) + { + _Db.Set().Remove(await GetEntityById(id)); + return await _Db.SaveChangesAsync() > 0; + } + + public async Task DeleteAsync(IEnumerable ids) + { + foreach (var id in ids) + { + _Db.Set().RemoveRange(await GetEntityById(id)); + } + return await _Db.SaveChangesAsync() > 0; + } + public async Task DeleteAsync(Expression> where) + { + IEnumerable entities = await GetEntitiesAsync(where); + if (entities != null) + { + _Db.Set().RemoveRange(entities); + + return await _Db.SaveChangesAsync() > 0; + } + return false; + } + } +} diff --git a/Yi.Framework/Yi.Framework.Service/Yi.Framework.Service.csproj b/Yi.Framework/Yi.Framework.Service/Yi.Framework.Service.csproj index f208d303..4b21d24f 100644 --- a/Yi.Framework/Yi.Framework.Service/Yi.Framework.Service.csproj +++ b/Yi.Framework/Yi.Framework.Service/Yi.Framework.Service.csproj @@ -4,4 +4,9 @@ net5.0 + + + + + diff --git a/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs b/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs index a6a4712e..0158a988 100644 --- a/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs +++ b/Yi.Framework/Yi.Framework.WebCore/CommonExtend.cs @@ -1,36 +1,36 @@ -using CC.ElectronicCommerce.Model; -using Microsoft.AspNetCore.Authentication; -using Microsoft.AspNetCore.Http; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Security.Claims; -using System.Text; -using System.Threading.Tasks; +//using Yi.Framework.Model; +//using Microsoft.AspNetCore.Authentication; +//using Microsoft.AspNetCore.Http; +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Security.Claims; +//using System.Text; +//using System.Threading.Tasks; -namespace CC.ElectronicCommerce.WebCore -{ - public static class CommonExtend - { - public static bool IsAjaxRequest(this HttpRequest request) - { - string header = request.Headers["X-Requested-With"]; - return "XMLHttpRequest".Equals(header); - } +//namespace CC.ElectronicCommerce.WebCore +//{ +// public static class CommonExtend +// { +// public static bool IsAjaxRequest(this HttpRequest request) +// { +// string header = request.Headers["X-Requested-With"]; +// return "XMLHttpRequest".Equals(header); +// } - /// - /// 基于HttpContext,当前鉴权方式解析,获取用户信息 - /// - /// - /// - public static UserInfo GetCurrentUserInfo(this HttpContext httpContext) - { - IEnumerable claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims; - return new UserInfo() - { - id = long.Parse(claimlist.FirstOrDefault(u => u.Type == "id").Value), - username = claimlist.FirstOrDefault(u => u.Type == "username").Value ?? "匿名" - }; - } - } -} +// /// +// /// 基于HttpContext,当前鉴权方式解析,获取用户信息 +// /// +// /// +// /// +// public static UserInfo GetCurrentUserInfo(this HttpContext httpContext) +// { +// IEnumerable claimlist = httpContext.AuthenticateAsync().Result.Principal.Claims; +// return new UserInfo() +// { +// id = long.Parse(claimlist.FirstOrDefault(u => u.Type == "id").Value), +// username = claimlist.FirstOrDefault(u => u.Type == "username").Value ?? "匿名" +// }; +// } +// } +//} diff --git a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomAction2CommitFilterAttribute.cs b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomAction2CommitFilterAttribute.cs index 247108b2..40a1d339 100644 --- a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomAction2CommitFilterAttribute.cs +++ b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomAction2CommitFilterAttribute.cs @@ -1,5 +1,5 @@ -using CC.ElectronicCommerce.Common.Models; -using CC.ElectronicCommerce.Core; +using Yi.Framework.Common.Models; +using Yi.Framework.Core; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; diff --git a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomExceptionFilterAttribute.cs b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomExceptionFilterAttribute.cs index 748d454a..785a6bd6 100644 --- a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomExceptionFilterAttribute.cs +++ b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/CustomExceptionFilterAttribute.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Newtonsoft.Json; -using CC.ElectronicCommerce.Common.Models; +using Yi.Framework.Common.Models; namespace CC.ElectronicCommerce.WebCore.FilterExtend { diff --git a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/LogActionFilterAttribute.cs b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/LogActionFilterAttribute.cs index c44e0186..d07922c8 100644 --- a/Yi.Framework/Yi.Framework.WebCore/FilterExtend/LogActionFilterAttribute.cs +++ b/Yi.Framework/Yi.Framework.WebCore/FilterExtend/LogActionFilterAttribute.cs @@ -1,4 +1,4 @@ -using CC.ElectronicCommerce.Common.Models; +using Yi.Framework.Common.Models; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Newtonsoft.Json; diff --git a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/StaticPageMiddleware.cs b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/StaticPageMiddleware.cs index 88bd72f7..c9340eef 100644 --- a/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/StaticPageMiddleware.cs +++ b/Yi.Framework/Yi.Framework.WebCore/MiddlewareExtend/StaticPageMiddleware.cs @@ -1,156 +1,156 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Http; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +//using Microsoft.AspNetCore.Builder; +//using Microsoft.AspNetCore.Http; +//using System; +//using System.Collections.Generic; +//using System.IO; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; -namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend -{ - /// - /// 支持在返回HTML时,将返回的Stream保存到指定目录 - /// - public class StaticPageMiddleware - { - private readonly RequestDelegate _next; - private string _directoryPath = @"D:/cc-ec/"; - private bool _supportDelete = false; - private bool _supportWarmup = false; +//namespace CC.ElectronicCommerce.WebCore.MiddlewareExtend +//{ +// /// +// /// 支持在返回HTML时,将返回的Stream保存到指定目录 +// /// +// public class StaticPageMiddleware +// { +// private readonly RequestDelegate _next; +// private string _directoryPath = @"D:/cc-ec/"; +// private bool _supportDelete = false; +// private bool _supportWarmup = false; - public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup) - { - this._next = next; - this._directoryPath = directoryPath; - this._supportDelete = supportDelete; - this._supportWarmup = supportWarmup; - } +// public StaticPageMiddleware(RequestDelegate next, string directoryPath, bool supportDelete, bool supportWarmup) +// { +// this._next = next; +// this._directoryPath = directoryPath; +// this._supportDelete = supportDelete; +// this._supportWarmup = supportWarmup; +// } - public async Task InvokeAsync(HttpContext context) - { - if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"])) - { - this.DeleteHmtl(context.Request.Path.Value); - context.Response.StatusCode = 200; - } - else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"])) - { - this.ClearDirectory(10);//考虑数据量 - context.Response.StatusCode = 200; - } - else if (!context.Request.IsAjaxRequest()) - { - Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}"); - #region context.Response.Body - var originalStream = context.Response.Body; - using (var copyStream = new MemoryStream()) - { - context.Response.Body = copyStream; - await _next(context); +// public async Task InvokeAsync(HttpContext context) +// { +// if (this._supportDelete && "Delete".Equals(context.Request.Query["ActionHeader"])) +// { +// this.DeleteHmtl(context.Request.Path.Value); +// context.Response.StatusCode = 200; +// } +// else if (this._supportWarmup && "ClearAll".Equals(context.Request.Query["ActionHeader"])) +// { +// this.ClearDirectory(10);//考虑数据量 +// context.Response.StatusCode = 200; +// } +// else if (!context.Request.IsAjaxRequest()) +// { +// Console.WriteLine($"This is StaticPageMiddleware InvokeAsync {context.Request.Path.Value}"); +// #region context.Response.Body +// var originalStream = context.Response.Body; +// using (var copyStream = new MemoryStream()) +// { +// context.Response.Body = copyStream; +// await _next(context); - copyStream.Position = 0; - var reader = new StreamReader(copyStream); - var content = await reader.ReadToEndAsync(); - string url = context.Request.Path.Value; +// copyStream.Position = 0; +// var reader = new StreamReader(copyStream); +// var content = await reader.ReadToEndAsync(); +// string url = context.Request.Path.Value; - this.SaveHmtl(url, content); +// this.SaveHmtl(url, content); - copyStream.Position = 0; - await copyStream.CopyToAsync(originalStream); - context.Response.Body = originalStream; - } - #endregion - } - else - { - await _next(context); - } - } +// copyStream.Position = 0; +// await copyStream.CopyToAsync(originalStream); +// context.Response.Body = originalStream; +// } +// #endregion +// } +// else +// { +// await _next(context); +// } +// } - private void SaveHmtl(string url, string html) - { - try - { - //Console.WriteLine($"Response: {html}"); - if (string.IsNullOrWhiteSpace(html)) - return; - if (!url.EndsWith(".html")) - return; +// private void SaveHmtl(string url, string html) +// { +// try +// { +// //Console.WriteLine($"Response: {html}"); +// if (string.IsNullOrWhiteSpace(html)) +// return; +// if (!url.EndsWith(".html")) +// return; - if (Directory.Exists(_directoryPath) == false) - Directory.CreateDirectory(_directoryPath); +// if (Directory.Exists(_directoryPath) == false) +// Directory.CreateDirectory(_directoryPath); - var totalPath = Path.Combine(_directoryPath, url.Split("/").Last()); - File.WriteAllText(totalPath, html);//直接覆盖 - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } +// var totalPath = Path.Combine(_directoryPath, url.Split("/").Last()); +// File.WriteAllText(totalPath, html);//直接覆盖 +// } +// catch (Exception ex) +// { +// Console.WriteLine(ex.Message); +// } +// } - /// - /// 删除某个页面 - /// - /// - /// - private void DeleteHmtl(string url) - { - try - { - if (!url.EndsWith(".html")) - return; - var totalPath = Path.Combine(_directoryPath, url.Split("/").Last()); - File.Delete(totalPath);//直接删除 - } - catch (Exception ex) - { - Console.WriteLine($"Delete {url} 异常,{ex.Message}"); - } - } +// /// +// /// 删除某个页面 +// /// +// /// +// /// +// private void DeleteHmtl(string url) +// { +// try +// { +// if (!url.EndsWith(".html")) +// return; +// var totalPath = Path.Combine(_directoryPath, url.Split("/").Last()); +// File.Delete(totalPath);//直接删除 +// } +// catch (Exception ex) +// { +// Console.WriteLine($"Delete {url} 异常,{ex.Message}"); +// } +// } - /// - /// 清理文件,支持重试 - /// - /// 最多重试次数 - private void ClearDirectory(int index) - { - if (index > 0)//简陋版---重试index次 - { - try - { - var files = Directory.GetFiles(_directoryPath); - foreach (var file in files) - { - File.Delete(file); - } - } - catch (Exception ex) - { - Console.WriteLine($"ClearDirectory failed {ex.Message}"); - ClearDirectory(index--); - } - } - } - } +// /// +// /// 清理文件,支持重试 +// /// +// /// 最多重试次数 +// private void ClearDirectory(int index) +// { +// if (index > 0)//简陋版---重试index次 +// { +// try +// { +// var files = Directory.GetFiles(_directoryPath); +// foreach (var file in files) +// { +// File.Delete(file); +// } +// } +// catch (Exception ex) +// { +// Console.WriteLine($"ClearDirectory failed {ex.Message}"); +// ClearDirectory(index--); +// } +// } +// } +// } - /// - /// 扩展中间件 - /// - public static class StaticPageMiddlewareExtensions - { - /// - /// - /// - /// - /// 文件写入地址,文件夹目录 - /// 是否支持删除 - /// 是否支持全量删除 - /// - public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear) - { - return app.UseMiddleware(directoryPath, supportDelete, supportClear); - } - } -} \ No newline at end of file +// /// +// /// 扩展中间件 +// /// +// public static class StaticPageMiddlewareExtensions +// { +// /// +// /// +// /// +// /// +// /// 文件写入地址,文件夹目录 +// /// 是否支持删除 +// /// 是否支持全量删除 +// /// +// public static IApplicationBuilder UseStaticPageMiddleware(this IApplicationBuilder app, string directoryPath, bool supportDelete, bool supportClear) +// { +// return app.UseMiddleware(directoryPath, supportDelete, supportClear); +// } +// } +//} \ No newline at end of file diff --git a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj index 60150096..6a3ce067 100644 --- a/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj +++ b/Yi.Framework/Yi.Framework.WebCore/Yi.Framework.WebCore.csproj @@ -5,7 +5,14 @@ + + + + + + +