Close

Custom Rules

IvanSlater
9 years ago
#8412 Quote
Avatar
  • 89
Hi. Thanks!!

Can you show me a sample, please?

Ivan.
iliyan.tanev
9 years ago
#8414 Quote
Avatar
  • 347
Hi,

This is your code and its ok if you are using nopCommerce below 3.4


public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
     builder.RegisterType<Reminder1Service>().As<IReminderRule>().InstancePerHttpRequest();
     builder.RegisterType<Reminder2Service>().As<IReminderRule>().InstancePerHttpRequest();
}


If you are using 3.4 or above the types are registered nop per http request but per lifetime scope.


public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
     builder.RegisterType<Reminder1Service>().As<IReminderRule>().InstancePerLifetimeScope();
     builder.RegisterType<Reminder2Service>().As<IReminderRule>().InstancePerLifetimeScope();
}


The difference is minor, but it could become reason for some unexpected behaviour.
Regards,
Iliyan Tanev
Nop-Templates Dev Team
IvanSlater
9 years ago
#8491 Quote
Avatar
  • 89
Is there a way to clear or truncate all messages sent? From Nop Admin (3.3).
IvanSlater
9 years ago
#8492 Quote
Avatar
  • 89
Btw, Im getting this error when click Run Now from customer reminder task, in task manager:

Exception type: ObjectDisposedException 
    Exception message: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.
   at Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed()

Im using InstancePerHttpRequest in DependencyRegistrar class (nop 3.3)

I have uninstalled and reinstalled my plugin, but same error.

help!
iliyan.tanev
9 years ago
#8496 Quote
Avatar
  • 347
IvanSlater wrote:
Is there a way to clear or truncate all messages sent? From Nop Admin (3.3).


You can delete them from the database table SS_CR_CustomerReminderMessageRecord.

About your Autofac issue - refer to this to understand the reason. 
Review your Dependency Register. You are probably registering your reminder wrong.
Regards,
Iliyan Tanev
Nop-Templates Dev Team
IvanSlater
9 years ago
#8498 Quote
Avatar
  • 89
Hi!

This is my DependencyRegistrar.cs


namespace Nop.Plugin.Misc.NewReminderRules
{
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<MakeCommentRulesService>().As<IReminderRule>().InstancePerHttpRequest();
            builder.RegisterType<BuyMoreRulesService>().As<IReminderRule>().InstancePerHttpRequest();
        }




        public int Order
        {
            get { return 0; }
        }
    }
}


Is anything wrong?
IvanSlater
9 years ago
#8569 Quote
Avatar
  • 89
@iliyan.tanev

can you help?

Thanks!
iliyan.tanev
9 years ago
#8576 Quote
Avatar
  • 347
Hi,

Sorry for the late response and Happy new Year!

I don't see anything wrong with your Dependency Register.

The problem is from the Autofac and it happens when you try to resolve the registered dependency. According to the issue discussion I send you the problem happens due to the disposal of the scope on EndRequest. Maybe the it has something to do with the resolution of the registered dependencies. Do you use them in some caching function ?
Regards,
Iliyan Tanev
Nop-Templates Dev Team
IvanSlater
9 years ago
#8615 Quote
Avatar
  • 89
Hi.

Im not using Autofac. My custome role makes simple processing justing for testing.

This is the code:


        public virtual IList<CustomerReminderInfo> GetCustomerReminderInfos(TimeSpan conditionMetDataEarlierThan, TimeSpan conditionMetDateLaterThan, int storeId)
        {
            try
            {
                var customerReminderInfos = new List<CustomerReminderInfo>();
                var finishedOrders = from a in _orderRepository.Table
                                     join b in _shipmentRepository.Table on a.Id equals b.OrderId
                                     join c in _orderItemRepository.Table on a.Id equals c.OrderId
                                     where b.DeliveryDateUtc != null
                                        && a.OrderStatusId == 30
                                        && DateTime.Now > DbFunctions.AddMinutes(b.DeliveryDateUtc, (int)conditionMetDataEarlierThan.TotalMinutes)
                                        && b.DeliveryDateUtc >= DbFunctions.AddMinutes(DateTime.Now, (int)conditionMetDateLaterThan.TotalMinutes * -1)
                                        && _productReviewRepository.Table.Where(p => p.CustomerId == a.CustomerId && p.ProductId == c.ProductId).Count() == 0
                                     select new { Order = a, Data = (DateTime)b.DeliveryDateUtc };




                foreach (var order in finishedOrders)
                {
                    var customerReminderInfo = new CustomerReminderInfo()
                    {
                        Customer = order.Order.Customer,
                        ReminderMessageId = order.Order.Id,
                        RuleConditionMetDate = order.Data,
                        Tokens = new List<Token>(),
                        StoreId = storeId
                    };
                    customerReminderInfos.Add(customerReminderInfo);
                }




                return customerReminderInfos;
            }
            catch (Exception ex)
            {
                _loggerService.InsertLog(LogLevel.Error, ex.Message, ex.StackTrace);
                return null;
            }
        }
iliyan.tanev
9 years ago
#8617 Quote
Avatar
  • 347
Hi,

Autofac is used for Dependency resolving. It is the container that holds and from which you resolve your dependencies. So you must use it in order for your dependency register to work.
Regards,
Iliyan Tanev
Nop-Templates Dev Team