我有一个 Windows 服务,我想确保我的 EF ObjectContext 在每次运行之间被处理掉。该服务每次执行的时间都会更长。似乎 ObjectContext 一直在增长。我的 ObjectContext 应该以不同的方式注册还是我做错了什么?
我在做什么的概述。
- 我正在使用Quartz.NET来安排我的服务
- 我正在使用Atlas来配置和设置我的 Windows 服务
- 我使用Autofac作为我的 IoC
- 我使用实体框架作为我的数据模型
代码演练:
- 因此,该程序通过使用 Atlas 注册服务开始。
- Atlas 将注册我的 autofac 注册,这些注册位于模块 MyModule 中
- MyModule 在 InstancePerLifetimeScope 注册 ObjectContext(这是正确的范围吗?),然后它将有一次我的自定义 UnitOfWork 实例作为 InstancePerLifetimeScope(这是正确的范围吗?)。
- MyService 由 Atlas 托管,它注入了 Quartz 调度程序和 AutofacJobListener 属性,并安排作业(MyJob)在服务启动时每 5 分钟运行一次。
- MyJob 从 AutofacJobListener 获取 ObjectContext 属性的实例。这项工作调用数据库并为我获取我的东西。
当作业运行时,它每次运行时都会调用并获取我的东西,这需要更长的时间(例如:第一次运行 2 分钟,第二次运行服务 4 分钟,下一次运行 6 分钟,下一次运行 8 分钟,依此类推) . 似乎我的 ObjectContext 每次都变得越来越大。它提取的数据没有改变,仍然是相同的行数和列数。所以我在想我的注册是错误的,是这样吗?如果没有,你能看出我在做什么的问题吗?
程序
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
var configuration =
Host.Configure<MyService>(c =>
{
c.AllowMultipleInstances();
c.WithRegistrations(b => b.RegisterModule(new MyModule()));
}, args);
Host.Start(configuration);
}
}
模块
public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
LoadQuartz(builder);
LoadServices(builder);
LoadInfrastructure(builder);
}
private void LoadInfrastructure(ContainerBuilder builder)
{
builder.Register(c => new ObjectContext())
.As<IObjectContext>()
.InstancePerLifetimeScope();
builder.Register(c => new UnitOfWork(c.Resolve<IObjectContext>()))
.As<ISession>().As<IObjectContextProvider>()
.InstancePerLifetimeScope();
}
private void LoadQuartz(ContainerBuilder builder)
{
builder.Register(c => new StdSchedulerFactory().GetScheduler()).As<IScheduler>().InstancePerLifetimeScope();
builder.Register(c => new AutofacJobListener(c)).As<IJobListener>();
}
private void LoadServices(ContainerBuilder builder)
{
builder.RegisterType<MyService>().As<IAmAHostedProcess>().PropertiesAutowired();
}
}
AutofacJobListener
public class AutofacJobListener : IJobListener
{
private readonly IComponentContext _container;
public AutofacJobListener(IComponentContext container)
{
_container = container;
}
public void JobToBeExecuted(JobExecutionContext context)
{
_container.InjectUnsetProperties(context.JobInstance);
}
public void JobExecutionVetoed(JobExecutionContext context)
{
/*noop*/
}
public void JobWasExecuted(JobExecutionContext context, JobExecutionException jobException)
{
/*noop*/
}
public string Name
{
get { return "AutofacInjectionJobListener"; }
}
}
我的服务
public class MyService : IAmAHostedProcess
{
public IScheduler Scheduler { get; set; }
public IJobListener AutofacJobListener { get; set; }
#region Implementation of IAmAHostedProcess
public void Start()
{
var trigger = TriggerUtils.MakeMinutelyTrigger(5);
trigger.Name = @"Job Trigger";
Scheduler.ScheduleJob(new JobDetail("Job", null, typeof(MyJob)), trigger);
Scheduler.AddGlobalJobListener(AutofacJobListener);
Scheduler.Start();
}
public void Stop()
{
Scheduler.Shutdown();
}
public void Resume()
{
}
public void Pause()
{
}
#endregion
}
我的工作
public class MyJob : IJob
{
public IObjectContext ObjectContext { get; set; }
public void Execute(JobExecutionContext context)
{
var myStuff = ObjectContext.GetIQueryable<Stuff>();
}
}