Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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