public override async Task RetrieveAddressEnrollments(AddressEnrollmentRequest request, IServerStreamWriter <AddressEnrollmentResponse> responseStream, ServerCallContext context) { var studentEnrollments = await _addressBll.RetrieveAddressEnrollments(Guid.Parse(request.StudentId)); foreach (var enrollment in studentEnrollments) { Console.WriteLine($"{enrollment.Enrollment} {enrollment.EnrollmentStatus}"); var addressEnrollment = new AddressEnrollmentResponse { StudentId = enrollment.StudentId.ToString(), Name = enrollment.Name, FullAddress = enrollment.FullAddress, Enrollment = enrollment.Enrollment, EnrollmentStatus = enrollment.EnrollmentStatus }; await responseStream.WriteAsync(addressEnrollment); } }
static async Task Main(string[] args) { string response = "Y"; _config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json").Build(); // gRPC Clients CollegeServiceClient CollegeClient = CollegeServiceClientHelper.GetCollegeServiceClient(_config["RPCService:ServiceUrl"]); AddressBookServerClient AddressClient = AddressServiceClientHelper.GetAddressBookServerClient(_config["RPCService:ServiceUrl"]); /* * var cts = new CancellationTokenSource(TimeSpan.FromSeconds(50)); * * using var replies = AddressClient.GetWeatherStream(new Empty(), cancellationToken: cts.Token); * * try * { * await foreach (var weatherData in replies.ResponseStream.ReadAllAsync(cancellationToken: cts.Token)) * { * Console.WriteLine($"{weatherData.DateTimeStamp.ToDateTime():s} | {weatherData.Summary} | {weatherData.TemperatureC} C"); * } * } * catch (RpcException ex) when (ex.StatusCode == StatusCode.Cancelled) * { * Console.WriteLine("Stream cancelled."); * } */ // Address Enrollments Client Request for Server Streaming. var studentEnrollmentsRequest = new AddressEnrollmentRequest { StudentId = "3698FAED-83A1-4700-89EE-CAFE7716833E" }; using var studentEnrollments = AddressClient.RetrieveAddressEnrollments(studentEnrollmentsRequest); try { await foreach (var studentEnrollment in studentEnrollments.ResponseStream.ReadAllAsync()) { Console.WriteLine($"{studentEnrollment.StudentId} {studentEnrollment.Name} {studentEnrollment.Enrollment} {studentEnrollment.EnrollmentStatus}"); } } catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled) { Console.WriteLine("Stream cancelled."); } // Address Enrollments Client Side Stream var userAddress = new AddAddressRequest { StudentId = Guid.NewGuid().ToString(), /* To Be replaced with Students's Id*/ Name = NameGenerator.GenerateName(12), FullAddress = AddressGenerator.GenerateAddress(), Enrollment = Konstants.AddressConstants.EnrollmentTypes[RandomNumberGenerator.GetRandomValue(1, 4)] }; using (var stream = AddressClient.AddAddressEnrollments()) { foreach (string enrollmentType in Konstants.AddressConstants.EnrollmentTypes) { userAddress.Enrollment = enrollmentType; await stream.RequestStream.WriteAsync(userAddress); } await stream.RequestStream.CompleteAsync(); await stream; WriteLine($"Sent All"); } /* * WriteLine("\n\nCreating New Professor ..."); * while (response == "Y") * { * // Add New Professor * AddProfessorRequest professorNew = GenerateNewProfessor(); * * var newlyAddedProfessor = await Client.AddProfessorAsync(professorNew); * WriteLine($"\n\nNew Professor Added with Professor Id: {newlyAddedProfessor.ProfessorId}"); * * WriteLine("\n\nDo you want to create New Professor: {Y/N}"); * response = ReadKey().KeyChar.ToString().ToUpper(); * } */ response = "Y"; while (response == "Y") { WriteLine("\n\nPlease enter a Professor Id: "); var professorId = ReadLine(); // Retrieve Single Row var professorRequest = new GetProfessorRequest { ProfessorId = professorId }; var professor = await CollegeClient.GetProfessorByIdAsync(professorRequest); DisplayProfessorDetails(professor); WriteLine("\n\nDo you want to Lookup a Professor: {Y/N}"); response = ReadKey().KeyChar.ToString().ToUpper(); } // Address Service gRPC var userAddress1 = new AddAddressRequest { StudentId = Guid.NewGuid().ToString(), /* To Be replaced with Students's Id*/ Name = NameGenerator.GenerateName(12), FullAddress = AddressGenerator.GenerateAddress(), Enrollment = Konstants.AddressConstants.EnrollmentTypes[RandomNumberGenerator.GetRandomValue(1, 4)] }; var newAddress = await AddressClient.AddAddressAsync(userAddress1); WriteLine($"Address Data with Id: {newAddress.Id}"); /* * response = "Y"; * while (response == "Y") * { * // Retrieve Multiple Rows * var professors = await Client.GetAllProfessorsAsync(new Empty()); * * foreach (var prof in professors.Professors) * { * DisplayProfessorDetails(prof); * } * * WriteLine("\n\nDo you want to retrieve all professors: {Y/N}"); * response = ReadKey().KeyChar.ToString().ToUpper(); * } */ WriteLine("\n\nThank You for using the application. \n\nPress any key ..."); ReadKey(); }
static async Task Main(string[] args) { _config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json").Build(); var _collegeClient = CollegeServiceClientHelper.GetCollegeServiceClient(_config["RPCService:ServiceUrl"]); var _addressClient = AddressServiceClientHelper.GetAddressBookServerClient(_config["RPCService:ServiceUrl"]); var jwtToken = new JwtAccessToken(); if (jwtToken.Expiration < DateTime.Now) { jwtToken = GetTokenFromAuth0(); } var headers = new Metadata { { "Authorization", $"Bearer {jwtToken.Access_Token}" } }; DisplayHeader("Retrieve Single Row ..."); WriteLine("\n\nPlease enter a Professor Id: "); var professorId = ReadLine(); var professorRequest = new GetProfessorRequest { ProfessorId = professorId }; var professor = await _collegeClient.GetProfessorByIdAsync(professorRequest, headers : headers); DisplayProfessorDetails(professor); DisplayFooter(); //**************** ADDRESS BOOK CLIENT SIDE STREAMING ****************// var userAddress = new AddAddressRequest { StudentId = Guid.NewGuid().ToString(), /* To Be replaced with Students's Id*/ Name = NameGenerator.GenerateName(12), FullAddress = AddressGenerator.GenerateAddress(), Enrollment = AddressConstants.EnrollmentTypes[RandomNumberGenerator.GetRandomValue(1, 4)] }; using (var stream = _addressClient.AddAddressEnrollments()) { foreach (string enrollmentType in AddressConstants.EnrollmentTypes) { userAddress.Enrollment = enrollmentType; await stream.RequestStream.WriteAsync(userAddress).ConfigureAwait(false); } await stream.RequestStream.CompleteAsync().ConfigureAwait(false); await stream; WriteLine($"Sent All"); } //**************** ADDRESS BOOK CLIENT SIDE STREAMING ****************// //**************** ADDRESS BOOK SERVER SIDE STREAMING ****************// var studentEnrollmentsRequest = new AddressEnrollmentRequest { StudentId = "62b76fc7-46d9-4d1c-a246-7f4083a87634" }; using var studentEnrollments = _addressClient .RetrieveAddressEnrollments(studentEnrollmentsRequest); try { await foreach (var studentEnrollment in studentEnrollments.ResponseStream.ReadAllAsync()) { WriteLine($"{studentEnrollment.StudentId} | {studentEnrollment.Name} | {studentEnrollment.Enrollment} | {studentEnrollment.EnrollmentStatus}"); } } catch (RpcException ex) when(ex.StatusCode == StatusCode.Cancelled) { Console.WriteLine("Stream cancelled."); } //**************** ADDRESS BOOK SERVER SIDE STREAMING ****************// /* * DisplayHeader("Creating New Professor ..."); * NewProfessorRequest professorNew = GenerateNewProfessor(); * var newlyAddedProfessor = await _collegeClient.AddProfessorAsync(professorNew); * WriteLine($"\n\nNew Professor Added with Professor Id: {newlyAddedProfessor.ProfessorId}"); * DisplayFooter(); */ //DisplayHeader("Retrieve Single Row ..."); //WriteLine("\n\nPlease enter a Professor Id: "); //var professorId = ReadLine(); //var professorRequest = new GetProfessorRequest { ProfessorId = professorId }; //var professor = await _collegeClient.GetProfessorByIdAsync(professorRequest); //DisplayProfessorDetails(professor); //DisplayFooter(); DisplayHeader("Retrieve All Rows ..."); var professors = await _collegeClient.GetAllProfessorsAsync(new Empty()); foreach (var prof in professors.Professors) { DisplayProfessorDetails(prof); } WriteLine("\n\nThank You for using the application. \n\nPress any key ..."); ReadKey(); }