diff --git a/samples/apis/Backet.Api/Backet.Api.csproj b/samples/apis/Backet.Api/Backet.Api.csproj index c1b32ca..c9da4a3 100644 --- a/samples/apis/Backet.Api/Backet.Api.csproj +++ b/samples/apis/Backet.Api/Backet.Api.csproj @@ -12,8 +12,16 @@ + + + + + + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -38,6 +46,7 @@ + diff --git a/samples/apis/Backet.Api/GrpcServices/BacketService.cs b/samples/apis/Backet.Api/GrpcServices/BacketService.cs new file mode 100644 index 0000000..cf1212d --- /dev/null +++ b/samples/apis/Backet.Api/GrpcServices/BacketService.cs @@ -0,0 +1,27 @@ +using Backet.Api.Grains.Abstraction; +using Backet.Grpc; +using Grpc.Core; +using Orleans; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Backet.Api.GrpcServices +{ + public class BacketService : Backet.Grpc.Backet.BacketBase + { + private readonly IClusterClient clusterClient; + public BacketService(IClusterClient clusterClient) + { + this.clusterClient = clusterClient; + } + public override Task AddBacket(AddBacketRequest backetDto, ServerCallContext context) + { + var newId = Guid.NewGuid().ToString("N").ToLower(); + backetDto.Id = newId; + var grain = clusterClient.GetGrain(newId); + return Task.FromResult(Pole.Grpc.ExtraType.CommonCommandResponse.SuccessResponse); + } + } +} diff --git a/samples/apis/Backet.Api/Program.cs b/samples/apis/Backet.Api/Program.cs index a3158ca..05226d0 100644 --- a/samples/apis/Backet.Api/Program.cs +++ b/samples/apis/Backet.Api/Program.cs @@ -37,6 +37,17 @@ namespace Backet.Api .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); + webBuilder.UseKestrel(option => + { + option.ListenAnyIP(81, config => + { + config.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http1; + }); + option.ListenAnyIP(82, config => + { + config.Protocols = Microsoft.AspNetCore.Server.Kestrel.Core.HttpProtocols.Http2; + }); + }); }); } } diff --git a/samples/apis/Backet.Api/Protos/Common/commonCommandResponse.proto b/samples/apis/Backet.Api/Protos/Common/commonCommandResponse.proto new file mode 100644 index 0000000..d9ff0c9 --- /dev/null +++ b/samples/apis/Backet.Api/Protos/Common/commonCommandResponse.proto @@ -0,0 +1,10 @@ +syntax = "proto3"; + + +package pole.Grpc.ExtraType; + +message CommonCommandResponse { + // 1 成功 2 失败 + int32 status = 1; + string message = 2; +} \ No newline at end of file diff --git a/samples/apis/Backet.Api/Protos/backet.proto b/samples/apis/Backet.Api/Protos/backet.proto new file mode 100644 index 0000000..6b32a22 --- /dev/null +++ b/samples/apis/Backet.Api/Protos/backet.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +option csharp_namespace = "Backet.Grpc"; + +import "Protos/Common/commonCommandResponse.proto"; +package greet; + +// The greeting service definition. +service Backet { + // Sends a greeting + rpc AddBacket (AddBacketRequest) returns (pole.Grpc.ExtraType.CommonCommandResponse); +} + +// The request message containing the user's name. +message AddBacketRequest { + string name = 1; + string id = 2; + string userId = 3; + repeated AddBacketItemRequest Items = 4; + message AddBacketItemRequest{ + string productId = 1; + string productName = 2; + int64 price = 3; + } +} + diff --git a/samples/apis/Backet.Api/Startup.cs b/samples/apis/Backet.Api/Startup.cs index e29b3e0..e7ef81e 100644 --- a/samples/apis/Backet.Api/Startup.cs +++ b/samples/apis/Backet.Api/Startup.cs @@ -1,4 +1,5 @@ using Backet.Api.Grains; +using Backet.Api.GrpcServices; using Backet.Api.Infrastructure; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -27,6 +28,10 @@ namespace Backet.Api services.AddDbContextPool(options => options.UseNpgsql(Configuration["postgres:write"])); services.AddControllers(); + services.AddGrpc(); + services.AddGrpcValidation(); + services.AddGrpcRequestValidator(); + services.AddPole(config => { config.AddRabbitMQ(option => @@ -62,6 +67,7 @@ namespace Backet.Api app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapGrpcService(); endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); diff --git a/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs b/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs index 61712d2..cdc3132 100644 --- a/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs +++ b/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs @@ -1,7 +1,4 @@ -using Pole.Application.Cqrs; -using Pole.Domain; -using Pole.Domain.UnitOfWork; -using System; +using System; using System.Collections.Generic; using System.Text; diff --git a/src/Pole.Grpc/Pole.Grpc.csproj b/src/Pole.Grpc/Pole.Grpc.csproj index c615e71..ba5b54b 100644 --- a/src/Pole.Grpc/Pole.Grpc.csproj +++ b/src/Pole.Grpc/Pole.Grpc.csproj @@ -13,6 +13,7 @@ - + + diff --git a/src/Pole.Grpc/Validation/Internal/DefaultValidatorRegistrar.cs b/src/Pole.Grpc/Validation/Internal/ValidatorRegistrar.cs similarity index 95% rename from src/Pole.Grpc/Validation/Internal/DefaultValidatorRegistrar.cs rename to src/Pole.Grpc/Validation/Internal/ValidatorRegistrar.cs index 0fd723b..5e418ed 100644 --- a/src/Pole.Grpc/Validation/Internal/DefaultValidatorRegistrar.cs +++ b/src/Pole.Grpc/Validation/Internal/ValidatorRegistrar.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace Pole.Grpc.Validation.Internal { - class DefaultValidatorRegistrar : IValidatorRegistrar + class ValidatorRegistrar : IValidatorRegistrar { public Task Register(Type validatorType, IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) { diff --git a/src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs b/src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs index 6f6e0b8..c52d1aa 100644 --- a/src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs +++ b/src/Pole.Grpc/Validation/ServiceCollectionExtensions.cs @@ -1,6 +1,8 @@ using FluentValidation; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Pole.Core.Utils; using Pole.Grpc.Validation; using Pole.Grpc.Validation.Internal; using System; @@ -16,33 +18,26 @@ namespace Microsoft.Extensions.DependencyInjection public static IServiceCollection AddGrpcValidation(this IServiceCollection services) { services.AddSingleton(); - services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); return services; } - public static IServiceCollection AddGrpcRequestValidator(this IServiceCollection services,Assembly validatorAssembly ,ServiceLifetime lifetime = ServiceLifetime.Singleton) + public static IServiceCollection AddGrpcRequestValidator(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) { - - Action validateOptionConfig = validateOption => { - validateOption.ValidatorAssembly = validatorAssembly; - }; - - services.Configure(validateOptionConfig); - - using (var serviceProvider= services.BuildServiceProvider()) + using (var serviceProvider = services.BuildServiceProvider()) { - var option = serviceProvider.GetRequiredService>(); var validatorRegistrar = serviceProvider.GetRequiredService(); - var validators = option.Value.ValidatorAssembly.GetTypes().Where(m => m.GetInterfaces().FirstOrDefault(t => - t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IValidator<>))!=null); - foreach (var validator in validators) + foreach (var assembly in AssemblyHelper.GetAssemblies(serviceProvider.GetService>())) { - validatorRegistrar.Register(validator, services); + var validators = assembly.GetTypes().Where(m => m.GetInterfaces().FirstOrDefault(t =>t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IValidator<>)) != null); + foreach (var validator in validators) + { + validatorRegistrar.Register(validator, services); + } } - return services; - } + } } } } diff --git a/src/Pole.Grpc/Validation/ValidateOption.cs b/src/Pole.Grpc/Validation/ValidateOption.cs deleted file mode 100644 index 4c5c286..0000000 --- a/src/Pole.Grpc/Validation/ValidateOption.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; - -namespace Pole.Grpc.Validation -{ - public class ValidateOption - { - public Assembly ValidatorAssembly { get; set; } - } -} diff --git a/test/Pole.Samples.Backet.Api/Program.cs b/test/Pole.Samples.Backet.Api/Program.cs index b917538..24eaa1d 100644 --- a/test/Pole.Samples.Backet.Api/Program.cs +++ b/test/Pole.Samples.Backet.Api/Program.cs @@ -27,23 +27,13 @@ namespace Pole.Samples.Backet.Api // await uploader.UpdateAsync("\"pole\".\"Events\"", events); //} // Queue the task. - for (var i = 0; i < 100; i++) - { - ThreadPool.QueueUserWorkItem(ThreadProc, i); - } + string s = "11111111111111111111"; + var bytes= Encoding.ASCII.GetBytes(s); Console.WriteLine("Main thread does some work, then sleeps."); Thread.Sleep(1000); Console.WriteLine("Main thread exits."); } - - // This thread procedure performs the task. - static void ThreadProc(Object stateInfo) - { - var i = Convert.ToInt32(stateInfo); - // No state object was passed to QueueUserWorkItem, so stateInfo is null. - Console.WriteLine($"Hello from the thread pool.{i}"); - } } }