c# - 从 Azure 队列 Run() 方法内部调用实体框架核心工作单元?
问题描述
在我的队列中,我正在生成一个需要保存到数据库的报告。
我尝试像这样注入我的工作单元/存储库:
[FunctionName("AppExecution")]
public static void Run(
[QueueTrigger("some-name", Connection = "ConnectionStrings")]
string myQueueItem,
ILogger log,
UnitOfWork Work
// IRepository<SomeClass> Repo // <-- doesn't work either :(
)
{ ... }
但它没有用。运行时出现此错误:
Microsoft.Azure.WebJobs.Host:索引方法“AppExecution”出错。Microsoft.Azure.WebJobs.Host:无法将参数“Work”绑定到类型 UnitOfWork。确保绑定支持参数类型。如果您正在使用绑定扩展(例如 Azure 存储、ServiceBus、计时器等),请确保您已在启动代码中调用了扩展的注册方法(例如 builder.AddAzureStorage()、builder.AddServiceBus( )、builder.AddTimers() 等)。
请问有什么建议吗?我真的需要将结果数据保存回数据库!
解决方案
您需要使类非静态和函数非静态,然后将您的对象注入到类构造函数中。在docs中有一些描述。例如:
public class AppExecutionFunction
{
private readonly UnitOfWork _unitOfWork;
public AppExecutionFunction(UnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
[FunctionName("AppExecution")]
public void Run(
[QueueTrigger("some-name", Connection = "ConnectionStrings")]
string myQueueItem,
ILogger log)
{
_unitOfWork.CallMethod();
}
}
推荐阅读
- angular - 在 Angular 应用程序中抛出错误
- c# - C# 中的代表以及如何将它们附加到 Visual Studio 中的控件?
- ios - 用户注销并重新登录时不显示徽标
- angular - (Angular 5) - 为什么 typescript 的 for 只取最后一个数字
- php - 扩展主机在打开 .php 文件时意外终止
- django - 如何在 Report/Flag User objectUR 的 CreateView 中获取页面的 URL
- c# - Emgu.CV.Util.CvException:OpenCV:layer_rows - 2 * min_margin > 0
- sql - 关系不存在 PostgreSQL
- ios - 如何在函数内返回 UIImage?
- python - 在非嵌套参数中使用嵌套变量