Browse Source

added public product grpc service

pull/75/head
Galip Tolga Erdem 5 years ago
parent
commit
02e76034b6
  1. 26
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs
  2. 12
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj
  3. 26
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs
  4. 21
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto
  5. 9
      shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs

26
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs

@ -7,9 +7,12 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using EShopOnAbp.CatalogService.Grpc;
using EShopOnAbp.CatalogService.MongoDB; using EShopOnAbp.CatalogService.MongoDB;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
@ -80,6 +83,10 @@ public class CatalogServiceHttpApiHostModule : AbpModule
}); });
Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; }); Configure<AbpAntiForgeryOptions>(options => { options.AutoValidate = false; });
context.Services.AddGrpc(options =>
{
options.EnableDetailedErrors = true;
});
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override void OnApplicationInitialization(ApplicationInitializationContext context)
@ -111,7 +118,24 @@ public class CatalogServiceHttpApiHostModule : AbpModule
app.UseAbpSerilogEnrichers(); app.UseAbpSerilogEnrichers();
app.UseAuditing(); app.UseAuditing();
app.UseUnitOfWork(); app.UseUnitOfWork();
app.UseConfiguredEndpoints(); app.UseConfiguredEndpoints(endpoints =>
{
endpoints.MapGrpcService<PublicProductGrpService>();
endpoints.MapGet("/_proto/", async ctx =>
{
ctx.Response.ContentType = "text/plain";
using var fs = new FileStream(Path.Combine(env.ContentRootPath, "Protos", "product.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);
}
}
});
});
} }
public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context) public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)

12
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj

@ -18,6 +18,18 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\..\..\shared\EShopOnAbp.Shared.Hosting.Microservices\EShopOnAbp.Shared.Hosting.Microservices.csproj" /> <ProjectReference Include="..\..\..\..\shared\EShopOnAbp.Shared.Hosting.Microservices\EShopOnAbp.Shared.Hosting.Microservices.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.19.3" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.42.0" />
<PackageReference Include="Grpc.Tools" Version="2.42.0" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<Protobuf Include="Protos\product.proto" GrpcServices="Server" Generator="MSBuild:Compile" />
<Content Include="@(Protobuf)" />
<None Remove="@(Protobuf)" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Remove="Logs\**" /> <Compile Remove="Logs\**" />

26
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Grpc/PublicProductGrpService.cs

@ -0,0 +1,26 @@
using System;
using System.Threading.Tasks;
using EShopOnAbp.CatalogService.Products;
using Grpc.Core;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
namespace EShopOnAbp.CatalogService.Grpc;
public class PublicProductGrpService : ProductPublic.ProductPublicBase
{
private readonly IRepository<Product, Guid> _productRepository;
private readonly IObjectMapper _objectMapper;
public PublicProductGrpService(IRepository<Product, Guid> productRepository, IObjectMapper objectMapper)
{
_productRepository = productRepository;
_objectMapper = objectMapper;
}
public override async Task<ProductDto> GetAsync(ProductRequest request, ServerCallContext context)
{
var product = await _productRepository.GetAsync(Guid.Parse(request.Id));
return _objectMapper.Map<Product, ProductDto>(product);
}
}

21
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Protos/product.proto

@ -0,0 +1,21 @@
syntax = "proto3";
option csharp_namespace = "EShopOnAbp.CatalogService.Grpc";
package CatalogApi;
service ProductPublic {
rpc GetAsync(ProductRequest) returns (ProductDto) {}
}
message ProductRequest {
string id = 1;
}
message ProductDto {
string id = 1;
string code = 2;
string name = 3;
float price = 4;
int32 stockCount = 5;
}

9
shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs

@ -1,5 +1,7 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Serilog; using Serilog;
@ -13,6 +15,13 @@ public static class ApplicationBuilderHelper
where TStartupModule : IAbpModule where TStartupModule : IAbpModule
{ {
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(serverOptions =>
{
serverOptions.ConfigureEndpointDefaults(listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
});
});
builder.Host builder.Host
.AddAppSettingsSecretsJson() .AddAppSettingsSecretsJson()
.UseAutofac() .UseAutofac()

Loading…
Cancel
Save