From e58429ac2a60950044bdfd1834f5d18941b7d5f7 Mon Sep 17 00:00:00 2001 From: ericuss Date: Mon, 26 Aug 2019 13:57:43 +0200 Subject: [PATCH] first approach --- docker-compose.override.yml | 1 + eShopOnContainers.sln | 107 +++++++++--------- .../Clients.Grpc.Caller.csproj | 16 +++ src/Clients/Clients.Grpc.Caller/Program.cs | 37 ++++++ .../Clients.Grpc.Caller/Protos/basket.proto | 34 ++++++ .../Basket/Basket.API/Basket.API.csproj | 6 +- .../Basket/Basket.API/Grpc/BasketService.cs | 89 +++++++++------ src/Services/Basket/Basket.API/Program.cs | 2 +- .../Basket/Basket.API/Proto/basket.proto | 10 +- src/Services/Basket/Basket.API/Startup.cs | 103 ++++++++++------- .../TestHttpResponseTrailersFeature.cs | 10 ++ src/_build/dependencies.props | 6 +- 12 files changed, 284 insertions(+), 137 deletions(-) create mode 100644 src/Clients/Clients.Grpc.Caller/Clients.Grpc.Caller.csproj create mode 100644 src/Clients/Clients.Grpc.Caller/Program.cs create mode 100644 src/Clients/Clients.Grpc.Caller/Protos/basket.proto create mode 100644 src/Services/Basket/Basket.API/TestHttpResponseTrailersFeature.cs diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 97379a4b0..4428a9330 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -72,6 +72,7 @@ services: ports: - "5103:80" + - "5580:5001" catalog.api: environment: diff --git a/eShopOnContainers.sln b/eShopOnContainers.sln index 232331854..658eb26d7 100644 --- a/eShopOnContainers.sln +++ b/eShopOnContainers.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.27130.2024 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29123.88 MinimumVisualStudioVersion = 10.0.40219.1 Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{FEA0C318-FFED-4D39-8781-265718CA43DD}" EndProject @@ -163,6 +163,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Devspaces.Support", "Devspa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Devspaces.Support", "src\BuildingBlocks\Devspaces.Support\Devspaces.Support.csproj", "{CB6D01A4-E597-4348-9570-FC8DB03B4267}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Clients", "Clients", "{8449B293-5949-4832-8612-6BCE2962BCB1}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Clients.Grpc.Caller", "src\Clients\Clients.Grpc.Caller\Clients.Grpc.Caller.csproj", "{7C8FA264-ED49-4ACA-9629-FCE5462B30AB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Ad-Hoc|Any CPU = Ad-Hoc|Any CPU @@ -1369,54 +1373,6 @@ Global {A7337243-33B8-463A-87AD-944B75EFD820}.Release|x86.ActiveCfg = Release|x86 {A7337243-33B8-463A-87AD-944B75EFD820}.Release|x86.Build.0 = Release|x86 {A7337243-33B8-463A-87AD-944B75EFD820}.Release|x86.Deploy.0 = Release|x86 - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|x64.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Ad-Hoc|x86.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|Any CPU.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|ARM.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|ARM.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|iPhone.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|iPhone.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|x64.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|x64.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|x86.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.AppStore|x86.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|Any CPU.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|ARM.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|ARM.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|iPhone.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|iPhone.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|x64.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|x64.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|x86.ActiveCfg = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Debug|x86.Build.0 = Debug|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|Any CPU.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|Any CPU.Build.0 = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|ARM.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|ARM.Build.0 = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|iPhone.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|iPhone.Build.0 = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|iPhoneSimulator.Build.0 = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|x64.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|x64.Build.0 = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|x86.ActiveCfg = Release|Any CPU - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480}.Release|x86.Build.0 = Release|Any CPU {9F00E62F-E180-4A9C-8794-98A72AFAC2DB}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU {9F00E62F-E180-4A9C-8794-98A72AFAC2DB}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU {9F00E62F-E180-4A9C-8794-98A72AFAC2DB}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU @@ -2089,6 +2045,54 @@ Global {CB6D01A4-E597-4348-9570-FC8DB03B4267}.Release|x64.Build.0 = Release|Any CPU {CB6D01A4-E597-4348-9570-FC8DB03B4267}.Release|x86.ActiveCfg = Release|Any CPU {CB6D01A4-E597-4348-9570-FC8DB03B4267}.Release|x86.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|x64.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Ad-Hoc|x86.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|Any CPU.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|ARM.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|ARM.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|iPhone.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|iPhone.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|x64.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|x64.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|x86.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.AppStore|x86.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|ARM.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|ARM.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|iPhone.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|x64.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|x64.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|x86.ActiveCfg = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Debug|x86.Build.0 = Debug|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|Any CPU.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|ARM.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|ARM.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|iPhone.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|iPhone.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|iPhoneSimulator.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|x64.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|x64.Build.0 = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|x86.ActiveCfg = Release|Any CPU + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -2136,7 +2140,6 @@ Global {A289A7F0-ACD8-42AE-87B6-AB1AFD310BF1} = {0AAED9FF-3260-43BB-B586-9AAF1E010A90} {B68C2B56-7581-46AE-B55D-D25DDFD3BFE3} = {0AAED9FF-3260-43BB-B586-9AAF1E010A90} {A7337243-33B8-463A-87AD-944B75EFD820} = {0AAED9FF-3260-43BB-B586-9AAF1E010A90} - {16CDE5D2-2DDE-4AF2-B902-AD9CC42DE480} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B} {9F00E62F-E180-4A9C-8794-98A72AFAC2DB} = {7BA332A2-189D-4D03-9935-FDFF81C42496} {63417272-1E6A-406A-AD11-C738558D89C0} = {7BA332A2-189D-4D03-9935-FDFF81C42496} {56E0E455-731E-41CB-AF46-C1A70F8A140B} = {2F0DEF71-84AC-4212-86D4-E36E8896BDBF} @@ -2163,6 +2166,8 @@ Global {E39BD762-BC86-459D-B818-B6BF2D9F1352} = {424BC53E-17EA-4E12-BC07-64BAA927ABCB} {ABBA561B-499B-48C0-8299-85D41E6E9E98} = {DB0EFB20-B024-4E5E-A75C-52143C131D25} {CB6D01A4-E597-4348-9570-FC8DB03B4267} = {ABBA561B-499B-48C0-8299-85D41E6E9E98} + {8449B293-5949-4832-8612-6BCE2962BCB1} = {932D8224-11F6-4D07-B109-DA28AD288A63} + {7C8FA264-ED49-4ACA-9629-FCE5462B30AB} = {8449B293-5949-4832-8612-6BCE2962BCB1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9} diff --git a/src/Clients/Clients.Grpc.Caller/Clients.Grpc.Caller.csproj b/src/Clients/Clients.Grpc.Caller/Clients.Grpc.Caller.csproj new file mode 100644 index 000000000..379eb2de1 --- /dev/null +++ b/src/Clients/Clients.Grpc.Caller/Clients.Grpc.Caller.csproj @@ -0,0 +1,16 @@ + + + + Exe + netcoreapp3.0 + + + + + + + + + + + diff --git a/src/Clients/Clients.Grpc.Caller/Program.cs b/src/Clients/Clients.Grpc.Caller/Program.cs new file mode 100644 index 000000000..32ce7905e --- /dev/null +++ b/src/Clients/Clients.Grpc.Caller/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using Grpc.Net.Client; +using GrpcBasket; +namespace Clients.Grpc.Caller +{ + class Program + { + static async Task Main(string[] args) + { + Console.WriteLine("Starting..."); + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); + AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true); + + using (var httpClientHandler = new HttpClientHandler()) + { + httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; + using (var httpClient = new HttpClient(httpClientHandler)) + { + // Make your request... + // var httpClient = new HttpClient(); + // The port number(5001) must match the port of the gRPC server. + //httpClient.BaseAddress = new Uri("http://localhost:5103"); + httpClient.BaseAddress = new Uri("http://localhost:5580"); + //httpClient.DefaultRequestVersion = Version.Parse("2.0"); + var client = GrpcClient.Create(httpClient); + var reply = await client.GetBasketByIdAsync( + new BasketRequest { Id = "11" }); + Console.WriteLine("Greeting: " + reply.Buyerid); + Console.WriteLine("Press any key to exit..."); + Console.ReadKey(); + } + } + } + } +} diff --git a/src/Clients/Clients.Grpc.Caller/Protos/basket.proto b/src/Clients/Clients.Grpc.Caller/Protos/basket.proto new file mode 100644 index 000000000..1fc22bc99 --- /dev/null +++ b/src/Clients/Clients.Grpc.Caller/Protos/basket.proto @@ -0,0 +1,34 @@ +syntax = "proto3"; + +option csharp_namespace = "GrpcBasket"; + +package BasketApi; + +service Basket { + rpc GetBasketById(BasketRequest) returns (CustomerBasketResponse); + rpc UpdateBasket(CustomerBasketRequest) returns (CustomerBasketResponse); +} + +message BasketRequest { + string id = 1; +} + +message CustomerBasketRequest { + string buyerid = 1; + repeated BasketItemResponse items = 2; +} + +message CustomerBasketResponse { + string buyerid = 1; + repeated BasketItemResponse items = 2; +} + +message BasketItemResponse { + string id = 1; + string productid = 2; + string productname = 3; + double unitprice = 4; + double oldunitprice = 5; + int32 quantity = 6; + string pictureurl = 7; +} \ No newline at end of file diff --git a/src/Services/Basket/Basket.API/Basket.API.csproj b/src/Services/Basket/Basket.API/Basket.API.csproj index 78cd14b2a..4ba0e19b4 100644 --- a/src/Services/Basket/Basket.API/Basket.API.csproj +++ b/src/Services/Basket/Basket.API/Basket.API.csproj @@ -27,11 +27,11 @@ - + - - + + diff --git a/src/Services/Basket/Basket.API/Grpc/BasketService.cs b/src/Services/Basket/Basket.API/Grpc/BasketService.cs index f6bd42ce7..a77ae080e 100644 --- a/src/Services/Basket/Basket.API/Grpc/BasketService.cs +++ b/src/Services/Basket/Basket.API/Grpc/BasketService.cs @@ -1,10 +1,12 @@ using Grpc.Core; +using Microsoft.AspNetCore.Authorization; using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.Extensions.Logging; using System.Linq; using System.Threading.Tasks; +using GrpcBasket; -namespace grpc +namespace GrpcBasket { public class BasketService : Basket.BasketBase { @@ -17,24 +19,47 @@ namespace grpc _logger = logger; } - public override async Task GetBasketById(BasketRequest request, ServerCallContext context) + [AllowAnonymous] + public override Task GetBasketById(BasketRequest request, ServerCallContext context) { _logger.LogInformation($"Begin grpc call from method {context.Method} for basket id {request.Id}"); - var data = await _repository.GetBasketAsync(request.Id); + //context.ResponseTrailers.Add("grpc-status", "0"); + //context.Status = Status.DefaultSuccess; - if (data != null) + return Task.FromResult(new CustomerBasketResponse { - context.Status = new Status(StatusCode.OK, $"Basket with id {request.Id} do exist"); + Buyerid = "55" + }); - return MapToCustomerBasketResponse(data); - } - else - { - context.Status = new Status(StatusCode.NotFound, $"Basket with id {request.Id} do not exist"); - } - return null; + // if (!context.Response.SupportsTrailers()) + // { + // var headers = new HeaderDictionary(); + // headers.Add("grpc-status", "0"); + + // Log.Logger.Information("Custom middleware headers {@headers}", headers); + // context.Features.Set(new TestHttpResponseTrailersFeature + // { + // Trailers = headers + // }); + // } + + // return next(); + // var data = await _repository.GetBasketAsync(request.Id); + + // if (data != null) + // { + // context.Status = new Status(StatusCode.OK, $"Basket with id {request.Id} do exist"); + + // return MapToCustomerBasketResponse(data); + // } + // else + // { + // context.Status = new Status(StatusCode.NotFound, $"Basket with id {request.Id} do not exist"); + // } + + // return new CustomerBasketResponse(); } public override async Task UpdateBasket(CustomerBasketRequest request, ServerCallContext context) @@ -62,16 +87,16 @@ namespace grpc Buyerid = customerBasket.BuyerId }; - customerBasket.Items.ForEach(item => response.Items.Add(new BasketItemResponse - { - Id = item.Id, - Oldunitprice = (double)item.OldUnitPrice, - Pictureurl = item.PictureUrl, - Productid = item.ProductId, - Productname = item.ProductName, - Quantity = item.Quantity, - Unitprice = (double)item.UnitPrice - })); + // customerBasket.Items.ForEach(item => response.Items.Add(new BasketItemResponse + // { + // Id = item.Id, + // Oldunitprice = (double)item.OldUnitPrice, + // Pictureurl = item.PictureUrl, + // Productid = item.ProductId, + // Productname = item.ProductName, + // Quantity = item.Quantity, + // Unitprice = (double)item.UnitPrice + // })); return response; } @@ -83,16 +108,16 @@ namespace grpc BuyerId = customerBasketRequest.Buyerid }; - customerBasketRequest.Items.ToList().ForEach(item => response.Items.Add(new BasketItem - { - Id = item.Id, - OldUnitPrice = (decimal)item.Oldunitprice, - PictureUrl = item.Pictureurl, - ProductId = item.Productid, - ProductName = item.Productname, - Quantity = item.Quantity, - UnitPrice = (decimal)item.Unitprice - })); + // customerBasketRequest.Items.ToList().ForEach(item => response.Items.Add(new BasketItem + // { + // Id = item.Id, + // OldUnitPrice = (decimal)item.Oldunitprice, + // PictureUrl = item.Pictureurl, + // ProductId = item.Productid, + // ProductName = item.Productname, + // Quantity = item.Quantity, + // UnitPrice = (decimal)item.Unitprice + // })); return response; } diff --git a/src/Services/Basket/Basket.API/Program.cs b/src/Services/Basket/Basket.API/Program.cs index caf3d581b..de1b54c2b 100644 --- a/src/Services/Basket/Basket.API/Program.cs +++ b/src/Services/Basket/Basket.API/Program.cs @@ -49,7 +49,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API .ConfigureKestrel(options => { var ports = GetDefinedPorts(configuration); - options.Listen(IPAddress.Any, ports.httpPort, listenOptions => { listenOptions.Protocols = HttpProtocols.Http1AndHttp2; @@ -59,6 +58,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API { listenOptions.Protocols = HttpProtocols.Http2; }); + }) .UseFailing(options => options.ConfigPath = "/Failing") .UseStartup() diff --git a/src/Services/Basket/Basket.API/Proto/basket.proto b/src/Services/Basket/Basket.API/Proto/basket.proto index 9b69effad..0ba9315df 100644 --- a/src/Services/Basket/Basket.API/Proto/basket.proto +++ b/src/Services/Basket/Basket.API/Proto/basket.proto @@ -1,15 +1,11 @@ syntax = "proto3"; -option csharp_namespace = "grpc"; +option csharp_namespace = "GrpcBasket"; package BasketApi; service Basket { - rpc GetBasketById(BasketRequest) returns (CustomerBasketResponse) { - option (google.api.http) = { - get: "/GetBasketById" - }; - } + rpc GetBasketById(BasketRequest) returns (CustomerBasketResponse) {} rpc UpdateBasket(CustomerBasketRequest) returns (CustomerBasketResponse) {} } @@ -19,12 +15,10 @@ message BasketRequest { message CustomerBasketRequest { string buyerid = 1; - repeated BasketItemResponse items = 2; } message CustomerBasketResponse { string buyerid = 1; - repeated BasketItemResponse items = 2; } message BasketItemResponse { diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 0176ea936..c38fb66b7 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -4,7 +4,6 @@ using Basket.API.Infrastructure.Filters; using Basket.API.Infrastructure.Middlewares; using Basket.API.IntegrationEvents.EventHandling; using Basket.API.IntegrationEvents.Events; -using grpc; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; @@ -34,6 +33,9 @@ using System; using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.IO; +using GrpcBasket; +using Microsoft.AspNetCore.Http.Features; +using Serilog; namespace Microsoft.eShopOnContainers.Services.Basket.API { @@ -198,39 +200,62 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API app.UsePathBase(pathBase); } - app.UseSwagger() - .UseSwaggerUI(setup => - { - setup.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1"); - setup.OAuthClientId("basketswaggerui"); - setup.OAuthAppName("Basket Swagger UI"); - }); + + //app.Use((context, next) => + //{ + // Log.Logger.Information("Custom middleware to avoid trailers"); + // Log.Logger.Information("Custom middleware context.Response {@context.Response}", context.Response); + // Log.Logger.Information("Custom middleware context.Response.SupportsTrailers {context.Response.SupportsTrailers}", context.Response.SupportsTrailers()); + // if (!context.Response.SupportsTrailers()) + // { + // var headers = new HeaderDictionary(); + // headers.Add("grpc-status", "0"); + + // Log.Logger.Information("Custom middleware headers {@headers}", headers); + // context.Features.Set(new TestHttpResponseTrailersFeature + // { + // Trailers = headers + // }); + // } + + // return next(); + + + //}); + + //app.UseSwagger() + // .UseSwaggerUI(setup => + // { + // setup.SwaggerEndpoint($"{ (!string.IsNullOrEmpty(pathBase) ? pathBase : string.Empty) }/swagger/v1/swagger.json", "Basket.API V1"); + // setup.OAuthClientId("basketswaggerui"); + // setup.OAuthAppName("Basket Swagger UI"); + // }); app.UseRouting(); - ConfigureAuth(app); + // ConfigureAuth(app); - app.UseStaticFiles(); + // app.UseStaticFiles(); app.UseCors("CorsPolicy"); app.UseEndpoints(endpoints => { + endpoints.MapGrpcService(); endpoints.MapDefaultControllerRoute(); endpoints.MapControllers(); - endpoints.MapGet("/_proto/", async ctx => - { - ctx.Response.ContentType = "text/plain"; - using var fs = new FileStream(Path.Combine(env.ContentRootPath, "Proto", "basket.proto"), FileMode.Open, FileAccess.Read); - using var sr = new StreamReader(fs); - while (!sr.EndOfStream) - { - var line = await sr.ReadLineAsync(); - if (line != "/* >>" || line != "<< */") - { - await ctx.Response.WriteAsync(line); - } - } - }); - endpoints.MapGrpcService(); + // endpoints.MapGet("/_proto/", async ctx => + // { + // ctx.Response.ContentType = "text/plain"; + // using var fs = new FileStream(Path.Combine(env.ContentRootPath, "Proto", "basket.proto"), FileMode.Open, FileAccess.Read); + // using var sr = new StreamReader(fs); + // while (!sr.EndOfStream) + // { + // var line = await sr.ReadLineAsync(); + // if (line != "/* >>" || line != "<< */") + // { + // await ctx.Response.WriteAsync(line); + // } + // } + // }); endpoints.MapHealthChecks("/hc", new HealthCheckOptions() { Predicate = _ => true, @@ -254,21 +279,21 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API private void ConfigureAuthService(IServiceCollection services) { // prevent from mapping "sub" claim to nameidentifier. - JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); + // JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove("sub"); - var identityUrl = Configuration.GetValue("IdentityUrl"); + // var identityUrl = Configuration.GetValue("IdentityUrl"); - services.AddAuthentication(options => - { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + // services.AddAuthentication(options => + // { + // options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + // options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - }).AddJwtBearer(options => - { - options.Authority = identityUrl; - options.RequireHttpsMetadata = false; - options.Audience = "basket"; - }); + // }).AddJwtBearer(options => + // { + // options.Authority = identityUrl; + // options.RequireHttpsMetadata = false; + // options.Audience = "basket"; + // }); } protected virtual void ConfigureAuth(IApplicationBuilder app) @@ -278,8 +303,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API app.UseMiddleware(); } - app.UseAuthentication(); - app.UseAuthorization(); + // app.UseAuthentication(); + // app.UseAuthorization(); } private void RegisterEventBus(IServiceCollection services) diff --git a/src/Services/Basket/Basket.API/TestHttpResponseTrailersFeature.cs b/src/Services/Basket/Basket.API/TestHttpResponseTrailersFeature.cs new file mode 100644 index 000000000..7cab5a07b --- /dev/null +++ b/src/Services/Basket/Basket.API/TestHttpResponseTrailersFeature.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; + +namespace Microsoft.eShopOnContainers.Services.Basket.API +{ + internal class TestHttpResponseTrailersFeature : IHttpResponseTrailersFeature + { + public IHeaderDictionary Trailers { get; set; } + } +} \ No newline at end of file diff --git a/src/_build/dependencies.props b/src/_build/dependencies.props index e7941d51b..1905ccd41 100644 --- a/src/_build/dependencies.props +++ b/src/_build/dependencies.props @@ -16,9 +16,9 @@ - 0.1.22-pre1 - 3.9.0-rc1 - 1.22.0 + 0.1.22-pre3 + 3.9.1 + 2.23.0 0.1.22-pre2 3.0.0-preview7.19362.4