我有一个处理程序方法的应用程序。处理程序方法获取一个json字符串,其中包含需要处理请求的对象的名称以及请求的参数。基本上,东西(我会保持它的简单)是这样的:使用Unity来获取基于名称的对象
public interface IJob
{
bool Execute();
bool Hydrate(string source);
}
public class JobBase
{
public int Id { get; set; }
public JobType JobType { get; set; }
public CronExpression CronExpression { get; set; }
}
public class JobSubmitClone : JobBase, IJob
{
public string[] Tokens { get; set; }
public bool Hydrate(string source)
{
// code omitted...
return true;
}
public bool Execute()
{
// code omitted...
return true;
}
}
IJob和JobBase都保存在一个公共类项目。所有应用程序引用此DLL。
在我的主要应用程序,我已经安装了统一和装载容器中的一个步骤来完成,如:
// Scan assemblies for Job definitions...
_container.RegisterTypes(AllClasses.FromAssembliesInBasePath().
Where(type => typeof(IJob).IsAssignableFrom(type)),
WithMappings.FromAllInterfaces,
WithName.TypeName,
WithLifetime.Transient);
每个“工作”,在自己的类项目是指,是不是通过引用主要的应用程序。每个“作业”必须继承自JobBase
和IJob
。
主应用程序有一个简单的REST服务暴露。您可以发表类似:
{ jobName : JobSubmitClone, Id : 1, JobType : 2, CronExpression : '' }
在主应用程序,我想从基于JobName
集装箱拉的对象。我已经试过这(是的,我知道这违反了IoC模式):
var container = UnityHelpers.GetConfiguredContainer();
var job = container.Resolve<IJob>(myParams.jobName); // "JobSubmitClone" //
var hydrated = job.Hydrate(myParams);
if(hydrated)
var result = job.Execute();
我收到以下错误:
Exception is: InvalidOperationException - The current type, IJob, is an interface and cannot be constructed. Are you missing a type mapping?
我缺少什么?