在MVC Web应用程序中,DbContext绑定可与InRequestScope()一起正常使用

 kernel.Bind<DbContext>().ToSelf().InRequestScope();
 kernel.Bind<IUnitOfWork<DbContext>>().To<UnitOfWork<DbContext>>();


但是从任务计划程序中调用DbContext中的InRequestScope()无法更新Db表(没有任何错误),直到将绑定更改为InSingletonScope()InThreadScope()

问题:因此,它们是如何将Task Scheduler调用的范围更改为InSingletonScope() / InThreadScope()的。 ?

//对于Task Scheduler Call,我尝试了波纹管绑定,但无法正常工作

kernel.Bind<DbContext>().ToSelf()
.When(request => request.Target.Type.Namespace.StartsWith("NameSpace.ClassName"))
.InSingletonScope();


**可能我想念一些东西。需要帮忙。

代码段已更新

#region Commented Code

public EmailTask() : this
 ( DependencyResolver.Current.GetService<IMessageManager>(),
 , DependencyResolver.Current.GetService<IUnitOfWork<DbContext>>()) { }

#endregion



public EmailTask(IMessageManager messageManager, IUnitOfWork<DbContext> unitOfWork)
{
            this._messageManager = messageManager;
            this._unitOfWork = unitOfWork;
            ProcessEmail();

}

public class NonRequestScopedParameter : IParameter { ... }

public void ProcessEmail()
{
   var temp = SomeRepository.GetAll();

   SendEmail(temp);

   temp.Date = DateTime.Now;

   SomeRepository.Update(temp);

   unitOfWork.Commit();
}

public class ExecuteEmailTask : ITask
{
  private readonly IResolutionRoot _resolutionRoot;
  private int _maxTries = 5;

  public ExecuteEmailTask(IResolutionRoot resolutionRoot)
  {
        _resolutionRoot = resolutionRoot;
  }

  public void Execute(XmlNode node)
        {
            XmlAttribute attribute1 = node.Attributes["maxTries"];
            if (attribute1 != null && !String.IsNullOrEmpty(attribute1.Value))
            {
                this._maxTries = int.Parse(attribute1.Value);
            }
            /// send email messages
            var task = _resolutionRoot.Get<EmailTask>(new NonRequestScopedParameter());
        }
}


在Web.Config中

<ScheduleTasks>
     <Thread seconds="60">
        <task name="ExecuteEmailTask" type="namespace.ExecuteEmailTask, AssemblyName" enabled="true" stopOnError="false" maxTries="5"/>
      </Thread>
    </ScheduleTasks>


在Global.asax中

protected void Application_Start()
{
   /* intialize Task */
            TaskConfig.Init();

            TaskManager.Instance.Initialize(TaskConfig.ScheduleTasks);
            TaskManager.Instance.Start();
}


Ninject绑定语法

kernel.Bind<DbContext>().ToSelf().InRequestScope(); // Default bind

kernel.Bind<DbContext>().ToSelf()
                  .When(x => x.Parameters.OfType<NonRequestScopedParameter>().Any())
                  .InCallScope();  // For Scheduler


注意:EmailTask类还具有SomeReposity作为构造函数参数。

查询:


但是解析TaskScheduler(IResolutionRoot resolutionRoot)的绑定语法是什么?
运行TaskScheduler的配置代码是什么?
就像直接将IFakeDbContext放入构造函数中,可以与IUnitOfWork<FakeDbContext>一起使用吗?


问题

任务无法使用重载构造函数调用,它只能调用TaskScheduler默认构造函数。

问题4:可以通过任何方法从TaskScheduler(IResolutionRoot resolutionRoot)默认构造函数调用TaskScheduler吗?

用于创建任务并使用System.Threading.Timer运行的示例代码段

private ITask createTask()
        {
            if (this.Enabled && (this._task == null))
            {
                if (this._taskType != null)
                {
                    this._task = Activator.CreateInstance(this._taskType) as ITask;
                }
                this._enabled = this._task != null;
            }
            return this._task;
        }


问题5:我可以在这里解决TaskScheduler(IResolutionRoot resolutionRoot)吗?

解决了

public ExecuteEmailTask() : this(DependencyResolver.Current.GetService<IResolutionRoot>())

要么

 public ExecuteEmailTask() : this(new Bootstrapper().Kernel) { }

        public ExecuteEmailTask(IResolutionRoot resolutionRoot)
        {
            _resolutionRoot = resolutionRoot;
        }

最佳答案

首先,您应该注意InSingletonScope()通常对于DbContext的/ Sessions是一个坏主意。如果同时有其他服务更改数据会怎样?我建议调查这有什么影响。



