├── .gitattributes
├── .gitignore
├── Colder.Components.sln
├── Directory.Build.props
├── LICENSE
├── README.md
├── demos
├── Demo.AspnetCore
│ ├── Controllers
│ │ ├── HomeController.cs
│ │ └── UserController.cs
│ ├── Demo.AspnetCore.csproj
│ ├── Dtos
│ │ ├── UserDto.cs
│ │ ├── UserExtendDto.cs
│ │ └── UserSearchDto.cs
│ ├── Entities
│ │ ├── DemoDbContext.cs
│ │ └── User.cs
│ ├── MapperProdiles
│ │ └── CommonProfile.cs
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── appsettings.json
│ └── db.db
├── Demo.Common
│ ├── Demo.Common.csproj
│ ├── MessageBusEndpoints.cs
│ ├── RequestMessage.cs
│ ├── ResponseMessage.cs
│ └── SubRequestMessage.cs
├── Demo.DistributedId
│ ├── Demo.DistributedId.csproj
│ └── Program.cs
├── Demo.DistributedLock
│ ├── Demo.DistributedLock.csproj
│ └── Program.cs
├── Demo.Logging
│ ├── Controllers
│ │ └── HomeController.cs
│ ├── Demo.Logging.csproj
│ ├── Program.cs
│ ├── Properties
│ │ ├── PublishProfiles
│ │ │ └── FolderProfile.pubxml
│ │ └── launchSettings.json
│ ├── Startup.cs
│ └── appsettings.json
├── Demo.MessageBus
│ ├── Controllers
│ │ └── HomeController.cs
│ ├── Demo.MessageBus.csproj
│ ├── Handler.cs
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── PublishService.cs
│ ├── Startup.cs
│ └── appsettings.json
├── Demo.OpenService.Client
│ ├── Demo.OpenService.Client.csproj
│ └── Program.cs
├── Demo.OpenService.Common
│ ├── Demo.OpenService.Common.csproj
│ └── IHelloOpenService.cs
├── Demo.OpenService.Server
│ ├── Controllers
│ │ └── HelloService.cs
│ ├── Demo.OpenService.Server.csproj
│ ├── Program.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── Startup.cs
│ └── appsettings.json
├── Demo.Orleans
│ ├── Bootstrapper.cs
│ ├── Demo.Orleans.csproj
│ ├── Grains
│ │ └── HelloGrain.cs
│ ├── IGrains
│ │ └── IHello.cs
│ ├── Program.cs
│ ├── Properties
│ │ └── PublishProfiles
│ │ │ └── FolderProfile.pubxml
│ └── appsettings.json
├── Demo.WebSockets.Server
│ ├── Controllers
│ │ └── HomeController.cs
│ ├── Demo.WebSockets.Server.csproj
│ ├── Program.cs
│ ├── Properties
│ │ ├── PublishProfiles
│ │ │ └── FolderProfile.pubxml
│ │ └── launchSettings.json
│ ├── Startup.cs
│ ├── appsettings.Development.json
│ ├── appsettings.json
│ └── wwwroot
│ │ └── index.html
└── Domo.Console
│ ├── Domo.Console.csproj
│ └── Program.cs
├── src
├── Colder.Api.Abstractions
│ ├── ApiExtentions.cs
│ ├── Colder.Api.Abstractions.csproj
│ ├── Controllers
│ │ ├── ApiControllerBase.cs
│ │ ├── CRUDControllerBase.cs
│ │ └── SaveContext.cs
│ ├── Filters
│ │ ├── ExceptionFilter.cs
│ │ └── TransactionFilter.cs
│ ├── JwtExtentions.cs
│ ├── Middlewares
│ │ ├── RealIpMiddleware.cs
│ │ ├── RequestInfo.cs
│ │ ├── RequestInfoMiddleware.cs
│ │ └── RequestLogMiddleware.cs
│ ├── Options
│ │ ├── ApiOptions.cs
│ │ └── SwaggerDocumentOptions.cs
│ └── Swagger
│ │ ├── ControllerFilterProcessor.cs
│ │ └── EnumSchemaProcessor.cs
├── Colder.AutoMapper
│ ├── Colder.AutoMapper.csproj
│ └── MappingExpressionExtensions.cs
├── Colder.Cache
│ ├── CacheExtentions.cs
│ ├── Colder.Cache.csproj
│ └── Options
│ │ ├── CacheOptions.cs
│ │ └── CacheTypes.cs
├── Colder.Common.Util
│ ├── Colder.Common.Util.csproj
│ ├── ElasticClientExtentions.cs
│ ├── Helpers
│ │ ├── FileZipHelper.cs
│ │ ├── JobHelper.cs
│ │ ├── LinqHelper.cs
│ │ ├── NPOIHelper.cs
│ │ ├── ProxyHelper.cs
│ │ └── QRCodeHelper.cs
│ └── SDKs
│ │ └── MiraiClient.cs
├── Colder.Common
│ ├── Colder.Common.csproj
│ ├── Extentions
│ │ ├── ByteExtentions.cs
│ │ ├── DataTableExtentions.cs
│ │ ├── DateTimeExtentions.cs
│ │ ├── DeepCloneExtensions.cs
│ │ ├── EnumExtentions.cs
│ │ ├── ExpressionExtentions.cs
│ │ ├── HttpClientExtentions.cs
│ │ ├── IEnumerableExtentions.cs
│ │ ├── IQueryableExtentions.cs
│ │ ├── ObjectExtentions.cs
│ │ ├── ReflectionExtentions.cs
│ │ ├── StreamExtentions.cs
│ │ ├── StringExtentions.cs
│ │ └── TypeExtentions.cs
│ ├── Helpers
│ │ ├── AsyncHelper.cs
│ │ ├── EncodingHelper.cs
│ │ ├── EnumHelper.cs
│ │ ├── ImgHelper.cs
│ │ ├── ImgVerifyCodeHelper.cs
│ │ ├── IpHelper.cs
│ │ ├── RSAHelper.cs
│ │ ├── RandomHelper.cs
│ │ ├── TimestampHelper.cs
│ │ ├── TrackingHelper.cs
│ │ ├── TreeHelper.cs
│ │ └── TypeBuilderHelper.cs
│ ├── Pipeline.cs
│ └── Primitives
│ │ ├── ApiResult.cs
│ │ ├── IdInput.cs
│ │ ├── MemoryMessageHandlerBase.cs
│ │ ├── MsgException.cs
│ │ ├── PageInput.cs
│ │ └── PageList.cs
├── Colder.Dependency
│ ├── AOP
│ │ ├── BaseAOPAttribute.cs
│ │ ├── CastleAOPContext.cs
│ │ ├── CastleInterceptor.cs
│ │ └── IAOPContext.cs
│ ├── Colder.Dependency.csproj
│ ├── DependencyExtentions.cs
│ ├── IScopedDependency.cs
│ ├── ISingletonDependency.cs
│ └── ITransientDependency.cs
├── Colder.DistributedId
│ ├── AsyncHelper.cs
│ ├── Colder.DistributedId.csproj
│ ├── DistributedId.cs
│ ├── DistributedIdExtentions.cs
│ ├── DistributedIdOptions.cs
│ ├── Guid
│ │ └── GuidHelper.cs
│ ├── IDistributedId.cs
│ └── Snowflake
│ │ ├── DisposableAction.cs
│ │ ├── IdWorker.cs
│ │ ├── InvalidSystemClock.cs
│ │ └── TimeExtensions.cs
├── Colder.DistributedLock.Abstractions
│ ├── Colder.DistributedLock.Abstractions.csproj
│ ├── DistributedLockOptions.cs
│ ├── IDistributedLock.cs
│ └── LockTypes.cs
├── Colder.DistributedLock.Hosting
│ ├── Colder.DistributedLock.Hosting.csproj
│ └── DistributedLockExtentions.cs
├── Colder.DistributedLock.InMemory
│ ├── Colder.DistributedLock.InMemory.csproj
│ └── InMemoryDistributedLock.cs
├── Colder.DistributedLock.Redis
│ ├── Colder.DistributedLock.Redis.csproj
│ └── RedisDistributedLock.cs
├── Colder.Domain.Abstractions
│ ├── Colder.Domain.Abstractions.csproj
│ ├── IAggregateRoot.cs
│ ├── IRepositoryBase.cs
│ └── IStatefulAggregateRoot.cs
├── Colder.EFCore
│ ├── Colder.EFCore.csproj
│ └── EFCoreExtentions.cs
├── Colder.Extentions
│ ├── AOPTransactionExtentions.cs
│ ├── Colder.Extentions.csproj
│ └── TransactionalAttribute.cs
├── Colder.ImageSharp
│ ├── Colder.ImageSharp.csproj
│ └── ImageSharpHelper.cs
├── Colder.Infrastructure.Abstractions
│ ├── Colder.Infrastructure.Abstractions.csproj
│ └── DbRepositoryBase.cs
├── Colder.Json
│ ├── Colder.Json.csproj
│ ├── Converters
│ │ ├── LongToStringConverter.cs
│ │ └── TimestampConverter.cs
│ └── JsonExtensions.cs
├── Colder.Logging.Abstractions
│ ├── Colder.Logging.Abstractions.csproj
│ ├── ElasticsearchOption.cs
│ ├── EnableOption.cs
│ ├── FileOption.cs
│ ├── KafkaOption.cs
│ ├── LogOptions.cs
│ └── OverrideOption.cs
├── Colder.Logging.Serilog
│ ├── Bootstrapper.cs
│ ├── Colder.Logging.Serilog.csproj
│ └── SerilogExtentions.cs
├── Colder.Mail
│ ├── Colder.Mail.csproj
│ └── IdleClient.cs
├── Colder.MessageBus.Abstractions
│ ├── Colder.MessageBus.Abstractions.csproj
│ ├── IMessageBus.cs
│ ├── IMessageHandler.cs
│ ├── MessageContext.T.cs
│ ├── MessageContext.cs
│ └── Options
│ │ ├── Constant.cs
│ │ ├── MessageBusOptions.cs
│ │ └── TransportType.cs
├── Colder.MessageBus.Hosting
│ ├── AbstractProvider.cs
│ ├── Cache.cs
│ ├── Colder.MessageBus.Hosting.csproj
│ ├── DependencyInjection
│ │ ├── DependencyInjectionExtentions.cs
│ │ └── MessageBusBootstraper.cs
│ ├── MessageBusFactory.cs
│ └── Primitives
│ │ ├── HandlerClass.cs
│ │ └── HandlerMethod.cs
├── Colder.MessageBus.InMemory
│ ├── Colder.MessageBus.InMemory.csproj
│ ├── InMemoryProvider.cs
│ ├── MassTransitMessageBus.cs
│ ├── MassTransitProvider.cs
│ └── ProxyHandler.cs
├── Colder.MessageBus.MQTT
│ ├── Colder.MessageBus.MQTT.csproj
│ ├── MQTTProvider.cs
│ ├── MassTransit
│ │ ├── MqttMessageReceivedEvent.cs
│ │ └── MqttMessageReceivedEventHandler.cs
│ ├── MqttMessageBus.cs
│ ├── Primitives
│ │ ├── MessageTypes.cs
│ │ ├── Topic.cs
│ │ └── Waiter.cs
│ └── RequestWaiter.cs
├── Colder.MessageBus.RabbitMQ
│ ├── Colder.MessageBus.RabbitMQ.csproj
│ └── RabbitMQProvider.cs
├── Colder.MessageBus.Redis
│ ├── Colder.MessageBus.Redis.csproj
│ ├── Primitives
│ │ ├── MessageTypes.cs
│ │ ├── Topic.cs
│ │ └── Waiter.cs
│ ├── RedisMessageBus.cs
│ └── RedisProvider.cs
├── Colder.OpenService.Abstractions
│ ├── Attributes
│ │ └── RouteAttribute.cs
│ ├── Colder.OpenService.Abstractions.csproj
│ ├── ICRUDOpenService.cs
│ ├── IOpenService.cs
│ └── Util
│ │ ├── Extentions.cs
│ │ └── Helper.cs
├── Colder.OpenService.Client
│ ├── Colder.OpenService.Client.csproj
│ ├── OpenServiceClientExtentions.cs
│ ├── OpenServiceClientFactory.cs
│ └── OpenServiceOptions.cs
├── Colder.OpenService.Hosting
│ ├── Colder.OpenService.Hosting.csproj
│ ├── Constant.cs
│ ├── DependencyInjection
│ │ └── OpenServiceHostDependencyInjectionExtentions.cs
│ ├── Filters
│ │ └── RPCResultFilter.cs
│ ├── OpenServiceBase.cs
│ ├── Properties
│ │ └── launchSettings.json
│ ├── WebApiRPCConvention.cs
│ └── WebApiRPCProvider.cs
├── Colder.Orleans.Hosting
│ ├── Colder.Orleans.Hosting.csproj
│ ├── Options
│ │ ├── OrleansOptions.cs
│ │ └── ProviderTypes.cs
│ └── OrleansExtentions.cs
├── Colder.Pdf
│ ├── Colder.Pdf.csproj
│ └── PdfHelper.cs
├── Colder.WebSockets.Abstractions
│ ├── Colder.WebSockets.Abstractions.csproj
│ ├── IWebSocketConnection.cs
│ ├── IWebSocketServer.cs
│ └── WebSocketServerOptions.cs
└── Colder.WebSockets.Server
│ ├── Colder.WebSockets.Server.csproj
│ ├── MessageReceivedEvent.cs
│ ├── WebSocketConnection.cs
│ ├── WebSocketServer.cs
│ └── WebSocketServerExtentions.cs
├── tests
└── Colder.Tests
│ ├── CacheTest.cs
│ ├── Colder.Tests.csproj
│ ├── HelperTest.cs
│ ├── InMemoryDistributedLockTest.cs
│ └── RedisDistributedLockTest.cs
└── version.json
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | latestMajor
5 |
6 | true
7 | https://github.com/Coldairarrow/Colder.Components
8 | Coldairarrow
9 | Coldairarrow
10 | net6.0
11 |
12 |
13 |
14 |
15 | all
16 | 3.5.119
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Colder.DistributedId;
3 | using Microsoft.AspNetCore.Mvc;
4 |
5 | namespace Demo.AspnetCore.Controllers
6 | {
7 | [ApiController]
8 | [Route("[controller]")]
9 | [ApiExplorerSettings(GroupName = "default")]
10 | public class HomeController : ControllerBase
11 | {
12 | private readonly IDistributedId _distributedId;
13 | public HomeController(IDistributedId distributedId)
14 | {
15 | _distributedId = distributedId;
16 | }
17 |
18 | [HttpPost]
19 | [Route("test")]
20 | //[AllowAnonymous]
21 | public string Test(IdInput id)
22 | {
23 | return _distributedId.NewLongId().ToString();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Controllers/UserController.cs:
--------------------------------------------------------------------------------
1 | using Colder.Api.Abstractions.Controllers;
2 | using Demo.AspnetCore.Dtos;
3 | using Demo.AspnetCore.Entities;
4 | using Microsoft.AspNetCore.Authorization;
5 | using Microsoft.AspNetCore.Mvc;
6 | using NSwag.Annotations;
7 | using System;
8 |
9 | namespace Demo.AspnetCore.Controllers;
10 |
11 | [AllowAnonymous]
12 | [ApiExplorerSettings(GroupName = "group1")]
13 | public class UserController : CRUDControllerBase
14 | {
15 | public UserController(IServiceProvider serviceProvider) : base(serviceProvider)
16 | {
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Demo.AspnetCore.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net8.0
5 | CS1591;
6 | disable
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | PreserveNewest
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Dtos/UserDto.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.AspnetCore.Dtos;
2 |
3 | public class UserDto
4 | {
5 | public long Id { get; set; }
6 |
7 | public string Name { get; set; }
8 | }
9 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Dtos/UserExtendDto.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.AspnetCore.Dtos;
2 |
3 | public class UserExtendDto : UserDto
4 | {
5 | }
6 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Dtos/UserSearchDto.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.AspnetCore.Dtos;
2 |
3 | public class UserSearchDto
4 | {
5 | public string Name { get; set; }
6 | }
7 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Entities/DemoDbContext.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.EntityFrameworkCore;
2 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
3 | using System;
4 |
5 | namespace Demo.AspnetCore.Entities;
6 |
7 | public class DemoDbContext : DbContext
8 | {
9 | static DemoDbContext()
10 | {
11 | //开启兼容模式 https://www.npgsql.org/efcore/release-notes/6.0.html?tabs=annotations
12 | AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
13 | }
14 | private static readonly ValueConverter _dateTimeConverter
15 | = new ValueConverter(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Local));
16 | public DemoDbContext(DbContextOptions options) : base(options)
17 | {
18 | }
19 |
20 | public DbSet Users { get; set; }
21 |
22 | protected override void OnModelCreating(ModelBuilder modelBuilder)
23 | {
24 | base.OnModelCreating(modelBuilder);
25 | //DateTime默认为Local
26 | foreach (var entityType in modelBuilder.Model.GetEntityTypes())
27 | {
28 | foreach (var property in entityType.GetProperties())
29 | {
30 | if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?))
31 | property.SetValueConverter(_dateTimeConverter);
32 | }
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Entities/User.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 | using System.ComponentModel.DataAnnotations.Schema;
3 |
4 | namespace Demo.AspnetCore.Entities
5 | {
6 | [Table(nameof(User))]
7 | public class User
8 | {
9 | [Key]
10 | public long Id { get; set; }
11 |
12 | public string Name { get; set; }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/MapperProdiles/CommonProfile.cs:
--------------------------------------------------------------------------------
1 | using AutoMapper;
2 | using Demo.AspnetCore.Dtos;
3 | using Demo.AspnetCore.Entities;
4 |
5 | namespace Invest.Api.MapperProdiles;
6 |
7 | public class CommonProfile : Profile
8 | {
9 | public CommonProfile()
10 | {
11 | CreateMap().ReverseMap();
12 | CreateMap().ReverseMap();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Api.Abstractions;
2 | using Demo.AspnetCore.Entities;
3 | using Microsoft.AspNetCore.Builder;
4 | using Microsoft.EntityFrameworkCore;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using System.Reflection;
7 |
8 | WebApplication.CreateBuilder(args).RunWebApiDefaults(services =>
9 | {
10 | services.AddDbContext(x => x.UseSqlite("Data Source=db.db"));
11 | services.AddAutoMapper(Assembly.GetExecutingAssembly());
12 | });
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "http://json.schemastore.org/launchsettings.json",
3 | "profiles": {
4 | "Demo.AspnetCore": {
5 | "commandName": "Project",
6 | "environmentVariables": {
7 | "ASPNETCORE_ENVIRONMENT": "Development"
8 | }
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "AllowedHosts": "*",
3 | "log": { //日志配置
4 | "Instance": "demo",
5 | "minlevel": "Trace", //定义详见Microsoft.Extensions.Logging.LogLevel
6 | "console": {
7 | "enabled": true
8 | },
9 | "debug": {
10 | "enabled": true
11 | },
12 | "file": {
13 | "enabled": true
14 | },
15 | "elasticsearch": {
16 | "enabled": false,
17 | "nodes": [ "http://localhost:9200/" ],
18 | "indexformat": "log-{0:yyyyMMdd}"
19 | },
20 | "kafka": {
21 | "enabled": false,
22 | "brokers": "192.168.56.201:9092",
23 | "userName": "user",
24 | "password": "bitnami",
25 | "topic": "log"
26 | },
27 | "overrides": [ //重写日志输出级别
28 | {
29 | "source": "Microsoft.AspNetCore",
30 | "minlevel": "Information"
31 | }
32 | ]
33 | },
34 | "api": {
35 | "EnableSwagger": true,
36 | "EnableJwt": false,
37 | "JwtSecret": null,
38 | "Documents": [
39 | {
40 | "Title": "default",
41 | "DocumentName": "default",
42 | "ApiGroupNames": [ "default" ]
43 | },
44 | {
45 | "Title": "group1",
46 | "DocumentName": "group1",
47 | "ApiGroupNames": [ "group1" ]
48 | }
49 | ]
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/demos/Demo.AspnetCore/db.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coldairarrow/Colder.Components/b294acc480ed8314299e61cf8a2950d9ab3c42ba/demos/Demo.AspnetCore/db.db
--------------------------------------------------------------------------------
/demos/Demo.Common/Demo.Common.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 |
6 | false
7 | 1701;1702;CS1591
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/demos/Demo.Common/MessageBusEndpoints.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.Common
2 | {
3 | public static class MessageBusEndpoints
4 | {
5 | public static readonly string Consumer = "Consumer";
6 | public static readonly string Producer = "Producer";
7 | public static readonly string Gateway = "Gateway";
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/demos/Demo.Common/RequestMessage.cs:
--------------------------------------------------------------------------------
1 | using Colder.MessageBus.Abstractions;
2 |
3 | namespace Demo.Common
4 | {
5 | public class RequestMessage
6 | {
7 | public string Text { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/demos/Demo.Common/ResponseMessage.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.Common
2 | {
3 | public class ResponseMessage
4 | {
5 | public string Text { get; set; }
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/demos/Demo.Common/SubRequestMessage.cs:
--------------------------------------------------------------------------------
1 | namespace Demo.Common
2 | {
3 | public class SubRequestMessage : RequestMessage
4 | {
5 |
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/demos/Demo.DistributedId/Demo.DistributedId.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/demos/Demo.DistributedId/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Cache;
2 | using Colder.DistributedId;
3 | using Colder.DistributedLock.Abstractions;
4 | using Colder.DistributedLock.Hosting;
5 | using Microsoft.Extensions.DependencyInjection;
6 |
7 | namespace Demo.DistributedId
8 | {
9 | class Program
10 | {
11 | static async Task Main(string[] args)
12 | {
13 | await Task.CompletedTask;
14 |
15 | IServiceCollection services = new ServiceCollection();
16 | services.AddDistributedLock(x =>
17 | {
18 | x.LockType = LockTypes.InMemory;
19 | x.RedisEndPoints = new string[] { "localhost:6379" };
20 | });
21 | services.AddCache(new CacheOptions
22 | {
23 | CacheType = CacheTypes.Redis,
24 | RedisConnectionString = "localhost:6379"
25 | });
26 | services.AddDistributedId(new DistributedIdOptions
27 | {
28 | Distributed = true
29 | });
30 |
31 | var serviceProvider = services.BuildServiceProvider();
32 | var distributedId = serviceProvider.GetRequiredService();
33 | var ids = Enumerable.Range(0, 10000).Select(x => distributedId.NewLongId()).ToList();
34 |
35 | Console.ReadLine();
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/demos/Demo.DistributedLock/Demo.DistributedLock.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/demos/Demo.DistributedLock/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.DistributedLock.Abstractions;
2 | using Colder.DistributedLock.Hosting;
3 | using Microsoft.Extensions.Caching.Distributed;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using System;
6 | using System.Collections.Generic;
7 | using System.Threading.Tasks;
8 |
9 | namespace Demo.DistributedLock
10 | {
11 | class Program
12 | {
13 | static async Task Main(string[] args)
14 | {
15 | IServiceCollection services = new ServiceCollection();
16 | services.AddDistributedLock(x =>
17 | {
18 | x.LockType = LockTypes.InMemory;
19 | });
20 |
21 | var serviceProvider = services.BuildServiceProvider();
22 |
23 | var theLock = serviceProvider.GetService();
24 | string key = Guid.NewGuid().ToString();
25 |
26 | List tasks = new List();
27 |
28 | int num = 0;
29 |
30 | for (int i = 0; i < 4; i++)
31 | {
32 | tasks.Add(Task.Run(async () =>
33 | {
34 | for (int j = 0; j < 10000; j++)
35 | {
36 | using var _ = await theLock.Lock(key);
37 | num++;
38 | Console.WriteLine(num);
39 | }
40 | }));
41 | }
42 |
43 | await Task.WhenAll(tasks);
44 |
45 | Console.ReadLine();
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc;
2 | using Microsoft.Extensions.Logging;
3 |
4 | namespace Demo.Logging.Controllers
5 | {
6 | [ApiController]
7 | [Route("[controller]/[action]")]
8 | public class HomeController : ControllerBase
9 | {
10 | private readonly ILogger _logger;
11 | public HomeController(ILogger logger)
12 | {
13 | _logger = logger;
14 | }
15 |
16 | [HttpGet]
17 | public string Index()
18 | {
19 | return "OK";
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/Demo.Logging.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(DemoNetVersion)
5 |
6 | false
7 | 1701;1702;CS1591
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Logging.Serilog;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Hosting;
4 |
5 | namespace Logging.Demo
6 | {
7 | public class Program
8 | {
9 | public static void Main(string[] args)
10 | {
11 | CreateHostBuilder(args).Build().Run();
12 | }
13 |
14 | public static IHostBuilder CreateHostBuilder(string[] args) =>
15 | Host.CreateDefaultBuilder(args)
16 | .ConfigureLoggingDefaults()
17 | .ConfigureWebHostDefaults(webBuilder =>
18 | {
19 | webBuilder.UseStartup();
20 | });
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/Properties/PublishProfiles/FolderProfile.pubxml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | False
8 | False
9 | True
10 | Release
11 | Any CPU
12 | FileSystem
13 | bin\Release\netcoreapp3.1\publish\
14 | FileSystem
15 |
16 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Logging.Demo": {
4 | "commandName": "Project",
5 | "launchUrl": "Home/Index",
6 | "environmentVariables": {
7 | "ASPNETCORE_ENVIRONMENT": "Development"
8 | },
9 | "applicationUrl": "http://localhost:5000"
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/demos/Demo.Logging/Startup.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Builder;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Configuration;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using Microsoft.Extensions.Hosting;
6 |
7 | namespace Logging.Demo
8 | {
9 | public class Startup
10 | {
11 | public Startup(IConfiguration configuration)
12 | {
13 | Configuration = configuration;
14 | }
15 |
16 | public IConfiguration Configuration { get; }
17 |
18 | // This method gets called by the runtime. Use this method to add services to the container.
19 | public void ConfigureServices(IServiceCollection services)
20 | {
21 | services.AddControllers();
22 | }
23 |
24 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
25 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
26 | {
27 | if (env.IsDevelopment())
28 | {
29 | app.UseDeveloperExceptionPage();
30 | }
31 |
32 | app.UseRouting();
33 |
34 | app.UseAuthorization();
35 |
36 | app.UseEndpoints(endpoints =>
37 | {
38 | endpoints.MapControllers();
39 | });
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/demos/Demo.Logging/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "AllowedHosts": "*",
3 | "log": { //日志配置
4 | "Instance": "demo",
5 | "minlevel": "Trace", //定义详见Microsoft.Extensions.Logging.LogLevel
6 | "console": {
7 | "enabled": true
8 | },
9 | "debug": {
10 | "enabled": true
11 | },
12 | "file": {
13 | "enabled": true
14 | },
15 | "elasticsearch": {
16 | "enabled": false,
17 | "nodes": [ "http://localhost:9200/" ],
18 | "indexformat": "log-{0:yyyyMMdd}"
19 | },
20 | "kafka": {
21 | "enabled": true,
22 | "brokers": "192.168.56.201:9092",
23 | "userName": "user",
24 | "password": "bitnami",
25 | "topic": "log"
26 | },
27 | "overrides": [ //重写日志输出级别
28 | {
29 | "source": "Microsoft.AspNetCore",
30 | "minlevel": "Information"
31 | }
32 | ]
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using Colder.MessageBus.Abstractions;
2 | using Demo.Common;
3 | using Microsoft.AspNetCore.Mvc;
4 | using Microsoft.Extensions.Logging;
5 | using System.Threading.Tasks;
6 |
7 | namespace Demo.Logging.Controllers
8 | {
9 | [ApiController]
10 | [Route("[controller]/[action]")]
11 | public class HomeController : ControllerBase
12 | {
13 | private readonly ILogger _logger;
14 | private readonly IMessageBus _messageBus;
15 | public HomeController(ILogger logger, IMessageBus messageBus)
16 | {
17 | _logger = logger;
18 | _messageBus = messageBus;
19 | }
20 |
21 | [HttpGet]
22 | public async Task Index()
23 | {
24 | await _messageBus.Publish(new RequestMessage
25 | {
26 | Text = "小明"
27 | });
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Demo.MessageBus.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | $(DemoNetVersion)
6 |
7 | false
8 | 1701;1702;CS1591
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | PreserveNewest
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Handler.cs:
--------------------------------------------------------------------------------
1 | using Colder.MessageBus.Abstractions;
2 | using Demo.Common;
3 | using Microsoft.Extensions.Logging;
4 | using Newtonsoft.Json;
5 | using System.Threading.Tasks;
6 |
7 | namespace Demo.MessageBus.Consumer
8 | {
9 | class Handler : IMessageHandler
10 | {
11 | private readonly ILogger _logger;
12 | private readonly IMessageBus _messageBus;
13 | public Handler(ILogger logger, IMessageBus messageBus)
14 | {
15 | _logger = logger;
16 | _messageBus = messageBus;
17 | }
18 | public async Task Handle(MessageContext context)
19 | {
20 | _logger.LogInformation("收到 {EventType} 事件,MessageBody:{MessageBody}",
21 | typeof(RequestMessage).Name, JsonConvert.SerializeObject(context.Message));
22 |
23 | await Task.CompletedTask;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Logging.Serilog;
2 | using Colder.MessageBus.Hosting;
3 | using Logging.Demo;
4 | using Microsoft.AspNetCore.Hosting;
5 | using Microsoft.Extensions.DependencyInjection;
6 | using Microsoft.Extensions.Hosting;
7 | using System.Threading.Tasks;
8 |
9 | namespace Demo.MessageBus.Consumer
10 | {
11 | class Program
12 | {
13 | public static async Task Main()
14 | {
15 | await Host.CreateDefaultBuilder()
16 | .ConfigureLoggingDefaults()
17 | .ConfigureMessageBusDefaults()
18 | .ConfigureServices(services =>
19 | {
20 | services.AddHostedService();
21 | })
22 | .ConfigureWebHostDefaults(webBuilder =>
23 | {
24 | webBuilder.UseStartup();
25 | })
26 | .RunConsoleAsync();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Demo.MessageBus": {
4 | "commandName": "Project",
5 | "environmentVariables": {
6 | "ASPNETCORE_ENVIRONMENT": "Development"
7 | }
8 | }
9 | }
10 | }
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/PublishService.cs:
--------------------------------------------------------------------------------
1 | using Colder.MessageBus.Abstractions;
2 | using Demo.Common;
3 | using Microsoft.Extensions.Hosting;
4 | using System.Threading;
5 | using System.Threading.Tasks;
6 |
7 | namespace Demo.MessageBus
8 | {
9 | class PublishService : BackgroundService
10 | {
11 | private readonly IMessageBus _messageBus;
12 | private Timer _timer;
13 | public PublishService(IMessageBus messageBus)
14 | {
15 | _messageBus = messageBus;
16 | }
17 | protected override async Task ExecuteAsync(CancellationToken stoppingToken)
18 | {
19 | await Task.CompletedTask;
20 |
21 | _timer = new Timer(async state =>
22 | {
23 | await _messageBus.Publish(new RequestMessage
24 | {
25 | Text = "小明"
26 | });
27 | }, null, 0, 1000);
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/Startup.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Builder;
2 | using Microsoft.AspNetCore.Hosting;
3 | using Microsoft.Extensions.Configuration;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using Microsoft.Extensions.Hosting;
6 |
7 | namespace Logging.Demo
8 | {
9 | public class Startup
10 | {
11 | public Startup(IConfiguration configuration)
12 | {
13 | Configuration = configuration;
14 | }
15 |
16 | public IConfiguration Configuration { get; }
17 |
18 | // This method gets called by the runtime. Use this method to add services to the container.
19 | public void ConfigureServices(IServiceCollection services)
20 | {
21 | services.AddControllers();
22 | }
23 |
24 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
25 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
26 | {
27 | if (env.IsDevelopment())
28 | {
29 | app.UseDeveloperExceptionPage();
30 | }
31 |
32 | app.UseRouting();
33 |
34 | app.UseAuthorization();
35 |
36 | app.UseEndpoints(endpoints =>
37 | {
38 | endpoints.MapControllers();
39 | });
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/demos/Demo.MessageBus/appsettings.json:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coldairarrow/Colder.Components/b294acc480ed8314299e61cf8a2950d9ab3c42ba/demos/Demo.MessageBus/appsettings.json
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Client/Demo.OpenService.Client.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | $(DemoNetVersion)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Client/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Colder.OpenService.Client;
3 | using Demo.OpenService.Common;
4 | using Microsoft.Extensions.DependencyInjection;
5 | using System;
6 | using System.Threading.Tasks;
7 |
8 | namespace Demo.OpenService.Client
9 | {
10 | internal class Program
11 | {
12 | static async Task Main(string[] args)
13 | {
14 | IServiceCollection services = new ServiceCollection();
15 | services.AddOpenServiceClient(typeof(IHelloOpenService).Assembly, new OpenServiceOptions
16 | {
17 | BaseUrl = "http://localhost:5000/api/"
18 | });
19 | IServiceProvider serviceProvider = services.BuildServiceProvider();
20 | IHelloOpenService helloOpenService = serviceProvider.GetService();
21 | var response = await helloOpenService.SayHello(new IdInput { Id = "Hello World" });
22 | Console.WriteLine($"请求成功 返回参:{response}");
23 |
24 | Console.ReadLine();
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Common/Demo.OpenService.Common.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(DemoNetVersion)
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Common/IHelloOpenService.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Colder.OpenService.Abstractions;
3 | using System.Threading.Tasks;
4 |
5 | namespace Demo.OpenService.Common
6 | {
7 | ///
8 | ///
9 | ///
10 | [Route("hello")]
11 | public interface IHelloOpenService : IOpenService
12 | {
13 | ///
14 | ///
15 | ///
16 | ///
17 | ///
18 | [Route("say")]
19 | Task SayHello(IdInput idInput);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/Controllers/HelloService.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Colder.OpenService.Hosting;
3 | using Demo.OpenService.Common;
4 | using System.Threading.Tasks;
5 |
6 | namespace Demo.OpenService.Controllers
7 | {
8 | ///
9 | ///
10 | ///
11 | public class HelloService : OpenServiceBase, IHelloOpenService
12 | {
13 | ///
14 | ///
15 | ///
16 | ///
17 | ///
18 | public Task SayHello(IdInput idInput)
19 | {
20 | return Task.FromResult(idInput.Id);
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/Demo.OpenService.Server.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(DemoNetVersion)
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Hosting;
2 | using Microsoft.Extensions.Hosting;
3 | using Colder.OpenService.Client;
4 | using System.Reflection;
5 |
6 | namespace Demo.OpenService
7 | {
8 | internal class Program
9 | {
10 | public static void Main(string[] args)
11 | {
12 | CreateHostBuilder(args).Build().Run();
13 | }
14 |
15 | public static IHostBuilder CreateHostBuilder(string[] args) =>
16 | Host.CreateDefaultBuilder(args)
17 | .ConfigureServices((host, services) =>
18 | {
19 | services.AddOpenServiceClient(Assembly.GetEntryAssembly(), new OpenServiceOptions
20 | {
21 | BaseUrl="http://localhost:5000/api/"
22 | });
23 | })
24 | .ConfigureWebHostDefaults(webBuilder =>
25 | {
26 | webBuilder.UseStartup();
27 | });
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Demo.OpenService.Server": {
4 | "commandName": "Project",
5 | "dotnetRunMessages": "true",
6 | "launchBrowser": true,
7 | "launchUrl": "swagger",
8 | "applicationUrl": "http://localhost:5000",
9 | "environmentVariables": {
10 | "ASPNETCORE_ENVIRONMENT": "Development"
11 | }
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/Startup.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coldairarrow/Colder.Components/b294acc480ed8314299e61cf8a2950d9ab3c42ba/demos/Demo.OpenService.Server/Startup.cs
--------------------------------------------------------------------------------
/demos/Demo.OpenService.Server/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Information",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | },
9 | "AllowedHosts": "*"
10 | }
11 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/Bootstrapper.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Hosting;
2 | using Microsoft.Extensions.Logging;
3 | using Orleans;
4 | using System;
5 | using System.Threading;
6 | using System.Threading.Tasks;
7 |
8 | namespace Demo.Orleans
9 | {
10 | class Bootstrapper : BackgroundService
11 | {
12 | private readonly IGrainFactory _grainFactory;
13 | private Timer _timer;
14 | private readonly ILogger _logger;
15 | public Bootstrapper(IGrainFactory grainFactory, ILogger logger)
16 | {
17 | _grainFactory = grainFactory;
18 | _logger = logger;
19 | }
20 | protected override Task ExecuteAsync(CancellationToken stoppingToken)
21 | {
22 | Guid id = Guid.NewGuid();
23 | _timer = new Timer(async _ =>
24 | {
25 | try
26 | {
27 | await _grainFactory.GetGrain(0).Say("小明");
28 | }
29 | catch (Exception ex)
30 | {
31 | _logger.LogError(ex, ex.Message);
32 | }
33 | }, null, 0, 1000);
34 |
35 | return Task.CompletedTask;
36 | }
37 |
38 | public override async Task StopAsync(CancellationToken cancellationToken)
39 | {
40 | await _timer.DisposeAsync();
41 |
42 | await base.StopAsync(cancellationToken);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/Demo.Orleans.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | $(DemoNetVersion)
6 |
7 |
8 |
9 |
10 |
11 |
12 | all
13 | runtime; build; native; contentfiles; analyzers; buildtransitive
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | PreserveNewest
25 | true
26 | PreserveNewest
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/Grains/HelloGrain.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.Extensions.Logging;
2 | using Orleans;
3 | using System.Threading.Tasks;
4 |
5 | namespace Demo.Orleans
6 | {
7 | internal class HelloGrain : Grain, IHello
8 | {
9 | private readonly ILogger _logger;
10 | public HelloGrain(ILoggerFactory loggerFactory)
11 | {
12 | _logger = loggerFactory.CreateLogger(GetType());
13 | }
14 | public Task Say(string name)
15 | {
16 | _logger.LogInformation("{Name} Say Hello", name);
17 |
18 | return Task.CompletedTask;
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/IGrains/IHello.cs:
--------------------------------------------------------------------------------
1 | using Orleans;
2 | using System.Threading.Tasks;
3 |
4 | namespace Demo.Orleans
5 | {
6 | internal interface IHello : IGrainWithIntegerKey
7 | {
8 | Task Say(string name);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Logging.Serilog;
2 | using Colder.Orleans.Hosting;
3 | using Microsoft.Extensions.DependencyInjection;
4 | using Microsoft.Extensions.Hosting;
5 | using Orleans;
6 | using Orleans.Hosting;
7 | using System.Threading.Tasks;
8 |
9 | namespace Demo.Orleans
10 | {
11 | class Program
12 | {
13 | public static async Task Main(string[] args)
14 | {
15 | await Host.CreateDefaultBuilder(args)
16 | .ConfigureLoggingDefaults()
17 | .ConfigureOrleansDefaults()
18 | .ConfigureServices(services =>
19 | {
20 | services.AddHostedService();
21 | })
22 | .RunConsoleAsync();
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/Properties/PublishProfiles/FolderProfile.pubxml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | Release
8 | Any CPU
9 | bin\Release\netcoreapp3.1\publish\
10 | FileSystem
11 |
12 |
--------------------------------------------------------------------------------
/demos/Demo.Orleans/appsettings.json:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coldairarrow/Colder.Components/b294acc480ed8314299e61cf8a2950d9ab3c42ba/demos/Demo.Orleans/appsettings.json
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Controllers/HomeController.cs:
--------------------------------------------------------------------------------
1 | using Colder.WebSockets.Abstractions;
2 | using Microsoft.AspNetCore.Mvc;
3 | using System;
4 | using System.Threading.Tasks;
5 |
6 | namespace Demo.WebSockets.Server.Controllers
7 | {
8 | [ApiController]
9 | [Route("[controller]")]
10 | public class HomeController : ControllerBase
11 | {
12 | private readonly IWebSocketServer _webSocketServer;
13 | public HomeController(IWebSocketServer webSocketServer)
14 | {
15 | _webSocketServer = webSocketServer;
16 | }
17 |
18 | [HttpGet]
19 | [Route("/send")]
20 | public async Task Send()
21 | {
22 | foreach (var aConnection in _webSocketServer.GetAllConnections())
23 | {
24 | await aConnection.Send(DateTime.Now.ToString());
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Demo.WebSockets.Server.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(DemoNetVersion)
5 | CS1591;
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Program.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Hosting;
2 | using Microsoft.Extensions.Hosting;
3 |
4 | namespace Demo.WebSockets.Server
5 | {
6 | internal class Program
7 | {
8 | public static void Main(string[] args)
9 | {
10 | CreateHostBuilder(args).Build().Run();
11 | }
12 |
13 | public static IHostBuilder CreateHostBuilder(string[] args) =>
14 | Host.CreateDefaultBuilder(args)
15 | .ConfigureWebHostDefaults(webBuilder =>
16 | {
17 | webBuilder.UseStartup();
18 | });
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Properties/PublishProfiles/FolderProfile.pubxml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 | False
8 | False
9 | True
10 | Release
11 | Any CPU
12 | FileSystem
13 | bin\Release\net5.0\publish\
14 | FileSystem
15 |
16 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "Demo.WebSockets.Server": {
4 | "commandName": "Project",
5 | "dotnetRunMessages": "true",
6 | "environmentVariables": {
7 | "ASPNETCORE_ENVIRONMENT": "Development"
8 | }
9 | }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/Startup.cs:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Coldairarrow/Colder.Components/b294acc480ed8314299e61cf8a2950d9ab3c42ba/demos/Demo.WebSockets.Server/Startup.cs
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/appsettings.Development.json:
--------------------------------------------------------------------------------
1 | {
2 | "Logging": {
3 | "LogLevel": {
4 | "Default": "Information",
5 | "Microsoft": "Warning",
6 | "Microsoft.Hosting.Lifetime": "Information"
7 | }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/appsettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "urls": "http://*:5000",
3 | "Logging": {
4 | "LogLevel": {
5 | "Default": "Information",
6 | "Microsoft": "Warning",
7 | "Microsoft.Hosting.Lifetime": "Information"
8 | }
9 | },
10 | "AllowedHosts": "*"
11 | }
12 |
--------------------------------------------------------------------------------
/demos/Demo.WebSockets.Server/wwwroot/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 菜鸟教程(runoob.com)
6 |
7 |
34 |
35 |
36 |
37 |
40 |
41 |
--------------------------------------------------------------------------------
/demos/Domo.Console/Domo.Console.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | $(DemoNetVersion)
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/demos/Domo.Console/Program.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using System;
3 | using System.Drawing;
4 | using System.Drawing.Imaging;
5 |
6 | namespace Domo.Console1
7 | {
8 | class Program
9 | {
10 | static void Main(string[] args)
11 | {
12 | Image image = Image.FromFile("1.jpg");
13 | var newImg = ImgHelper.AddText(image, new Rectangle(0, 0, 50, 50), "测试", Color.White);
14 | newImg.Save("2.jpg", ImageFormat.Jpeg);
15 |
16 | Console.WriteLine("Hello World!");
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Colder.Api.Abstractions.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net6.0
5 |
6 | true
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Controllers/ApiControllerBase.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Microsoft.AspNetCore.Mvc;
3 |
4 | namespace Colder.Api.Abstractions.Controllers;
5 |
6 | ///
7 | /// Api控制器
8 | ///
9 | [Route("api/[controller]/[action]")]
10 | [ApiController]
11 | public abstract class ApiControllerBase : ControllerBase
12 | {
13 | ///
14 | /// 返回成功
15 | ///
16 | /// 数据类型
17 | /// 数据
18 | ///
19 | protected ApiResult Success(T data)
20 | {
21 | return new ApiResult { Data = data, Code = 200 };
22 | }
23 |
24 | ///
25 | /// 返回成功
26 | ///
27 | ///
28 | protected ApiResult Success()
29 | {
30 | return new ApiResult { Code = 200 };
31 | }
32 |
33 | ///
34 | /// 返回失败
35 | ///
36 | /// 错误消息
37 | /// 错误码
38 | ///
39 | protected ApiResult Failure(string msg, int code = 1)
40 | {
41 | return new ApiResult { Msg = msg, Code = code };
42 | }
43 |
44 | ///
45 | /// 返回失败
46 | ///
47 | /// 数据类型
48 | /// 错误消息
49 | /// 数据
50 | /// 错误码
51 | ///
52 | protected ApiResult Failure(string msg, T data = default, int code = 1)
53 | {
54 | return new ApiResult { Data = data, Msg = msg, Code = code };
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Controllers/SaveContext.cs:
--------------------------------------------------------------------------------
1 | namespace Colder.Api.Abstractions.Controllers;
2 |
3 | ///
4 | /// 保存上下文
5 | ///
6 | /// Dto数据类型
7 | /// 数据库表实体类型
8 | public class SaveContext
9 | {
10 | ///
11 | /// Dto数据
12 | ///
13 | public TDto Dto { get; set; }
14 |
15 | ///
16 | /// 原实体(更新之前的数据,更新后不会改变)
17 | ///
18 | public TEntity OriginalEntity { get; set; }
19 |
20 | ///
21 | /// 当前实体(跟踪中的数据库实体数据,会随着更新而改变,若数据删除则为null)
22 | ///
23 | public TEntity CurrentEntity { get; set; }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Filters/ExceptionFilter.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Microsoft.AspNetCore.Mvc;
3 | using Microsoft.AspNetCore.Mvc.Filters;
4 | using Microsoft.Extensions.Logging;
5 | using System;
6 | using System.Threading.Tasks;
7 |
8 | namespace Logistics.Api;
9 |
10 | ///
11 | ///
12 | ///
13 | public class ExceptionFilter : IAsyncExceptionFilter
14 | {
15 | readonly ILogger _logger;
16 |
17 | ///
18 | ///
19 | ///
20 | ///
21 | public ExceptionFilter(ILogger logger)
22 | {
23 | _logger = logger;
24 | }
25 |
26 | ///
27 | ///
28 | ///
29 | ///
30 | ///
31 | public async Task OnExceptionAsync(ExceptionContext context)
32 | {
33 | Exception ex = context.Exception;
34 |
35 | if (ex is MsgException busEx)
36 | {
37 | _logger.LogInformation(busEx.Message);
38 | context.Result = new JsonResult(new ApiResult { Msg = ex.Message, Code = 1 });
39 | }
40 | else
41 | {
42 | _logger.LogError(ex, ex.Message);
43 | context.Result = new JsonResult(new ApiResult { Msg = "系统异常", Code = 1 });
44 | }
45 |
46 | await Task.CompletedTask;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Filters/TransactionFilter.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Mvc.Filters;
2 | using Microsoft.EntityFrameworkCore;
3 | using System.Threading.Tasks;
4 | using System;
5 | using Microsoft.Extensions.DependencyInjection;
6 |
7 | namespace Colder.Api.Abstractions.Filters;
8 |
9 | ///
10 | ///
11 | ///
12 | public class TransactionFilter : Attribute, IAsyncActionFilter
13 | {
14 | private readonly Type _dbContextType;
15 |
16 | ///
17 | ///
18 | ///
19 | ///
20 | ///
21 | public TransactionFilter(Type dbContextType)
22 | {
23 | if (!dbContextType.IsAssignableTo(typeof(DbContext)))
24 | {
25 | throw new Exception("dbContextType必须为DbContext");
26 | }
27 | _dbContextType = dbContextType;
28 | }
29 |
30 | ///
31 | ///
32 | ///
33 | ///
34 | ///
35 | ///
36 | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
37 | {
38 | IServiceProvider serviceProvider = context.HttpContext.RequestServices;
39 | var dbContext = serviceProvider.GetRequiredService(_dbContextType) as DbContext;
40 |
41 | using var transaction = await dbContext.Database.BeginTransactionAsync();
42 |
43 | var excuted = await next();
44 |
45 | if (excuted.Exception == null)
46 | {
47 | await transaction.CommitAsync();
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/JwtExtentions.cs:
--------------------------------------------------------------------------------
1 | using Colder.Api.Abstractions.Options;
2 | using Microsoft.AspNetCore.Authentication.JwtBearer;
3 | using Microsoft.Extensions.DependencyInjection;
4 | using Microsoft.IdentityModel.Tokens;
5 | using System;
6 | using System.Text;
7 |
8 | namespace Colder.Api.Abstractions;
9 |
10 | ///
11 | ///
12 | ///
13 | public static class JwtExtentions
14 | {
15 | ///
16 | ///
17 | ///
18 | ///
19 | ///
20 | ///
21 | public static IServiceCollection AddJwt(this IServiceCollection services, ApiOptions apiOption)
22 | {
23 | services.AddAuthentication(x =>
24 | {
25 | x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
26 | x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
27 | }).AddJwtBearer(x =>
28 | {
29 | x.RequireHttpsMetadata = false;
30 | x.SaveToken = false;
31 | //Token Validation Parameters
32 | x.TokenValidationParameters = new TokenValidationParameters
33 | {
34 | ClockSkew = TimeSpan.Zero,//到时间立即过期
35 | ValidateIssuerSigningKey = true,
36 | //获取或设置要使用的Microsoft.IdentityModel.Tokens.SecurityKey用于签名验证。
37 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.
38 | GetBytes(apiOption.JwtSecret)),
39 | ValidateIssuer = false,
40 | ValidateAudience = false,
41 | };
42 | });
43 |
44 | return services;
45 | }
46 | }
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Middlewares/RealIpMiddleware.cs:
--------------------------------------------------------------------------------
1 | using Microsoft.AspNetCore.Http;
2 | using System.Threading.Tasks;
3 | using System;
4 | using System.Net;
5 |
6 | namespace Colder.Api.Abstractions.Middlewares;
7 |
8 | internal class RealIpMiddleware
9 | {
10 | private readonly RequestDelegate _next;
11 |
12 | public RealIpMiddleware(RequestDelegate next)
13 | {
14 | _next = next;
15 | }
16 |
17 | public Task Invoke(HttpContext context)
18 | {
19 | var headers = context.Request.Headers;
20 | if (headers.ContainsKey("X-Forwarded-For"))
21 | {
22 | context.Connection.RemoteIpAddress = IPAddress.Parse(headers["X-Forwarded-For"].ToString().Split(',', StringSplitOptions.RemoveEmptyEntries)[0]);
23 | }
24 | return _next(context);
25 | }
26 | }
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Middlewares/RequestInfo.cs:
--------------------------------------------------------------------------------
1 | namespace Colder.Api.Abstractions
2 | {
3 | ///
4 | /// 请求信息
5 | ///
6 | public class RequestInfo
7 | {
8 | ///
9 | /// 请求Body
10 | ///
11 | public string RequestBody { get; set; }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Middlewares/RequestInfoMiddleware.cs:
--------------------------------------------------------------------------------
1 | using Colder.Common;
2 | using Microsoft.AspNetCore.Http;
3 | using Microsoft.Extensions.DependencyInjection;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Colder.Api.Abstractions
8 | {
9 | internal class RequestInfoMiddleware
10 | {
11 | private readonly RequestDelegate _next;
12 |
13 | public RequestInfoMiddleware(RequestDelegate next)
14 | {
15 | _next = next;
16 | }
17 |
18 | public async Task Invoke(HttpContext context)
19 | {
20 | var contentType = (context.Request.ContentType ?? string.Empty).ToLower();
21 | if (contentType.Contains("text")
22 | || contentType.Contains("xml")
23 | || contentType.Contains("json")
24 | || contentType.Contains("x-www-form-urlencoded")
25 | || contentType.Contains("form-data"))
26 | {
27 | context.Request.EnableBuffering();
28 | string body = await context.Request.Body?.ReadToStringAsync(Encoding.UTF8);
29 | context.RequestServices.GetService().RequestBody = body;
30 | }
31 |
32 | await _next(context);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Options/ApiOptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Colder.Api.Abstractions.Options;
4 |
5 | ///
6 | ///
7 | ///
8 | public class ApiOptions
9 | {
10 | ///
11 | /// 启用Jwt
12 | ///
13 | public bool EnableJwt { get; set; } = true;
14 |
15 | ///
16 | /// Jwt密钥
17 | ///
18 | public string JwtSecret { get; set; }
19 |
20 | ///
21 | /// 启用Swagger
22 | ///
23 | public bool EnableSwagger { get; set; } = true;
24 |
25 | ///
26 | /// 文档分组
27 | ///
28 | public SwaggerDocumentOptions[] Documents { get; set; } = Array.Empty();
29 | }
30 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Options/SwaggerDocumentOptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Colder.Api.Abstractions.Options;
4 |
5 | ///
6 | /// Swagger 文档配置。
7 | ///
8 | public class SwaggerDocumentOptions
9 | {
10 | ///
11 | /// 获取或设置 Swagger 标题。
12 | ///
13 | public string Title { get; set; }
14 |
15 | ///
16 | /// 获取或设置文档名称。
17 | ///
18 | public string DocumentName { get; set; }
19 |
20 | ///
21 | /// 获取或设置 API 分组名称。
22 | ///
23 | public string[] ApiGroupNames { get; set; } = Array.Empty();
24 | }
25 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Swagger/ControllerFilterProcessor.cs:
--------------------------------------------------------------------------------
1 | using NSwag.Generation.Processors;
2 | using NSwag.Generation.Processors.Contexts;
3 | using System;
4 |
5 | namespace Colder.Api.Abstractions;
6 |
7 | internal class ControllerFilterProcessor : IOperationProcessor
8 | {
9 | public bool Process(OperationProcessorContext context)
10 | {
11 | Console.WriteLine("[NSWAG] CONTROLLER " + context.ControllerType.Name + " " + context.MethodInfo.Name);
12 | return true;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Colder.Api.Abstractions/Swagger/EnumSchemaProcessor.cs:
--------------------------------------------------------------------------------
1 | using Namotion.Reflection;
2 | using NJsonSchema.Generation;
3 | using System;
4 | using System.Linq;
5 | using System.Text;
6 |
7 | namespace Colder.Api.Abstractions.Swagger;
8 |
9 | ///
10 | ///
11 | ///
12 | internal class EnumSchemaProcessor : ISchemaProcessor
13 | {
14 | ///
15 | ///
16 | ///
17 | ///
18 | public void Process(SchemaProcessorContext context)
19 | {
20 | var type = context.ContextualType.Type;
21 | if (!type.IsEnum)
22 | {
23 | return;
24 | }
25 |
26 | var sb = new StringBuilder();
27 | sb.AppendLine(type.GetXmlDocsSummary());
28 |
29 | var members = type.GetMembers();
30 | var schema = context.Schema;
31 | for (var i = 0; i < schema.Enumeration.Count; i++)
32 | {
33 | var item = schema.Enumeration.ElementAt(i);
34 | var enumName = Enum.GetName(type, item);
35 |
36 | var summary = members.FirstOrDefault(a => a.Name == enumName)
37 | .GetXmlDocsSummary();
38 |
39 | sb.AppendLine($"{item} = {summary}");
40 | }
41 |
42 | schema.Description = sb.ToString();
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Colder.AutoMapper/Colder.AutoMapper.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.1
5 | enable
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/Colder.AutoMapper/MappingExpressionExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using AutoMapper;
3 |
4 | namespace Colder.AutoMapper;
5 |
6 | ///
7 | ///
8 | ///
9 | public static class AutoMapperExtensions
10 | {
11 | ///
12 | /// 忽略所有不匹配的属性。
13 | ///
14 | ///
15 | ///
16 | ///
17 | ///
18 | public static IMappingExpression IgnoreAllNonExisting(this IMappingExpression expression)
19 | {
20 | var flags = BindingFlags.Public | BindingFlags.Instance;
21 | var sourceType = typeof(TSource);
22 | var destinationProperties = typeof(TDestination).GetProperties(flags);
23 |
24 | foreach (var property in destinationProperties)
25 | {
26 | if (sourceType.GetProperty(property.Name, flags) == null)
27 | {
28 | expression.ForMember(property.Name, opt => opt.Ignore());
29 | }
30 | }
31 | return expression;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Colder.Cache/Colder.Cache.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | 缓存封装
6 |
7 | true
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Colder.Cache/Options/CacheOptions.cs:
--------------------------------------------------------------------------------
1 | namespace Colder.Cache
2 | {
3 | ///
4 | /// 缓存配置
5 | ///
6 | public class CacheOptions
7 | {
8 | ///
9 | /// 缓存类型
10 | ///
11 | public CacheTypes CacheType { get; set; }
12 |
13 | ///
14 | /// Redis连接字符串
15 | /// 配置参考 https://stackexchange.github.io/StackExchange.Redis/Configuration.html
16 | ///
17 | public string RedisConnectionString { get; set; }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Colder.Cache/Options/CacheTypes.cs:
--------------------------------------------------------------------------------
1 | namespace Colder.Cache
2 | {
3 | ///
4 | /// 缓存类型
5 | ///
6 | public enum CacheTypes
7 | {
8 | ///
9 | /// 使用内存缓存(不支持分布式)
10 | ///
11 | InMemory = 0,
12 |
13 | ///
14 | /// 使用Redis缓存(支持分布式)
15 | ///
16 | Redis = 1
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Colder.Common.Util/Colder.Common.Util.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | 通用工具
6 |
7 | true
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/src/Colder.Common.Util/ElasticClientExtentions.cs:
--------------------------------------------------------------------------------
1 | using Nest;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 |
6 | namespace Colder.Common.Util;
7 |
8 | ///
9 | /// ElasticClient拓展
10 | ///
11 | public static class ElasticClientExtentions
12 | {
13 | ///
14 | /// 查询所有数据,通过SearchAfter方式
15 | ///
16 | /// 文档类型
17 | /// es客户端
18 | /// 查询条件
19 | /// Id字段
20 | ///
21 | public static async Task SearchAll(
22 | this IElasticClient elasticClient,
23 | SearchDescriptor searchDescriptor = null,
24 | string idField = "Id"
25 | )
26 | where TDocument : class
27 | {
28 | List result = new List();
29 | TDocument last = null;
30 | while (true)
31 | {
32 | searchDescriptor ??= new SearchDescriptor();
33 | searchDescriptor = searchDescriptor.Sort(y => y.Ascending($"{idField}.keyword")).Size(10000);
34 | if (last != null)
35 | {
36 | searchDescriptor = searchDescriptor.SearchAfter(last.GetType().GetProperty(idField).GetValue(last));
37 | }
38 |
39 | var response = await elasticClient.SearchAsync(searchDescriptor);
40 | if (!response.ApiCall.Success)
41 | {
42 | throw response.OriginalException;
43 | }
44 |
45 | result.AddRange(response.Documents);
46 |
47 | if (response.Documents.Count < 10000)
48 | {
49 | break;
50 | }
51 | last = response.Documents.LastOrDefault();
52 | }
53 |
54 | return result.ToArray();
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/src/Colder.Common.Util/Helpers/FileZipHelper.cs:
--------------------------------------------------------------------------------
1 | using ICSharpCode.SharpZipLib.Zip;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.IO;
5 |
6 | namespace Colder.Common
7 | {
8 | ///
9 | /// 文件压缩帮助类
10 | ///
11 | public class FileZipHelper
12 | {
13 | ///
14 | /// 压缩一个文件
15 | ///
16 | /// 文件信息
17 | ///
18 | public static byte[] ZipFile(FileEntry file)
19 | {
20 | return ZipFile(new List { file });
21 | }
22 |
23 | ///
24 | /// 压缩多个文件
25 | ///
26 | /// 文件信息列表
27 | ///
28 | public static byte[] ZipFile(List files)
29 | {
30 | using (MemoryStream ms = new MemoryStream())
31 | {
32 | using (ZipOutputStream zipStream = new ZipOutputStream(ms))
33 | {
34 | files.ForEach(aFile =>
35 | {
36 | byte[] fileBytes = aFile.FileBytes;
37 | ZipEntry entry = new ZipEntry(aFile.FileName)
38 | {
39 | DateTime = DateTime.Now,
40 | IsUnicodeText = true
41 | };
42 | zipStream.PutNextEntry(entry);
43 | zipStream.Write(fileBytes, 0, fileBytes.Length);
44 | zipStream.CloseEntry();
45 | });
46 |
47 | zipStream.IsStreamOwner = false;
48 | zipStream.Finish();
49 | zipStream.Close();
50 | ms.Position = 0;
51 |
52 | return ms.ToArray();
53 | }
54 | }
55 | }
56 | }
57 |
58 | ///
59 | /// 文件信息
60 | ///
61 | public struct FileEntry
62 | {
63 | ///
64 | ///
65 | ///
66 | ///
67 | ///
68 | public FileEntry(string fileName, byte[] fileBytes)
69 | {
70 | FileName = fileName;
71 | FileBytes = fileBytes;
72 | }
73 |
74 | ///
75 | /// 文件名
76 | ///
77 | public string FileName { get; set; }
78 |
79 | ///
80 | /// 文件字节
81 | ///
82 | public byte[] FileBytes { get; set; }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/Colder.Common.Util/Helpers/LinqHelper.cs:
--------------------------------------------------------------------------------
1 | using DynamicExpresso;
2 | using System;
3 | using System.Linq.Expressions;
4 | using System.Reflection;
5 |
6 | namespace Colder.Common.Util
7 | {
8 | ///
9 | /// Linq操作帮助类
10 | ///
11 | public static class LinqHelper
12 | {
13 | static LinqHelper()
14 | {
15 | try
16 | {
17 | EFCore = Assembly.Load("Microsoft.EntityFrameworkCore");
18 | EFCoreNpgsql = Assembly.Load("Npgsql.EntityFrameworkCore.PostgreSQL");
19 | }
20 | catch
21 | {
22 |
23 | }
24 | }
25 |
26 |
27 | private static readonly Assembly EFCore;
28 | private static readonly Assembly EFCoreNpgsql;
29 |
30 | ///
31 | /// 创建初始条件为True的表达式
32 | ///
33 | ///
34 | ///
35 | public static Expression> True()
36 | {
37 | return x => true;
38 | }
39 |
40 | ///
41 | /// 创建初始条件为False的表达式
42 | ///
43 | ///
44 | ///
45 | public static Expression> False()
46 | {
47 | return x => false;
48 | }
49 |
50 | ///
51 | /// 构建动态表达式
52 | ///
53 | ///
54 | public static Expression> BuildDynamicExpression(string[] paramterNames, string expressionStr, params (string name, object value)[] variables)
55 | {
56 | var interpreter = new Interpreter();
57 | if (EFCore != null)
58 | {
59 | interpreter.Reference(EFCore.GetType("Microsoft.EntityFrameworkCore.EF"));
60 | interpreter.Reference(EFCore.GetType("Microsoft.EntityFrameworkCore.DbFunctionsExtensions"));
61 | }
62 | if (EFCoreNpgsql != null)
63 | {
64 | interpreter.Reference(EFCoreNpgsql.GetType("Microsoft.EntityFrameworkCore.NpgsqlDbFunctionsExtensions"));
65 | }
66 | if (variables != null)
67 | {
68 | foreach (var (name, value) in variables)
69 | {
70 | interpreter.SetVariable(name, value);
71 | }
72 | }
73 |
74 | return interpreter.ParseAsExpression>(expressionStr, paramterNames);
75 | }
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/Colder.Common.Util/Helpers/ProxyHelper.cs:
--------------------------------------------------------------------------------
1 | using Castle.DynamicProxy;
2 | using System;
3 | using System.Threading.Tasks;
4 |
5 | namespace Colder.Common.Util;
6 |
7 | ///
8 | /// 代理帮助类
9 | ///
10 | public static class ProxyHelper
11 | {
12 | private static readonly ProxyGenerator _generator = new ProxyGenerator();
13 |
14 | ///
15 | /// 创建接口代理
16 | ///
17 | /// 接口类型
18 | /// 接口对象
19 | /// 过滤器,参数依次为上下文、执行委托
20 | ///
21 | public static TInterface CreateProxy(TInterface @interface, Func, Task> filter)
22 | where TInterface : class
23 | {
24 | return _generator.CreateInterfaceProxyWithTarget(@interface, new CastleInterceptor(filter));
25 | }
26 |
27 | internal class CastleInterceptor : AsyncInterceptorBase
28 | {
29 | private readonly Func, Task> _filter;
30 | public CastleInterceptor(Func, Task> filter)
31 | {
32 | _filter = filter;
33 | }
34 |
35 | protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func proceed)
36 | {
37 | await _filter(invocation, () => proceed(invocation, proceedInfo));
38 | }
39 |
40 | protected override async Task InterceptAsync(IInvocation invocation, IInvocationProceedInfo proceedInfo, Func> proceed)
41 | {
42 | TResult result = default;
43 |
44 | await _filter(invocation, async () =>
45 | {
46 | result = await proceed(invocation, proceedInfo);
47 | if (typeof(Task).IsAssignableFrom(invocation.Method.ReturnType))
48 | {
49 | invocation.ReturnValue = Task.FromResult(result);
50 | }
51 | else
52 | {
53 | invocation.ReturnValue = result;
54 | }
55 | });
56 |
57 | return result;
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/Colder.Common/Colder.Common.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | true
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/Colder.Common/Extentions/DateTimeExtentions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Globalization;
3 |
4 | namespace Colder.Common
5 | {
6 | ///
7 | ///
8 | ///
9 | public static class DateTimeExtentions
10 | {
11 | ///
12 | /// 获取某一日期是该年中的第几周
13 | ///
14 | /// 日期
15 | /// 该日期在该年中的周数
16 | public static int GetWeekOfYear(this DateTime dateTime)
17 | {
18 | GregorianCalendar gc = new GregorianCalendar();
19 | return gc.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Colder.Common/Extentions/EnumExtentions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.ComponentModel;
3 | using System.Linq;
4 |
5 | namespace Colder.Common
6 | {
7 | ///
8 | /// 拓展类
9 | ///
10 | public static class EnumExtentions
11 | {
12 | ///
13 | /// 获取枚举描述
14 | ///
15 | /// 枚举值
16 | ///
17 | public static string GetDescription(this Enum value)
18 | {
19 | DescriptionAttribute attribute = value.GetType()
20 | .GetField(value.ToString())
21 | .GetCustomAttributes(typeof(DescriptionAttribute), false)
22 | .SingleOrDefault() as DescriptionAttribute;
23 | return attribute == null ? value.ToString() : attribute.Description;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Colder.Common/Extentions/HttpClientExtentions.cs:
--------------------------------------------------------------------------------
1 | using System.Net.Http;
2 | using System.Net.Http.Headers;
3 | using System.Threading.Tasks;
4 |
5 | namespace Colder.Common
6 | {
7 | ///
8 | ///
9 | ///
10 | public static class HttpClientExtentions
11 | {
12 | ///
13 | /// Post请求并获取Json
14 | ///
15 | ///
16 | ///
17 | ///
18 | ///
19 | public static async Task PostJson(this HttpClient httpClient, string url, string body)
20 | {
21 | HttpContent content = new StringContent(body);
22 | content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
23 | HttpResponseMessage response = await httpClient.PostAsync(url, content);
24 | response.EnsureSuccessStatusCode();
25 | return await response.Content.ReadAsStringAsync();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Colder.Common/Extentions/IQueryableExtentions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 | using System.Linq.Expressions;
4 |
5 | namespace Colder.Common
6 | {
7 | ///
8 | /// IQueryable"T"的拓展操作
9 | ///
10 | public static class IQueryableExtentions
11 | {
12 | ///
13 | /// 符合条件则Where
14 | ///
15 | /// 实体类型
16 | /// 数据源
17 | /// 是否符合条件
18 | /// 筛选
19 | ///
20 | public static IQueryable WhereIf(this IQueryable q, bool need, Expression> where)
21 | {
22 | if (need)
23 | {
24 | return q.Where(where);
25 | }
26 | else
27 | {
28 | return q;
29 | }
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Colder.Common/Extentions/ObjectExtentions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.ComponentModel;
4 | using System.Linq;
5 |
6 | namespace Colder.Common
7 | {
8 | ///
9 | ///
10 | ///
11 | public static class ObjectExtentions
12 | {
13 | ///
14 | /// 判断是否为Null或者空
15 | ///
16 | /// 对象
17 | ///
18 | public static bool IsNullOrEmpty(this object obj)
19 | {
20 | if (obj == null)
21 | return true;
22 | else
23 | {
24 | string objStr = obj.ToString();
25 | return string.IsNullOrEmpty(objStr);
26 | }
27 | }
28 |
29 | ///
30 | /// 改变类型
31 | ///
32 | /// 原对象
33 | /// 目标类型
34 | ///
35 | public static object ChangeTypeByConvert(this object obj, Type targetType)
36 | {
37 | object resObj;
38 | if (targetType.IsGenericType && targetType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
39 | {
40 | NullableConverter newNullableConverter = new NullableConverter(targetType);
41 | resObj = newNullableConverter.ConvertFrom(obj);
42 | }
43 | else
44 | {
45 | resObj = Convert.ChangeType(obj, targetType);
46 | }
47 |
48 | return resObj;
49 | }
50 |
51 | ///
52 | /// 判断是否有效
53 | /// 注:不为null,不为空Guid,不为空集合
54 | ///
55 | /// 对象
56 | ///
57 | public static bool IsValid(this object obj)
58 | {
59 | if (obj == null)
60 | {
61 | return false;
62 | }
63 | if (obj is Guid?)
64 | {
65 | return (Guid?)obj != Guid.Empty;
66 | }
67 | if (obj is Guid guid)
68 | {
69 | return guid != Guid.Empty;
70 | }
71 | if (obj is string str)
72 | {
73 | return !string.IsNullOrEmpty(str);
74 | }
75 | if (obj is IEnumerable enumerable)
76 | {
77 | return enumerable.Cast