0

I have this structuremap configuration and ITaskFactory for fluentScheduler.

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>();
    }
}

I got this exception.

{StructureMap.StructureMapConfigurationException: No default Instance is registered and cannot be automatically determined for type 'TheGame.Tasks.IAddHeroesQuests'

There is no configuration specified for 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 63
   v StructureMap.BuildSession.GetInstance(Type pluginType) v c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\BuildSession.cs:řádek 60
   v StructureMap.Container.GetInstance(Type pluginType) v c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:řádek 336
   v StructureMap.Container.GetInstance[T]() v c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:řádek 201
   v TheGame.StructureMapTaskFactory.GetTaskInstance[T]() v d:\Programovani\VisualStudio\Testy\TheGame\thegame\TheGame\Global.asax.cs:řádek 101
   v FluentScheduler.Registry.b__2[T]() v c:\TeamCity\buildAgent\work\21c2d4ee90f3f489\FluentScheduler\Registry.cs:řádek 50
   v System.Threading.Tasks.Task.InnerInvoke()
   v System.Threading.Tasks.Task.Execute()}

I do not know why.

talles
  • 14,356
  • 8
  • 45
  • 58
user3512982
  • 61
  • 1
  • 9
  • I solved the problem. The solution is simple. I forgot initialize ObjectFactory. – user3512982 Oct 16 '14 at 10:25
  • I'm dealing with a similar issue, it's my first exposure to StructureMap3+, could you possibly post a code sample of what you did? Thanks! – Eric Brown - Cal Sep 30 '15 at 16:14
  • offcourse, this code need in application start init the protected void Application_Start() { var container = new Container(new StructureMapRegistry()); var dependencyResolver = new StructureMapDependencyResolver(container); ObjectFactory.Initialize(x => x.AddRegistry(new StructureMapRegistry())); } – user3512982 Sep 30 '15 at 18:45
  • and this is the scheduler public class StructureMapTaskFactory :ITaskFactory { public ITask GetTaskInstance() where T : ITask { return Container.For().GetInstance(); } } public class MyRegistry : FluentScheduler.Registry { public MyRegistry() { Schedule().ToRunNow(); //ToRunEvery(1).Days().At(04, 00); //.At(04, 00); } } – user3512982 Sep 30 '15 at 18:46

1 Answers1

0

Dependency Injection

FluentScheduler makes it easy to use your IoC tool of choice to create job instances. Simply implement IJobFactory.

An example using 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();
    }
} 

Register the new job factory with the JobManager: This part is missin on your code. You need to tell JobManager.JobFactory is StructuremapJobFactory like you say your dependency injector is so.

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