OrderService is always NULL in production but not in Dev. environment

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 года назад
so result is not null, orderService is not null, but order is?  can you verify that order 3255 exists through yoursite.com/Admin/Order/Edit/3255 ?

or directly with SQL?
3 года назад
result.OrderNumber = 2751 which is the number of the order in NopCommerce. I get this from the order API in mollie. That is the first part the retrieveOrderTask.

Then we get to
IOrderService orderService = EngineContext.Current.Resolve<IOrderService>();
This is the one that is NULL

Full code:


[HttpPost]
        public IActionResult MollieWebHook(string id)
        {
            _logger.Information(id);

            Task<OrderResponse> retrieveOrderTask = Task.Run(() => (_mollieOrderClient.GetOrderAsync(id)));
            retrieveOrderTask.Wait();
            OrderResponse result = retrieveOrderTask.Result;

            _logger.Information(result.OrderNumber);

            IOrderService orderService = EngineContext.Current.Resolve<IOrderService>();

            _logger.Information("OrderService " + orderService.ToString());

            if (orderService == null)
                _logger.Warning("orderService object is null");

            Order order = orderService.GetOrderById(Convert.ToInt32(result.OrderNumber));

            if (order == null)
                _logger.Warning("Order object is null");

            order.PaymentStatus = MolliePaymentStatusToNopCommercePaymentStatus(result.Status);

            return Ok(200);
        }
3 года назад
Also the order does exist. I can see it in the backend also in DB (sql). That is not the problem. And As I said localhost works fine. So like you said it must be the async method. But I dont know.
3 года назад
odd, I don't know why EngineContext would not work.  Does it resolve if you temporarily comment out the async method and retrieve the order with a hard-coded Id?


[HttpPost]
        public IActionResult MollieWebHook(string id)
        {
            _logger.Information(id);

            //Task<OrderResponse> retrieveOrderTask = Task.Run(() => (_mollieOrderClient.GetOrderAsync(id)));
            //retrieveOrderTask.Wait();
            //OrderResponse result = retrieveOrderTask.Result;

            //_logger.Information(result.OrderNumber);


            IOrderService orderService = EngineContext.Current.Resolve<IOrderService>();

            _logger.Information("OrderService " + orderService.ToString());

            if (orderService == null)
                _logger.Warning("orderService object is null");

            Order order = orderService.GetOrderById(2751));

            if (order == null)
                _logger.Warning("Order object is null");

            order.PaymentStatus = MolliePaymentStatusToNopCommercePaymentStatus(result.Status);

            return Ok(200);
        }
3 года назад
Hardcoded test wouldt work. This method is being called by mollie after order placement. As a webhook. Anyways localhost object is perfectly set. Also when I produce an error before IOrderService i get a diferrent stacktrace. After not.

It certainly is odd. Could it be because this class derivese from BasePaymentController?

3 года назад
can you use their testmode?
https://docs.mollie.com/guides/testing

Outside of that, try my previous suggestion of converting the synchronous parts of the method (everything after the async call to Mollie) into a 2nd task, then await them both:
https://stackoverflow.com/questions/55647455/why-is-dispose-called-before-its-finishes-async-task
3 года назад
Alright once again thanks for the quick answer.

Like you said. I tried Task chaining. Wrapped the retrieve order in a Task and the GetOrder in a seperate task. Still the same result. Localhost working. Production version null ref. error. sadly.

New Method with Task Chaining:


[HttpPost]
        public IActionResult MollieWebHook(string id)
        {
            var retrieveOrderTask = RetrieveOrder(id);
            var GetOrderTask = retrieveOrderTask.ContinueWith(t => GetOrder(retrieveOrderTask));

            var rot = retrieveOrderTask.Result;
            var got = GetOrderTask.Result;

            if (rot == null || got == null)
                return NotFound(404);

            return Ok(200);
        }

        private Task<OrderResponse> RetrieveOrder(string id)
        {
            return _mollieOrderClient.GetOrderAsync(id);
        }

        private Task<Order> GetOrder(Task<OrderResponse> response)
        {
            return Task.Run(() =>
            {
                IOrderService orderService = EngineContext.Current.Resolve<IOrderService>();
                Order order = orderService.GetOrderById(Convert.ToInt32(response.Result.OrderNumber));
                order.PaymentStatus = MolliePaymentStatusToNopCommercePaymentStatus(response.Result.Status);

                return order;
            });
        }


object debug on local machine:

rot = {Mollie.Api.Models.Order.OrderResponse}
got = Id = 3371, Status = RanToCompletion, Method = "{null}", Result =
"Nop.Core.Domain.Orders.Order"
3 года назад
Tried the last part with live version.

I dont get any errors and Mollie says the plugin got called successfully in 0.2 - 0.4 seconds.
But the payment status is still unpayed. Wich means nothing changed. Only the method returns a 200 success code.
3 года назад
At last I went with Ydna his advice. I set the orderNumber in the callback url, so now I can properly set the orderStatus using OrderService and it works fine.

Not Ideal since I'm not able to verify the order with the mollie orderStatus.


[HttpPost]
        public virtual IActionResult MollieWebhook(int? orderId, string id)
        {
            if (!orderId.HasValue)
                return BadRequest(400);

            IOrderService orderService = EngineContext.Current.Resolve<IOrderService>();
            Order order = orderService.GetOrderById(orderId.Value);
            order.PaymentStatus = Core.Domain.Payments.PaymentStatus.Paid;

            return Ok(200);
        }
3 года назад
Bizarre, glad you got it working though.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.