Skip to content

Instantly share code, notes, and snippets.

@tanaka-takayoshi
Created November 30, 2019 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaka-takayoshi/e5d0d287e55b2b5ea2ccc83f689b4549 to your computer and use it in GitHub Desktop.
Save tanaka-takayoshi/e5d0d287e55b2b5ea2ccc83f689b4549 to your computer and use it in GitHub Desktop.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Serilog;
namespace WebPortal.Pages
{
public class CouponModel : PageModel
{
[BindProperty]
public string Code { get; set; }
public bool Succeeded { get; set; } = false;
private static HttpClient httpClient = new HttpClient();
public void OnGet()
{
}
public void OnPost()
{
VerifyAsync().GetAwaiter().GetResult();
}
private async Task VerifyAsync()
{
Log.Logger.Information($"code={Code}");
var res = await httpClient.GetAsync($"http://promoservice/api/Promo?code={Code}");
var json = await res.Content.ReadAsStringAsync();
Log.Logger.Information(json);
var definition = new { verified = false , message =""};
var result = JsonConvert.DeserializeAnonymousType(json, definition);
if (!result.verified)
{
var e = new Exception(result.message);
throw e;
}
Succeeded = result.verified;
}
}
}
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;
namespace RPCService
{
public class GreeterService : Greeter.GreeterBase
{
public GreeterService()
{
}
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = Verify(request.Name)
});
}
private string Verify(string code)
{
return "S";
}
}
}
using System;
using System.Threading.Tasks;
using Grpc.Net.Client;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RPCService;
using Serilog;
namespace PromoService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PromoController : ControllerBase
{
public PromoController()
{
}
[HttpGet("Test")]
public string GetTest()
{
return "Test succeeded";
}
[HttpGet]
public async Task<VerificationResult> VerifyAsync([FromQuery]string code)
{
Log.Logger.Information($"Verification Request with code: {code}");
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var channel = GrpcChannel.ForAddress("http://rpcservice");
var client = new Greeter.GreeterClient(channel);
var reply = await client.SayHelloAsync(
new HelloRequest { Name = "VeifyClient" });
if (reply.Message.StartsWith("S") == true)
{
return new VerificationResult { Verified = true };
}
else
{
return new VerificationResult { Verified = false, Message = "Invalid coupon code" };
}
}
}
public class VerificationResult
{
public bool Verified { get; set; }
public string Message { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment