C# 将Spring.Net与HangFire结合使用
我们使用spring.net来管理依赖注入,但是当我们使用hangfire运行的方法时,依赖类是空的。使用其他IoC容器还不是一个选项-请不要问为什么。顺便说一句,如果uploadDoc()没有通过hangfire运行,即在应用程序的线程中运行,spring.net能够注入依赖类 代码如下:C# 将Spring.Net与HangFire结合使用,c#,webforms,spring.net,hangfire,C#,Webforms,Spring.net,Hangfire,我们使用spring.net来管理依赖注入,但是当我们使用hangfire运行的方法时,依赖类是空的。使用其他IoC容器还不是一个选项-请不要问为什么。顺便说一句,如果uploadDoc()没有通过hangfire运行,即在应用程序的线程中运行,spring.net能够注入依赖类 代码如下: class docUpload { IDependencyClass1 DClass1; IDependencyClass2 DClass2; uploadDoc() { D
class docUpload {
IDependencyClass1 DClass1;
IDependencyClass2 DClass2;
uploadDoc() {
DClass1.doSomething(); //Dclass1 is null here
}
}
hangfireclass.jobclient.enqueue(() => docUpload.uploadDoc());
public class SpringNetActivator: JobActivator
{
private readonly IApplicationContext _springContext;
public SpringNetActivator(IApplicationContext springContext)
{
_springContext = springContext;
}
public override object ActivateJob(Type jobType)
{
return _springContext.GetObject(jobType.Name);
}
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
//some code here
return base.BeginScope(context);
}
}
然后在SpringXML中:
<object id="docUpload " type="some code here">
<property name="DClass1" ref="dClass1" />
<property name="DClass2" ref="dClass2" />
</object>
<object id="dClass1" type="some code here">
<object id="dClass2" type="some code here">
你知道为什么在hangfire线程中,spring.net的依赖注入不起作用吗?如果在应用程序的线程中运行,它就会工作。@ma22
允许将Spring.NET与Hangfire一起使用。
您只需像这样实现JobActivator:
class docUpload {
IDependencyClass1 DClass1;
IDependencyClass2 DClass2;
uploadDoc() {
DClass1.doSomething(); //Dclass1 is null here
}
}
hangfireclass.jobclient.enqueue(() => docUpload.uploadDoc());
public class SpringNetActivator: JobActivator
{
private readonly IApplicationContext _springContext;
public SpringNetActivator(IApplicationContext springContext)
{
_springContext = springContext;
}
public override object ActivateJob(Type jobType)
{
return _springContext.GetObject(jobType.Name);
}
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
//some code here
return base.BeginScope(context);
}
}
然后,您需要在启动Hangfire服务器之前将其注册为当前作业激活器:
var springActivator = new SpringNetActivator(ContextRegistry.GetContext());
GlobalConfiguration.Configuration.UseActivator(springActivator);
...
app.UseHangfireServer();
您可以在Hangfire文档中阅读有关使用IoC容器的更多信息@ma22
允许将Spring.NET与Hangfire一起使用。
您只需像这样实现JobActivator:
class docUpload {
IDependencyClass1 DClass1;
IDependencyClass2 DClass2;
uploadDoc() {
DClass1.doSomething(); //Dclass1 is null here
}
}
hangfireclass.jobclient.enqueue(() => docUpload.uploadDoc());
public class SpringNetActivator: JobActivator
{
private readonly IApplicationContext _springContext;
public SpringNetActivator(IApplicationContext springContext)
{
_springContext = springContext;
}
public override object ActivateJob(Type jobType)
{
return _springContext.GetObject(jobType.Name);
}
public override JobActivatorScope BeginScope(JobActivatorContext context)
{
//some code here
return base.BeginScope(context);
}
}
然后,您需要在启动Hangfire服务器之前将其注册为当前作业激活器:
var springActivator = new SpringNetActivator(ContextRegistry.GetContext());
GlobalConfiguration.Configuration.UseActivator(springActivator);
...
app.UseHangfireServer();
您可以在Hangfire文档中阅读有关使用IoC容器的更多信息您需要发布一些真实的代码
docUpload
甚至无法编译,而且xml
代码段看起来也不是有效的spring
格式。您需要发布一些真实的代码docUpload
甚至不会编译,而且xml
代码段看起来也不是有效的spring
格式。