如何为fluentscheduler配置结构图

本文关键字:配置 结构图 fluentscheduler | 更新日期: 2023-09-27 18:26:43

我有这个结构图配置和用于fluentScheduler的ITaskFactory。

public class StructureMapRegistry : Registry
{
    public StructureMapRegistry()
    {
        Scan(x =>
        {
            x.AssembliesFromApplicationBaseDirectory();
            x.WithDefaultConventions();
        });
        //implementace daného rozhraní naplní stejně jmenující se třídou.
        For<IPlayerService>().Use<PlayerService>().Singleton();
        For<IWorldService>().Use<WorldService>().Singleton();
        For<IQuestService>().Use<QuestService>().Singleton();
        For<IHeroesService>().Use<HeroesService>().Singleton();
        For<ISaveChangesService>().Use<SaveChangesService>().Singleton();
        For<IAddHeroesQuests>().Use<AddHeroesQuests>().Singleton();
        IncludeRegistry(new IoC());
    }
}
public class StructureMapTaskFactory : ITaskFactory
{
    public ITask GetTaskInstance<T>() where T : ITask
    {
        return ObjectFactory.Container.GetInstance<T>();
    }
}

我得到了这个例外。

{StructureMap.StructureMapConfigurationException:没有注册默认实例,无法自动确定类型为"TheGame.Tasks.IAddHeroesQuests"没有为TheGame.Tasks.IAddHeroesQuests指定配置1.)Container.GetInstance(TheGame.Tasks.IAddHeroesQuests)v StructureMap.SessionCache.GetDefault(Type pluginType,IPipelineGraph pipelineGraph)v c:''BuildAgent''work''996e173a8ceccdca''src''StructureMap''SessionCache.cs:řádek 63v StructureMap.BuildSession.GetInstance(Type pluginType)v c:''BuildAgent''work''996e173a8ceccdca''src''StructureMap''BuildSession.cs:řádek 60v StructureMap.Container.GetInstance(Type pluginType)v c:''BuildAgent''work''996e173a8ceccdca''src''StructureMap''Container.cs:řádek 336v StructureMap.Container.GetInstance[T]()v c:''BuildAgent''work''996e173a8ceccdca''src''StructureMap''Container.cs:řádek 201v TheGame.StructureMapTaskFactory.GetTaskInstance[T]()v d:''Programovani''VisualStudio''Testy''TheGame''TheGame''TheGame''Global.asax.cs:řádek 101v FluentScheduler.Registry.b_2[T]()v c:''TeamCity''buildAgent''work''21c2d4ee90f3f489''FluentScheduler''Registry.cs:řádek 50v System.Threading.Tasks.Task.InerInvoke()v System.Threading.Tasks.Task.Execute()}

我不知道为什么。

如何为fluentscheduler配置结构图

依赖注入

FluentScheduler可以方便地使用您选择的IoC工具来创建作业实例。只需实现IJobFactory。

使用StructureMap的示例:

using FluentScheduler;
using StructureMap;
public class StructureMapJobFactory : IJobFactory
{
    public IJob GetJobInstance<T>() where T : IJob
    {
        return ObjectFactory.Container.GetInstance<T>();
    }
}
public class MyRegistry : Registry
{
    public MyRegistry()
    {
        // Schedule an IJob to run at an interval
        Schedule<MyJob>().ToRunNow().AndEvery(2).Seconds();
    }
} 

向JobManager注册新的作业工厂:您的代码中缺少此部分。你需要告诉JobManager.JobFactory是StructuremapJobFactory,就像你说的依赖项注入器是这样。

protected void Application_Start()
{
    JobManager.JobFactory = new StructureMapJobFactory(); // THIS PART IS MISSING ON YOUR CODE
    JobManager.Initialize(new MyRegistry()); 
}