Plugin with Scheduled Task

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 年 前
Hi All,

I have written a plugin with the normal config page that has 2 buttons that run a couple of tasks. This all works fine.

I am trying to get these tasks to also be triggered by a scheduled task. The Scheduled task gets registered an runs, but throws an error when trying to run the services.

Bit about the project.
I have three files IMYImportService.cs and MYImportService.cs which do the work, and work perfectly when called from the config form. I have a third Serviceschedule.cs that has the schedule stuff in and tries to call the services to do the work.


I hope this explains my problem well enough, and someone can point me in the right direction

Thanks in advance.

Paul



The Error I get is on the Execute() method in Task.cs

Error while running the 'Update MY Stock' schedule task. None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Plugin.Feed.MYImport.Services.MYUpdateScheduler' can be invoked with the available services and parameters: Cannot resolve parameter 'Nop.Plugin.Feed.MYImport.Services.MYImportService MYImportService' of constructor 'Void .ctor(Nop.Plugin.Feed.MYImport.Services.MYImportService)'.

Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Plugin.Feed.MYImport.Services.MYUpdateScheduler' can be invoked with the available services and parameters: Cannot resolve parameter 'Nop.Plugin.Feed.MYImport.Services.MYImportService MYImportService' of constructor 'Void .ctor(Nop.Plugin.Feed.MYImport.Services.MYImportService)'. at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) at Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) at Autofac.ResolutionExtensions.TryResolve(IComponentContext context, Type serviceType, Object& instance) at Nop.Core.Infrastructure.DependencyManagement.ContainerManager.TryResolve(Type serviceType, Object& instance) in c:\..\Libraries\Nop.Core\Infrastructure\DependencyManagement\ContainerManager.cs:line 159 at Nop.Services.Tasks.Task.CreateTask() in c:\..\Libraries\Nop.Services\Tasks\Task.cs:line 42 at Nop.Services.Tasks.Task.Execute() in c:\..\Libraries\Nop.Services\Tasks\Task.cs:line 65
10 年 前
Hi Paul,

Does  Serviceschedule.cs inherit from ITask?
Can you post the constructor and execute method code
10 年 前
Hi,

This is my Schedule code
namespace Nop.Plugin.Feed.MYImport.Services
{
    /// <summary>
    /// Represents a task for keeping the site alive
    /// </summary>
    public partial class MYUpdateScheduler : ITask
    {
        private readonly MYImportService _MYImportService;

        public MYUpdateScheduler(MYImportService MYImportService)
        {
            this._MYImportService = MYImportService;
        }

        /// <summary>
        /// Executes a task
        /// </summary>
        public void Execute()
        {
            
            MYImportRecord MYImportRecord = _MYImportService.GetConfig();
            try
            {
                _MYImportService.doUpdateXMLImport(MYImportRecord.UpdateXMLURL);
            }
            catch (Exception)
            {
            }
            finally
            {
                MYImportRecord = null;
            }
            
        }
    }
    public partial class MYImportScheduler : ITask
    {
        private readonly MYImportService _MYImportService;

        public MYImportScheduler(MYImportService MYImportService)
        {
            this._MYImportService = MYImportService;
        }

        /// <summary>
        /// Executes a task
        /// </summary>
        public void Execute()
        {
            MYImportRecord MYImportRecord = _MYImportService.GetConfig();
            try
            {
                _MYImportService.doUpdateXMLImport(MYImportRecord.ImportXMLURL);
            }
            catch (Exception)
            {
            }
            finally
            {
                MYImportRecord = null;
            }
        }
    }
}


The constructor of the class that is called looks like. This works absolutely fine when called from the config form.
        public MYImportService(IRepository<MYImportRecord> MYRepository,
            ICacheManager cachManager, IProductService produtSevice,
            ICategoryService categoryService, IManufacturerService manfService,
            IUrlRecordService urlRecordService, IPictureService pictureService,
            IDiscountService discountService, IRepository<MYCategory> MYCategoryRepository)
        {
            this._MYRepository = MYRepository;
            this._cacheManager = cachManager;
            this._productService = produtSevice;
            this._categoryService = categoryService;
            this._manfService = manfService;
            this._urlRecordService = urlRecordService;
            this._pictureService = pictureService;
            this._discountService = discountService;
            this._MYCategoryRepository = MYCategoryRepository;
        }

Hope that will help a little.

Thanks for the response.

Paul
10 年 前
Hi Paul,

Try changing the _MYImportService to IMYImportService, not MYImportService

ie:

private readonly IMYImportService _MYImportService;

        public MYUpdateScheduler(IMYImportService myImportService)
        {
            this._MYImportService = myImportService;
        }
10 年 前
and the same for the importScheduler:

private readonly IMYImportService _MYImportService;

        public MYImportScheduler(IMYImportService myImportService)
        {
            this._MYImportService = myImportService;
        }
10 年 前
Thank you @[email protected]

Will give that a go and see where it takes me.


Paul
10 年 前
@[email protected] Thank you that did it.

Actually it makes perfect sense now, frustrated that I didn't see it before.

Thanks Again.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.