0

I 'm working on .NET WEB API project where I implemented with post method controller .

namespace MockWebAPI_with_JWT.Controllers
{
    public class CustomerController : ApiController
    {

        #region GetNonce
        [RequiredAttributes]
        [HttpPost]
        public IHttpActionResult GetNonce([FromBody] JObject customer)
        {
            var customerResponse = new CustomerResponse { };
            HttpResponseMessage responseMsg = new HttpResponseMessage();
            HttpClient httpClientforNonce = new HttpClient();
            dynamic custReq = customer;
            dynamic newReq = new JObject();
            IHttpActionResult response = null;

            if (custReq == null)
            {
                customerResponse.responseMsg.StatusCode = HttpStatusCode.BadRequest;
                response = ResponseMessage(customerResponse.responseMsg);
                return response;
            }
            //Customer Information from Self signed certificate
            CertificateValidation certificateValidation = new CertificateValidation();
            X509Certificate2 clientCertforCustomer = Request.GetClientCertificate();
            string custID = certificateValidation.CustomerInfoFromClientCert(clientCertforCustomer);
           
          //  newReq.id = custReq.id;
            newReq.id = custID;
            newReq.mpn = custReq.mpn;
            newReq.manufacturer =custReq.manufacturer;
            newReq.commands = new JArray();
            // newReq.commands = custReq.commands;

            bool isCustomerIdValid = false;
            bool custAuthorized = false;
            dynamic custCommand = null;
            MockWebAPI_with_JWT.ProxyAPIService.CustomerInfo customerDetails = new MockWebAPI_with_JWT.ProxyAPIService.CustomerInfo();
            ProxyService proxyService = new ProxyService();
            proxyService.Url = "http://proxyapiwebservicetest.micron.com/ProxyService.svc";
           // MockWebAPI_with_JWT.ProxyAPIService.CustomerInfo customers = proxyService.GetCustomerInfo(custReq.id.ToString());
            MockWebAPI_with_JWT.ProxyAPIService.CustomerInfo customers = proxyService.GetCustomerMPNInfo(custID, custReq.mpn.ToString());

           // if (customers.CustomerID == custReq.id.ToString())
            if (customers.CustomerID == custID && customers.CustomerMPN == custReq.mpn.ToString())
            {
                
                isCustomerIdValid = true;
               // customerDetails = proxyService.GetCustomerCommands(custReq.id.ToString());
                customerDetails = proxyService.GetMPNCommands(custReq.mpn.ToString());
                foreach (var commandItem in customerDetails.Cust_Commands)
                {
                    custCommand = new JObject();
                    custCommand.command = commandItem.CommandName;
                    if (commandItem.CommandName.Contains("Nonce"))
                    {
                        custAuthorized = true;
                    }
                    custCommand.commandExp = (Int32)(DateTime.UtcNow.AddMonths(1).Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                    newReq.commands.Add(custCommand);
                }
            }
            // if credentials are valid, create JWT
            if (isCustomerIdValid && custAuthorized)
            {
                TokenGenerator keyProv = new TokenGenerator();
                string token = keyProv.GenerateToken(newReq, "GetNonce");

                if (string.IsNullOrEmpty(token))
                {
                    customerResponse.responseMsg.StatusCode = HttpStatusCode.BadRequest;
                    response = ResponseMessage(customerResponse.responseMsg);
                    return response;
                }
                else
                {   //call Rambus method - GET Nonce  
                    RequestHandler reqHandler = new RequestHandler();
                    string result = "";
                    result = reqHandler.SendGetRequestToKMS(newReq, token, "Nonce");

                    if (!string.IsNullOrEmpty(result))
                    {
                        string[] strResponse = result.Split('|');
                        if (strResponse[0].ToUpper() == "OK")
                        {
                            customerResponse.responseMsg.StatusCode = HttpStatusCode.OK;
                        }
                        else if (strResponse[0].ToUpper() == "UNAUTHORIZED")
                        {
                            customerResponse.responseMsg.StatusCode = HttpStatusCode.Unauthorized;
                        }
                        else if (strResponse[0].ToUpper() == "BADREQUEST")
                        {
                            customerResponse.responseMsg.StatusCode = HttpStatusCode.BadRequest;
                        }
                        else if (strResponse[0].ToUpper() == "INTERNALSERVERERROR")
                        {
                            customerResponse.responseMsg.StatusCode = HttpStatusCode.InternalServerError;
                        }
                        else
                        {
                            customerResponse.responseMsg.StatusCode = HttpStatusCode.BadRequest;
                        }
                        customerResponse.responseMsg.Content = new StringContent(strResponse[1]);
                        response = ResponseMessage(customerResponse.responseMsg);
                    }

                }
            }
            else
            {
                // if credentials are not valid send unauthorized status code in response
                customerResponse.responseMsg.StatusCode = HttpStatusCode.Unauthorized;
                response = ResponseMessage(customerResponse.responseMsg);
            }
            return response;
        }
        #endregion

when I tested (as single user at a same time )this GetNonce() method is working fine but when we trying to do load testing (multiple requests to GetNonce method at same time) then we are getting following error-

Response: status = 500; body = { 'Message': 'An error has occurred.', 'ExceptionMessage': 'Object reference not set to an instance of an object.', 'ExceptionType': 'System.NullReferenceException', 'StackTrace': ' at MockWebAPI_with_JWT.Controllers.CustomerController.GetNonce(JObject customer)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Filters.AuthorizationFilterAttribute.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()'}

Thanks for your help in advance.

Maldanna Gk
  • 155
  • 2
  • 11

0 Answers0