Base solution for your next web application
Open Closed

Background Job #5193


User avatar
0
pankajmathur created

Hi,

I am using Hangfire for background job.

public override void Execute(GeneratePayloadAndSendToMobileArgs generatePayloadAndSendToMobileArgs)
        {

            Task SendDataToMobileTask = Task.Run(() => SendDataToMobile(generatePayloadAndSendToMobileArgs));
            SendDataToMobileTask.Wait();
        }

private async Task SendDataToMobile(GeneratePayloadAndSendToMobileArgs generatePayloadAndSendToMobileArgs)
        {
            try
            {
                using (_abpSession.Use(generatePayloadAndSendToMobileArgs.TenantId, generatePayloadAndSendToMobileArgs.UserId))
                {
                    using (var unitOfWork = _unitOfWorkManager.Begin())
                    {
                        Dictionary<RetailObjectId, ICRUDAppService> CRUDAppServices = new Dictionary<RetailObjectId, ICRUDAppService>();
                        List<PayloadData> PayloadsToSend = new List<PayloadData>();
                        Dictionary<long, string> PayloadsToSave = new Dictionary<long, string>();

                        var Payloads = (from Rep in _ReplicationTransactionMobileRepository.GetAll()
                                        select new
                                        {
                                            SequenceNo = Rep.Id,
                                            SourceType = (RetailObjectId)Rep.SourceType,
                                            SourceKey = Rep.SourceKey,
                                            OperationType = (OperationType)Rep.OperationType,
                                            Payload = Rep.Payload,

                                        }).ToList().Take(500);



                        foreach (var item in Payloads)
                        {
                            PayloadData PayloadToSend = new PayloadData()
                            {
                                SequenceNo = item.SequenceNo,
                                SourceType = item.SourceType,
                                SourceKey = item.SourceKey,
                                OperationType = item.OperationType,
                            };

                            if (!string.IsNullOrEmpty(item.Payload))
                                PayloadToSend.Payload = GetPayload(item.SourceType, item.Payload);
                            else
                            {
                                string PayloadToSave = GeneratePayload(item.SequenceNo, item.SourceType, item.SourceKey, CRUDAppServices);
                                PayloadToSend.Payload = GetPayload(item.SourceType, PayloadToSave);
                                PayloadsToSave.Add(item.SequenceNo, PayloadToSave);
                            }

                            PayloadsToSend.Add(PayloadToSend);
                            PayloadResult obj = new PayloadResult();
                            obj.result = PayloadsToSend;


                            List<ConnectedClient> connectedClients = AppStatic.ConnectedClients.Where(p => p.TenantId == generatePayloadAndSendToMobileArgs.TenantId).ToList();
                            if (connectedClients.Count() > 0 && PayloadsToSend.Count > 0)
                            {
                                JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();
                                jsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                                await _iVend365HubContext.Clients.All.SendAsync("GetDeltaQueue", JsonConvert.SerializeObject(obj, jsonSerializerSettings));

                            }
                        }

                        //Delete the Successfully send records
                        _iVendRepository.ExecuteQuery("Delete From RepReplicationTransactionMobileDetail Where ProcessStatus = 1");
                        _iVendRepository.ExecuteQuery("Delete From RepReplicationTransactionMobile Where SequenceNo Not In (Select RepReplicationTransactionMobileSequenceNo From RepReplicationTransactionMobileDetail)");

                        //Save the newely generated Payload in DB
                        foreach (KeyValuePair<long, string> PayloadToSave in PayloadsToSave)
                        {
                            ReplicationTransactionMobile replicationTransactionMobile = _ReplicationTransactionMobileRepository.Get(PayloadToSave.Key);
                            replicationTransactionMobile.Payload = PayloadToSave.Value;
                            _ReplicationTransactionMobileRepository.Update(replicationTransactionMobile);
                        }


                        unitOfWork.Complete();

                    }
                }
            }
            catch (Exception ex)
            {

            }

        }

In the above code, at following line, it throws the error, "There is already an open DataReader associated with this Command which must be closed first." Any idea, what could be wrong here?

var Payloads = (from Rep in _ReplicationTransactionMobileRepository.GetAll()
                                        select new
                                        {
                                            SequenceNo = Rep.Id,
                                            SourceType = (RetailObjectId)Rep.SourceType,
                                            SourceKey = Rep.SourceKey,
                                            OperationType = (OperationType)Rep.OperationType,
                                            Payload = Rep.Payload,

                                        }).ToList().Take(500);

2 Answer(s)
  • User Avatar
    0
    pankajmathur created

    Hi,

    Please ignore this post...It was my mistake.....solved now....

  • User Avatar
    0
    alper created
    Support Team

    good to know it works ;)