对于您首先描述的场景,正确编写的.When(...)应该适用。

作为.When(...)绑定的替代方法,您还可以使用.Named("FooBar")绑定。
然后,计划任务的构造函数应如下所示:

ctor(Named["FooBar"] DbContext dbContext);


但是,请注意,只有在需要将DbContext注入到单个构造函数中的情况下,这才(轻松)起作用。如果任务具有依赖项,并且这些依赖项也需要相同的DbContext实例,则它会变得有些棘手。

由于您更新了答案并说是这种情况,因此,我建议一种完全不同的方法:将请求参数用作When(...)条件与InCallScope绑定结合的基础。请参阅下面的示例。

振作起来,这是代码的补充:)该实现需要ninject.extensions.NamedScope扩展(nuget)。
我还使用了xUnit和FluentAssertions nuget包来执行测试。

public class Test
{
    // the two implementations are just for demonstration and easy verification purposes. You will only use one DbContext type.
    public interface IFakeDbContext { }
    public class RequestScopeDbContext : IFakeDbContext { }
    public class CallScopeDbContext : IFakeDbContext { }

    public class SomeTask
    {
        public IFakeDbContext FakeDbContext { get; set; }
        public Dependency1 Dependency1 { get; set; }
        public Dependency2 Dependency2 { get; set; }

        public SomeTask(IFakeDbContext fakeDbContext, Dependency1 dependency1, Dependency2 dependency2)
        {
            FakeDbContext = fakeDbContext;
            Dependency1 = dependency1;
            Dependency2 = dependency2;
        }
    }

    public class Dependency1
    {
        public IFakeDbContext FakeDbContext { get; set; }

        public Dependency1(IFakeDbContext fakeDbContext)
        {
            FakeDbContext = fakeDbContext;
        }
    }

    public class Dependency2
    {
        public IFakeDbContext FakeDbContext { get; set; }

        public Dependency2(IFakeDbContext fakeDbContext)
        {
            FakeDbContext = fakeDbContext;
        }
    }

    public class TaskScheduler
    {
        private readonly IResolutionRoot _resolutionRoot;

        public TaskScheduler(IResolutionRoot resolutionRoot)
        {
            _resolutionRoot = resolutionRoot;
        }

        public SomeTask CreateScheduledTaskNow()
        {
            return _resolutionRoot.Get<SomeTask>(new NonRequestScopedParameter());
        }
    }

    public class NonRequestScopedParameter : Ninject.Parameters.IParameter
    {
        public bool Equals(IParameter other)
        {
            if (other == null)
            {
                return false;
            }

            return other is NonRequestScopedParameter;
        }

        public object GetValue(IContext context, ITarget target)
        {
            throw new NotSupportedException("this parameter does not provide a value");
        }

        public string Name
        {
            get { return typeof(NonRequestScopedParameter).Name; }
        }

        // this is very important
        public bool ShouldInherit
        {
            get { return true; }
        }
    }

    [Fact]
    public void FactMethodName()
    {
        var kernel = new StandardKernel();

        // this is the default binding
        kernel.Bind<IFakeDbContext>().To<RequestScopeDbContext>();

        // this binding is _only_ used when the request contains a NonRequestScopedParameter
        // in call scope means, that all objects built in the a single request get the same instance
        kernel.Bind<IFakeDbContext>().To<CallScopeDbContext>()
            .When(x => x.Parameters.OfType<NonRequestScopedParameter>().Any())
            .InCallScope();

        // let's try it out!
        var task = kernel.Get<SomeTask>(new NonRequestScopedParameter());

        // verify that the correct binding was used
        task.FakeDbContext.Should().BeOfType<CallScopeDbContext>();

        // verify that all children of the task get injected the same task instance
        task.FakeDbContext.Should()
            .Be(task.Dependency1.FakeDbContext)
            .And.Be(task.Dependency2.FakeDbContext);
    }
}


如您所说,由于任务计划程序不利用IoC来创建任务,因此它仅支持无参数构造函数。在这种情况下,您可以使用DependencyResolver.Current(但是,请注意,我绝不是asp.net / MVC的专家,所以我不做任何声称这是线程安全或100%可靠工作的声明):

public class TaskExecutor : ITask
{
    public TaskExecutor()
        : this(DependencyResolver.Current.GetService<IResolutionRoot>())
    {}

    internal TaskExecutor(IResolutionRoot resolutionRoot)
    {
        this.resolutionRoot = resolutionRoot;
    }

    public void Execute()
    {
        IFooTask actualTask = this.resolution.Get<IFooTask>(new NonRequestScopedParameter());
        actualTask.Execute();
    }
}

10-01 03:28
查看更多