C# 如何为fluentscheduler配置structuremap?
我有这个structuremap配置和fluentScheduler的ITaskFactoryC# 如何为fluentscheduler配置structuremap?,c#,structuremap,fluentscheduler,C#,Structuremap,Fluentscheduler,我有这个structuremap配置和fluentScheduler的ITaskFactory public class StructureMapRegistry : Registry { public StructureMapRegistry() { Scan(x => { x.AssembliesFromApplicationBaseDirectory(); x.WithDefaultCo
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>();
}
}
公共类结构映射注册表:注册表
{
公共结构映射注册表()
{
扫描(x=>
{
x、 AssembliesFromApplicationBaseDirectory();
x、 使用默认约定();
});
//实施daného rozhranínaplnístejnějmenujícíse třdou。
For().Use().Singleton();
For().Use().Singleton();
For().Use().Singleton();
For().Use().Singleton();
For().Use().Singleton();
For().Use().Singleton();
包括注册(新IoC());
}
}
公共类结构MapTaskFactory:ITaskFactory
{
公共ITask GetTaskInstance(),其中T:ITask
{
返回ObjectFactory.Container.GetInstance();
}
}
我得到了这个例外
{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()}
{StructureMap.StructureMappConfigurationException:未注册任何默认实例,无法自动确定类型“TheGame.Tasks.IddheroRequests”
没有为game.Tasks.iaddherosquests指定配置
1.)Container.GetInstance(TheGame.Tasks.iaddherosquests)
v StructureMap.SessionCache.GetDefault(类型pluginType,IPipelineGraph pipelineGraph)v c:\BuildAgent\work\996e173a8ceccda\src\StructureMap\SessionCache.cs:
v StructureMap.BuildSession.GetInstance(类型pluginType)v c:\BuildAgent\work\996e173a8ceccda\src\StructureMap\BuildSession.cs:
v StructureMap.Container.GetInstance(类型pluginType)v c:\BuildAgent\work\996e173a8ceccda\src\StructureMap\Container.cs:
v StructureMap.Container.GetInstance[T]()v c:\BuildAgent\work\996e173a8ceccda\src\StructureMap\Container.cs:
v TheGame.structuremapstaskfactory.GetTaskInstance[T]()v d:\Programovani\VisualStudio\Testy\TheGame\TheGame\TheGame\Global.asax.cs:
v FluentScheduler.Registry.b_u2[T]()v c:\TeamCity\buildAgent\work\21c2d4ee90f3f489\FluentScheduler\Registry.cs:
v System.Threading.Tasks.Task.InnerInvoke()
v System.Threading.Tasks.Task.Execute()}
我不知道为什么。依赖注入 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();
}
}
我解决了这个问题。解决办法很简单。我忘了初始化ObjectFactory。我正在处理一个类似的问题,这是我第一次接触StructureMap3+,你可以发布一个你所做的代码示例吗?谢谢当然,此代码需要在应用程序启动init中启动受保护的无效应用程序_start(){var container=new container(new structuremapgregistry());var dependencysolver=new structuremappdependencysolver(container);ObjectFactory.Initialize(x=>x.AddRegistry(new structuremapgistry());}这是调度程序公共类StructureMapTaskFactory:ITaskFactory{public ITask GettaskinInstance(),其中T:ITask{return Container.For().GetInstance();}}}}公共类MyRegistry:FluentScheduler.Registry{public MyRegistry(){Schedule().ToRunNow();//在(04,00);//.At(04,00);}
protected void Application_Start()
{
JobManager.JobFactory = new StructureMapJobFactory(); // THIS PART IS MISSING ON YOUR CODE
JobManager.Initialize(new MyRegistry());
}