- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Simple Injector 进行了以下配置。
public class SimpleInjectorIntegrator
{
private static Container container;
public static Container Setup()
{
container = new Container();
container.Options.DefaultScopedLifestyle = Lifestyle.CreateHybrid(
defaultLifestyle: new WebRequestLifestyle(),
fallbackLifestyle: new ThreadScopedLifestyle());
container.Register<IUserService, UserService>(Lifestyle.Scoped);
container.Register<IJob, BackgroundScheduler>(Lifestyle.Scoped);
JobManager.JobFactory = new SimpleInjectorJobFactory(container);
JobManager.Initialize(new RegisterScheduler());
}
}
public class SimpleInjectorJobFactory : IJobFactory
{
Container Container;
public SimpleInjectorJobFactory(Container container)
{
this.Container = container;
}
public IJob GetJobInstance<T>() where T : IJob
{
return Container.GetInstance<IJob>();
}
}
RegisterScheduler 初始化并安排作业。
BackgroundScheduler 如下所示:
public class BackgroundScheduler : IJob, IRegisteredObject
{
IUserService _userService;
public BackgroundScheduler(IUserService userService)
{
_userService = userService;
}
public void Execute()
{
_userService.GetAll();
}
}
BackgroundScheduler 依赖于 IUserService。当我尝试在后台调度程序中注入(inject) IUserService 时,出现以下异常:
BackgroundScheduler is registered as 'Hybrid Web Request / Thread Scoped' lifestyle, but the instance is requested outside the context of an active (Hybrid Web Request / Thread Scoped) scope.
堆栈跟踪:
SimpleInjector.ActivationException was unhandled by user code
HResult=-2146233088
Message=The BackgroundScheduler is registered as 'Hybrid Web Request / Thread Scoped' lifestyle, but the instance is requested outside the context of an active (Hybrid Web Request / Thread Scoped) scope.
Source=SimpleInjector
StackTrace:
at SimpleInjector.Scope.GetScopelessInstance[TImplementation](ScopedRegistration`1 registration)
at SimpleInjector.Scope.GetInstance[TImplementation](ScopedRegistration`1 registration, Scope scope)
at SimpleInjector.Advanced.Internal.LazyScopedRegistration`1.GetInstance(Scope scope)
at lambda_method(Closure )
at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.Container.GetInstanceFromProducer(InstanceProducer instanceProducer, Type serviceType)
at SimpleInjector.Container.GetInstanceForRootType[TService]()
at SimpleInjector.Container.GetInstance[TService]()
at FluentScheduler.JobManager.<>c__12`1.<GetJobAction>b__12_0() in __the_file_path_omitted__:line 76
at System.Threading.Tasks.Task.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
InnerException:
我不确定为什么会这样?
最佳答案
FleuntScheduler 的 IJobFactory
已弃用,但不会被另一个扩展点取代。虽然官方文档似乎缺少任何关于如何有效地从您的 DI 容器解决您的工作的描述,但维护者的观点似乎是您 register your jobs as a closure .
由于使用闭包意味着解析作业,将其包装在一个范围内,并在 Simple Injector 中注册您的作业,因此最实用的解决方案是将此逻辑移动到扩展方法中。这可能看起来像这样:
public static void AddFluentSchedulerJob<TJob>(
this Container container, Action<Schedule> schedule)
where TJob : class, IMyJob
{
container.Register<TJob>();
JobManager.AddJob(() =>
{
using (AsyncScopedLifestyle.BeginScope(container))
{
container.GetInstance<TJob>().Run();
}
},
schedule);
}
在这个例子中,IMyJob
是一个应用程序指定的抽象。这样做时,您可以防止应用程序代码依赖 FluentScheduler。
此扩展方法可以按如下方式使用:
container.AddFluentSchedulerJob<MyAwesomeJob>(s => s.ToRunEvery(5).Seconds());
其他选项是使用(现已弃用)IJobFactory
。这需要您的作业实现 FluentScheduler 的 IJob
接口(interface)。实现工作工厂的难点在于您需要找到一种方法将操作包装在一个范围内。
诀窍是用一个范围来包装你的工作的解决和执行。由于 FluentScheduler 中的拦截点似乎有限,我认为你可以做到这一点的唯一方法是改变你的工作工厂,使其返回一个装饰器,该装饰器将真正工作的创建推迟到装饰器的 Execute
方法被调用。装饰器的 Execute
可以启 Action 用域、解析实际作业、执行该作业并在内部处理作用域。
这是使用作用域的工厂:
public class SimpleInjectorJobFactory : IJobFactory
{
private readonly Container container;
public SimpleInjectorJobFactory(Container container) => this.container = container;
public IJob GetJobInstance<T>() where T : IJob
{
return new AsyncScopedJobDecorator(
this.container,
() => (IJob)this.container.GetInstance(typeof(T)));
}
private sealed class AsyncScopedJobDecorator : IJob
{
private readonly Container container;
private readonly Func<IJob> decorateeFactory;
public AsyncScopedJobDecorator(Container container, Func<IJob> decorateeFactory)
{
this.container = container;
this.decorateeFactory = decorateeFactory;
}
public void Execute()
{
using (AsyncScopedLifestyle.BeginScope(this.container))
{
this.decorateeFactory().Execute();
}
}
}
}
您可以通过设置 JobManager.JobFactory
来使用该工厂,就像您已经在做的那样:
JobManager.JobFactory = new SimpleInjectorJobFactory(container);
关于c# - 如何使用 FluentScheduler 正确配置 Simple Injector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953651/
$injector.instantiate有什么区别, $injector.get和 $injector.invoke在 AngularJS 中? 最佳答案 鉴于以下服务: app.service('
我尝试使用两种方式显式创建依赖关系。两者几乎相同,但我有点困惑使用反射注入(inject)器比普通注入(inject)器有什么优势,推荐使用哪种方式? 使用注入(inject)器 import { I
我对 Angular js 很陌生,以下是我的代码 (function(angular){ var testAngular = angular.mo
在英雄之旅教程的第 5 节中,main.dart 包含以下行: final InjectorFactory injector = self.injector$Injector; 这是错字吗?它在 VS
你好我是 Angular JS 的初学者,当我开始创建模块和 Controller 时,我无法打印我的消息,下面是我的代码 var myModule = angular.module("myFirst
我只是想在 Angular Dart 中尝试路由。我在关注这个https://webdev.dartlang.org/angular/guide/router/1指导,但我收到错误。 这一行给出错误
自从我设置一个与 Angular 一起运行的网络应用程序以来已经有一段时间了。我正在通过一个旧项目回顾我的步骤(尝试在我正在做的页面上实现一个 Controller ,确保 Controller 文件
我正在尝试学习 Angular,但无法正确加载它。我看了一下关于 SO 的类似问题,但找不到我要找的东西。 有人知道为什么会出现这个错误吗?在我看来,我是否正确地调用了我的新应用程序/模块?我的剧本错
我使用环回作为后端,使用 postgresql 作为数据库。所以我尝试使用 lb-service/js。 如果我尝试在 login.js 中包含 lbServices (angular.module(
我正在尝试将表情符号应用程序注入(inject)我的项目。 我添加了emoji.min.js并像这样引用它 现在我也将它注入(inject)到我的项目中,如下所示: angular.module('
当我尝试将 $cookie 包含到我的 Controller 中时出现此错误。 任何人都可以向我解释我做错了什么? 我在 index.html 中有 angular-cookie.js 我在 conf
我正在使用 anguarjs 开发一个应用程序,并出现如下错误: 无法实例化模块 myApp,原因是:错误:[$injector:modulerr] http://errors.angularjs.o
var demoapp = angular.module('demoapp', []); demoapp.controller('SimpleC
我正在尝试通过网络摄像头简单地读取二维码,但我在控制台打开 Angular 帮助页面时遇到此错误,但说使用 ngRoute Error: [$injector:modulerr] http://err
我正在尝试对此进行编码,只是使用路由从第一个页面跳转到另一个页面,但为什么它不起作用,我搜索并使用了很多技巧但仍然失败,请问有人吗? index.html Angular Js
我收到此错误: ncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$inje
我正在尝试创建一个带有 Angular 路由的 SPA(单页应用程序),但是当我将 ndRoute 添加到我的模块时,它给了我这个错误 Uncaught Error: [$injector:modul
this is the error i m getting.这个问题被问了很多次。但是下面的代码在在线编译器中运行良好,但在我的本地系统中运行不佳,即使系统已连接到互联网,它也显示未定义 Angula
我正在尝试学习 Angular 并尝试在基于 Web 的教程之外编写我的第一个 Controller 。我一直在 JS 控制台的标题中收到错误消息,无法弄清楚发生了什么。该链接将我带到一个引用需要包含
我在尝试编写代码时遇到此错误。我已经完成了其他 Stackflow 问题,但找不到答案。是什么导致了这个错误? var app = angular.module("app", ['ui.router'
我是一名优秀的程序员,十分优秀!