Exemple #1
0
        public ProfessorsController(ILogger <ProfessorsController> logger, IConfiguration config)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            _config = config ?? throw new ArgumentNullException(nameof(config));

            _collegeServiceClient = CollegeServiceClientHelper.GetCollegeServiceClient(_config["RPCService:ServiceUrl"]);
        }
        public GrpcProfessorController(ILogger <GrpcProfessorController> logger, IConfiguration config)
        {
            _logger = logger;

            _config = config;

            _collegeServiceClient = CollegeServiceClientHelper.GetCollegeServiceClient(_config["RPCService:ServiceUrl"]);
        }
        static public CollegeServiceClient GetCollegeServiceClient(string serviceUrl)
        {
            if (_client == null)
            {
                var channel = GrpcChannel.ForAddress(serviceUrl);
                _client = new CollegeServiceClient(channel);
            }

            return(_client);
        }
        public FormAddNewProfessor()
        {
            InitializeComponent();

            _config = new ConfigurationBuilder()
                      .SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json").Build();

            textBoxName.Text = NameGenerator.GenerateName(12);

            _client = CollegeServiceClientHelper.GetCollegeServiceClient(_config["RPCService:ServiceUrl"]);
        }
Exemple #5
0
        public static CollegeServiceClient GetCollegeServiceClient(string serviceUrl)
        {
            if (_client == null)
            {
                var httpHandler = new HttpClientHandler();
                httpHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true;
                var httpClient = new HttpClient(httpHandler);

                var channel = GrpcChannel.ForAddress(serviceUrl, new GrpcChannelOptions {
                    HttpClient = httpClient
                });
                _client = new CollegeServiceClient(channel);
            }

            return(_client);
        }
        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();
        }
Exemple #7
0
        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"]);

            // 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();
        }