简体   繁体   English

NopCommerce任务插件未启动任务

[英]NopCommerce Task Plugin not starting task

Using NopCommerce 3.8. 使用NopCommerce 3.8。

I have created a new Misc plugin. 我创建了一个新的杂项插件。 The purpose is to run a task that restfully calls my web api for data. 目的是运行一个任务,该任务彻底调用我的Web api来获取数据。

The following is my code: 以下是我的代码:

Plugin class 插件类

public class MereSyncPlugin : BasePlugin, IMiscPlugin
{
    private MereSyncObjectContext _context;
    private IScheduleTaskService _schedualTaskService;

    public MereSyncPlugin(MereSyncObjectContext context, IScheduleTaskService schedualTaskService)
    {
        _context = context;
        _schedualTaskService = schedualTaskService;
    }

    public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
    {
        actionName = "";
        controllerName = "";
        routeValues = new RouteValueDictionary()
        {
            { "Namespaces", "Nop.plugin.Misc.MereSync.Controllers" },
            { "area", null }
        };   
    }

    public override void Install()
    {
        //TODO: add more
        _schedualTaskService.InsertTask(new Nop.Core.Domain.Tasks.ScheduleTask()
        {
            Enabled = true,
            Name = "Product Sync",
            Seconds = 3600,
            StopOnError = false,
            Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync"
        });

        //_context.Install();
        base.Install();
    }

    public override void Uninstall()
    {
        Nop.Core.Domain.Tasks.ScheduleTask task = _schedualTaskService.GetTaskByType("Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync");

        if (task != null)
            _schedualTaskService.DeleteTask(task);

        //_context.Uninstall();
        base.Uninstall();
    }
}

Object context class (although not set up at this point so commented out some parts) 对象上下文类(尽管此时尚未设置,因此注释掉了某些部分)

public class MereSyncObjectContext : DbContext, IDbContext
{
    public bool ProxyCreationEnabled
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public bool AutoDetectChangesEnabled
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public MereSyncObjectContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {

    }

    /*protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //TODO: insert ALL model MAPPINGS!!!!
        //modelBuilder.Configurations.Add(new insertnewmap());
        base.OnModelCreating(modelBuilder);
    }*/

    public string CreateDatabaseInstallationScript()
    {
        return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
    }

    public void Install()
    {
        Database.SetInitializer<MereSyncObjectContext>(null);
        Database.ExecuteSqlCommand(CreateDatabaseInstallationScript());
        SaveChanges();
    }

    public void Uninstall()
    {
        //this.DropPluginTable("MereSys_TableName");
    }

    IDbSet<TEntity> IDbContext.Set<TEntity>()
    {
        return base.Set<TEntity>();
    }

    public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
    {
        throw new NotImplementedException();
    }

    public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
    {
        throw new NotImplementedException();
    }

    public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = default(int?), params object[] parameters)
    {
        throw new NotImplementedException();
    }

    public void Detach(object entity)
    {
        throw new NotImplementedException();
    }
}

Task class 任务班

public class ProductSyncTask : ITask
{
    private readonly ILogger _logger;
    public ProductSyncTask(ILogger logger)
    {
        this._logger = logger;
    }
    public async void Execute()
    {
        //TODO: Call My method, USE BEST PRACTICE, IoC DI
        //throw new NotImplementedException();
        _logger.InsertLog(Nop.Core.Domain.Logging.LogLevel.Information, "Start", "Product sync has started");
        ProductSyncService test = new ProductSyncService();
        await test.GetProductsAsync();
    }
}

Question

When i Navigate in the Nopcommerce backend to the log file 当我在Nopcommerce后端中导航到日志文件时

admin area > system > log 管理区域>系统>日志

I expected to see my log statment there, but nothing shows. 我希望在那里看到我的日志语句,但是什么也没显示。

So i also check the schedual status of the task, it never started. 因此,我还检查了任务的计划状态,它从未启动。 Breakpoints are also set up on my task class in debug mode and is never hit. 断点也在调试模式下在我的任务类上设置,并且永远不会命中。

So, Am i missing something here? 所以,我在这里想念什么吗? Is my implimentation correct? 我的暗示正确吗?

Notes: 笔记:

I have removed the async call but it did nothing (as its never reaching the class level). 我删除了异步调用,但是它什么也没做(因为它从未达到类级别)。 Cleaned and rebuilt, set copy local false where applicable. 清理并重建,在适用的情况下将copy local设置为false。 The plugin is installed. 插件已安装。 Event viewer on machine doesnt show any warnings or errors relating to nopcommerce. 机器上的事件查看器不显示与nopcommerce相关的任何警告或错误。 "Run Now" button when pressed refreshs page but no indication task run, nor my class breakpoints hit in debug mode. 当按下“立即运行”按钮时,刷新页面,但没有运行指示任务,也没有在调试模式下击中我的类断点。

Source file Source 源文件

plugin contains my code so far, most is redundant not used ie rest client etc as im not ever getting to that stage of the flow. 到目前为止,插件包含我的代码,大多数是多余的,没有使用过,例如,其余客户端等,因为我从未进入流程的那个阶段。

First parameter of Type property is full path of the class, which inherits the ITask interface. Type属性的第一个参数是Type的完整路径,该类继承了ITask接口。 And the second parameter is namespace of the plugin. 第二个参数是插件的名称空间。

So, you need to just change here: 因此,您只需要在此处进行更改:

_schedualTaskService.InsertTask(new Nop.Core.Domain.Tasks.ScheduleTask()
{
    Enabled = true,
    Name = "Product Sync",
    Seconds = 3600,
    StopOnError = false,
    Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync"
});

To

Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask.MereSyncPlugin, Nop.Plugin.Misc.MereSync"

Hope this helps! 希望这可以帮助!

OK Div was on the correct track but couldnt see my file system, I forgot 1 folder. 好的Div是在正确的轨道上,但是看不到我的文件系统,我忘记了1个文件夹。

Lesson to learn here? 要在这里学习的课程? Check Strings, I forgot i added a nice extra folder called...... "Tasks". 检查字符串,我忘了我添加了一个名为...“ Tasks”的好文件夹。

full path for install? 完整的安装路径?

"Nop.Plugin.Misc.MereSync.Tasks.ProductSyncTask, Nop.Plugin.Misc.MereSync.MereSyncPlugin" “ Nop.Plugin.Misc.MereSync.Tasks.ProductSyncTask,Nop.Plugin.Misc.MereSync.MereSyncPlugin”

Task in nop really is that simple. nop的任务实际上就是这么简单。 Dont waste a day like i just did! 不要像我一样浪费一天!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM