【发布时间】:2018-03-27 13:24:07
【问题描述】:
我有以下申请:
1) Mvc 应用程序:Hangfire 客户端,我将从那里将作业排入队列并托管 仪表板。这个应用程序将包含我的类库的引用。
2) 控制台应用程序:Hangfire 服务器将存在于此控制台应用程序中。
3) 类库:hangfire 服务器(控制台应用程序)和hangfire 客户端(asp.net mvc)之间的共享库,我的长时间运行的代码将驻留。Hangfire 服务器 将执行该库的代码。
我在 strategy design pattern. 之后的类库中具有如下结构
代码取自以下:Reference:
接口:
public interface IOperation
{
Output Process(Input input);
bool AppliesTo(Type type);
}
public interface IOperationStrategy
{
Output Process(Type type,Input input);
}
操作:
public class Add : IOperation
{
public bool AppliesTo(Type type)
{
return typeof(Add).Equals(type);
}
public Output Process(Input input)
{
// Implementation
return new Output();
}
}
public class Multiply : IOperation
{
public bool AppliesTo(Type type)
{
return typeof(Multiply).Equals(type);
}
public Output Process(Input input)
{
// Implementation
return new Output();
}
}
策略:
public class OperationStrategy : IOperationStrategy
{
private readonly IOperation[] operations;
public OperationStrategy(params IOperation[] operations)
{
if (operations == null)
throw new ArgumentNullException(nameof(operations));
this.operations = operations;
}
public Output Process(Type type, Input input)
{
var op = operations.FirstOrDefault(o => o.AppliesTo(type));
if (op == null)
throw new InvalidOperationException($"{operation} not registered.");
return op.Process(input);
}
}
用法:
// Do this with your DI container in your composition
// root, and the instance would be created by injecting
// it somewhere.
var strategy = new OperationStrategy(
new Add(), // Inject any dependencies for operation here
new Multiply()); // Inject any dependencies for operation here
// And then once it is injected, you would simply do this.
var input = new Input { Value1 = 2, Value2 = 3 };
BackgroundJob.Enqueue(() => strategy.Process(typeof(Add), input));
但是hangfire服务器无法选择工作,当我检查state table时,我看到如下错误:
“FailedAt”:“2018-03-21T13:14:46.0172303Z”、“ExceptionType”: "System.MissingMethodException", "ExceptionMessage": "没有无参数 为此对象定义的构造函数。", "ExceptionDetails": “System.MissingMethodException:未定义无参数构造函数 对于此对象。\r\n 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型,布尔值 publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)\r\n at System.RuntimeType.CreateInstanceSlow(布尔 publicOnly,布尔 skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)\r\n at System.Activator.CreateInstance(类型类型,布尔非公共)\r\n at System.Activator.CreateInstance(类型类型)\r\n at Hangfire.JobActivator.SimpleJobActivatorScope.Resolve(类型类型)\r\n 在 Hangfire.Server.CoreBackgroundJobPerformer.Perform(PerformContext 上下文)\r\n 在 Hangfire.Server.BackgroundJobPerformer.c__DisplayClass8_0.b__0()\r\n 在 Hangfire.Server.BackgroundJobPerformer.InvokePerformFilter(IServerFilter 过滤器、PerformingContext preContext、Func 1 延续)\r\n at Hangfire.Server.BackgroundJobPerformer.PerformJobWithFilters(PerformContext 上下文,IEnumerable`1 过滤器)\r\n at Hangfire.Server.BackgroundJobPerformer.Perform(PerformContext 上下文)\r\n 在 Hangfire.Server.Worker.PerformJob(BackgroundProcessContext 上下文, IStorageConnection 连接,字符串 jobId)"
我没有得到应该为这个结构设置的hangfire,因为我这里没有涉及任何 IOC 容器。
有人可以帮我解决这个问题吗?
演示项目:https://www.dropbox.com/s/bfjr58y6azgmm3w/HFDemo.zip?dl=0
【问题讨论】:
标签: c# asp.net-mvc strategy-pattern hangfire