- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们的应用程序大量使用 Autofac 每个请求依赖项。这工作得很好并且符合我们的需求。
现在我们需要监听 AzureWebJobs。
由于我对 Azure WebJobs 还很陌生,所以我关注了 this教程(这里是 git-sources )
现在是棘手的部分。
尽管我遵循了所有说明并创建了 AutofacActivator
Autofac 似乎无法解析我的按请求注册的依赖项
DependencyResolutionException: No scope with a Tag matching 'AutofacWebRequest' is visible from the scope in which the instance was requested. This generally indicates that a component registered as per-HTTP request is being requested by a SingleInstance() component (or a similar scenario.) Under the web integration always request dependencies from the DependencyResolver.Current or ILifetimeScopeProvider.RequestLifetime, never from the container itself.
有什么办法可以根据上述要求使 Autofac 与 Azure-WebJobs 一起工作吗?
需要明确的是,我的 Autofaclistener 仅解析一项服务。这不能注册为 SingleInstance,因为它也在其他服务上得到解决。另外,我无法将我的 Per-Request-Stuff 更改为 InstancePerLifetimeScope
按照建议here
我希望我的问题和问题是不言自明的。如果没有,请告诉我
最佳答案
Azure webjobs 不是 Web 应用程序,它们更像是控制台应用程序。它们没有请求/响应语义。它们没有 http session 上下文,也没有请求范围。因此,您不应该使用按请求注册,它们在网络作业中毫无意义。如果您坚持使用每个请求的依赖关系,您将必须为您的网络作业模拟请求上下文,这是一种有风险的方法。您很容易引入内存泄漏和不良副作用。请注意,您正在强制采用一种不自然的方式来处理网络作业中的依赖范围。请参阅this page about implementing custom per-request scopes了解更多信息。
处理此问题的一个简单方法是仅在事件处理程序方法的生命周期内获取依赖范围。您可以在 using
block 内显式定义您的作用域(因为作用域不会像在 MVC 或 ASP.NET 应用程序中自动创建那样在 Webjob 中自动创建)。您必须将类型注册为 .InstancePerLifetimeScope()
。当您退出 using
block 时,这些实例将被释放。
因此,在事件处理程序中,您可以为处理程序方法的每次执行定义生命周期范围:
private void OnEvent(...)
{
using (var scope = container.BeginLifetimeScope())
{
...
var myService = scope.Resolve<IMyService>();
...
}
}
此示例使用显式 Resolve
调用来获取服务的实例。有更好的方法可以做到这一点,例如使用 autofac 工厂方法。
更高级的解决方案是实现您自己的IJobActivator
。这样,您就可以在 Functions
类中使用构造函数注入(inject),并且这些实例的生命周期范围将是处理程序方法执行的生命周期。 This answer包含代码示例。不过,您仍然需要将类型注册为 .InstancePerLifetimeScope()
。
关于c# - Autofac 和 Azure WebJobs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50148200/
我有一个 Azure WebJob,我正在从 visual studio 2017 发布到标准 S1 应用服务,WebJob 应该由 CRON 触发,但始终发布为连续,我无法弄清楚我做错了什么(另外两
我需要在计划的时间段内为不同的用户运行一项作业(例如 CRM 同步)。此同步由 ServiceBus 队列上的另一个函数放置的消息触发,该函数每 30 分钟由 TimerTrigger 触发一次。 现
我需要在计划的时间段内为不同的用户运行一项作业(例如 CRM 同步)。此同步由 ServiceBus 队列上的另一个函数放置的消息触发,该函数每 30 分钟由 TimerTrigger 触发一次。 现
我无法将计划的 Web 作业发布到 Azure 应用服务。我正在使用 Visual Studio 2017。 使用此设置一切正常: { "$schema": "http://schemastore.o
我计划使用连续的 Azure WebJob 发送电子邮件和推送通知。我了解 WebJobs 会因各种原因不时启动和停止。没关系,但我希望有机会在工作关闭之前“清理”一下。 这样我的 WebJob 可以
我已经看到了一些关于使用 settings.job 的指导。为此,但它不起作用 - 在控制台中我看到: WebJob singleton setting is False 如何防止横向扩展运行我的
我正在尝试部署一个触发的网络作业并使用 settings.json 文件安排它。我的 webapp 在 3 个实例中运行,所以我假设 webjob 将在 3 个实例中部署。因此,如果我使用带有 cro
我正在尝试找出是否有任何方法可以修改 webjobs sdk 重试策略。 现在,如果网络作业抛出异常,它会立即重新排队。这并不理想,特别是如果错误是由于数据库超时之类的原因造成的。 有谁知道该策略是否
我正在尝试部署一个触发的网络作业并使用 settings.json 文件安排它。我的 webapp 在 3 个实例中运行,所以我假设 webjob 将在 3 个实例中部署。因此,如果我使用带有 cro
我有一个启用了 Azure 持续交付的 Web 应用程序 (azure webapp)。这意味着每当我进行提交并将其推送到远程时,它都会自动构建应用程序并部署它。 我还有一些网络作业,我希望该过程也可
有没有办法从 MVC 应用程序中更改 webjob 的预定时间,或者是通过 azure 门户的唯一方法。 我不是指初始计划时间,而是在部署后通过 Mvc 应用程序中的 UI 更改计划时间。 最佳答案
Azure 提供了创建“触发式”WebJob 的可能性,例如每天都有时间表。 此外,还有 azure-webjobs-sdk-extensions( https://github.com/Azure/
我开始使用 WebJobs 来处理一些后台任务,但我遇到了关于错误处理和重试的问题。看来,如果我的任何函数是异步的,那么即使我在函数中抛出异常,该函数总是会在仪表板中报告成功。 考虑以下简单示例 ->
我们有一个带有“暂存”部署槽的 Azure 网站设置,我们使用连续的 Azure WebJob 来处理长时间运行的后台作业。看来,当您发布到网站(并包含 WebJob 二进制文件)时,如果 WebJo
我正在开发一个使用 TimerTrigger 的触发式网络作业. 在webjob停止之前,我需要处理一些对象,但我不知道如何触发“webjob停止”。 有了 NoAutomaticTrigger 函数
我正在处理一个网络作业,用于检查网络服务器的一些数据。有时网络服务器很忙,会在几秒钟后重试。 问题是,如果我只是忽略它并通过该函数,它将从队列中删除消息,我将无法重试调用。 我当前的解决方案(我一点也
我读过关于优雅关机的文章 here使用 WEBJOBS_SHUTDOWN_FILE 和 here使用取消 token ,所以我理解正常关闭的前提,但是我不确定它们将如何影响正在处理队列消息的 WebJ
如果运行webjob时出现任何异常或错误,我想释放所有对象。我如何知道是否发生错误。webjob 是否有任何错误触发器? 最佳答案 可以有两种方法来处理这个问题: 1) Try/Catch 方法:-
我在 AppService 中长时间运行 Web 作业(大约 1 小时)。AppService 已打开“始终打开”。 它的初始化是: var host = new JobHost(config); h
我在 AppService 中长时间运行 Web 作业(大约 1 小时)。AppService 已打开“始终打开”。 它的初始化是: var host = new JobHost(config); h
我是一名优秀的程序员,十分优秀!