我用 project.json
导入 EntityFramework 6.1.3
NuGet 包。我可以确认 NuGet 包已在我的函数应用中成功还原。
根据document ,我可以直接使用EntityFramework。但是,我不断收到错误消息,例如:
ManualTriggerCSharp1: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
我的函数似乎无法正确加载 EntityFramework dll。即使我复制了
EntityFramework.dll
和 EntityFramework.SqlServer.dll
进bin
目录,它仍然提示同样的异常。我错过了什么?
更新
这是我的
project.json
文件:{
"frameworks": {
"net46":{
"dependencies": {
"Autofac": "4.2.0",
"AutoMapper": "5.1.1",
"EntityFramework": "6.1.3",
"Newtonsoft.Json": "9.0.1"
}
}
}
}
正如我上面提到的,这是行不通的。所以,我手动复制了
EntityFramework.dll
和 EntityFramework.SqlServer.dll
进bin
目录并在 run.csx
中引用它文件如:#r "EntityFramework.dll"
#r "EntityFramework.SqlServer.dll"
但是还在提示。整行日志消息是:
2016-11-18T20:22:46.296 Exception while executing function: Functions.ManualTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. ManualTriggerCSharp1: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
更新 2
这是我的完整功能代码:
#r "Tournaments.EntityModels.dll"
using System;
using System.Configuration;
using Tournaments.EntityModels;
public static void Run(string input, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
var connString = ConfigurationManager.ConnectionStrings["TournamentDbContext"].ConnectionString;
var dbContext = new TournamentDbContext(connString);
var numberOfPlayers = dbContext.Players.ToList().Count;
log.Info($"Number of Players: {numberOfPlayers}");
}
请注意
Tournaments.EntityModels.dll
是实际的数据库实体模型。这是我的 project.json
:{
"frameworks": {
"net46": {
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
我能够编译该函数。根本没有问题。问题发生在运行时。当我运行此函数时,出现错误:
2016-11-19T00:52:40.818 Function started (Id=c03af3f2-38f5-4df9-9bc5-b5932c365c04)
2016-11-19T00:52:40.818 Function completed (Failure, Id=c03af3f2-38f5-4df9-9bc5-b5932c365c04)
2016-11-19T00:52:40.836 Exception while executing function: Functions.ManualTriggerCSharp3. mscorlib: Exception has been thrown by the target of an invocation. ManualTriggerCSharp3: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
我查看了 KUDU 中的日志,并从日志流中找到了这个:
2016-11-19T01:00:25.454 Executing: 'Functions.ManualTriggerCSharp3' - Reason: 'This function was programmatically called via the host APIs.'
2016-11-19T01:00:25.649 Function started (Id=b18b45a7-83a8-453e-a337-955e6a4a5117)
2016-11-19T01:00:25.649 Function completed (Failure, Id=b18b45a7-83a8-453e-a337-955e6a4a5117)
2016-11-19T01:00:25.649 A ScriptHost error has occurred
2016-11-19T01:00:25.649 Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
2016-11-19T01:00:25.649 Function started (Id=b18b45a7-83a8-453e-a337-955e6a4a5117)
2016-11-19T01:00:25.649 Function completed (Failure, Id=b18b45a7-83a8-453e-a337-955e6a4a5117)
2016-11-19T01:00:25.665 Exception while executing function: Functions.ManualTriggerCSharp3. mscorlib: Exception has been thrown by the target of an invocation. ManualTriggerCSharp3: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
2016-11-19T01:00:25.665 Exception while executing function: Functions.ManualTriggerCSharp3. mscorlib: Exception has been thrown by the target of an invocation. ManualTriggerCSharp3: Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
2016-11-19T01:00:25.681 Exception while executing function: Functions.ManualTriggerCSharp3
2016-11-19T01:00:25.696 Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
2016-11-19T01:00:25.696 Executed: 'Functions.ManualTriggerCSharp3' (Failed)
2016-11-19T01:00:25.696 Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
2016-11-19T01:00:25.696 Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID is 'b18b45a7-83a8-453e-a337-955e6a4a5117'
2016-11-19T01:00:25.696 Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.
这是来自函数日志的堆栈跟踪消息:
...obs.Script.Description.FunctionInvokerBase.d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`1.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__3b.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__36.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__1a.MoveNext()
--- End of inner exception stack trace ---
我想我可以在这里提供所有必要的信息。你能给我一个建议吗?
最佳答案
以下步骤适用于最新的 Functions 运行时 (1.0)。创建一个新函数(例如 ManualTrigger),并使用“查看文件”UI 添加一个新的 project.json
包含以下内容的文件:
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
保存文件后,您应该会在功能日志窗口中看到包恢复,例如:
2016-11-18T16:50:05.772 Starting NuGet restore
2016-11-18T16:50:06.991 Restoring packages for D:\home\site\wwwroot\ManualTriggerCSharp2\project.json...
2016-11-18T16:50:07.553 Committing restore...
2016-11-18T16:50:07.569 Writing lock file to disk. Path: D:\home\site\wwwroot\ManualTriggerCSharp2\project.lock.json
2016-11-18T16:50:07.620 D:\home\site\wwwroot\ManualTriggerCSharp2\project.json
2016-11-18T16:50:07.620 Restore completed in 645ms.
2016-11-18T16:50:07.631
2016-11-18T16:50:07.631 NuGet Config files used:
2016-11-18T16:50:07.631 C:\DWASFiles\Sites\function-fun\AppData\NuGet\NuGet.Config
2016-11-18T16:50:07.631
2016-11-18T16:50:07.631 Feeds used:
2016-11-18T16:50:07.631 https://api.nuget.org/v3/index.json
2016-11-18T16:50:07.662
2016-11-18T16:50:07.662
2016-11-18T16:50:07.709 Packages restored.
之后,您可以简单地将 EF using 语句添加到您的代码中,它将成功编译:
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;
public static void Run(string input, TraceWriter log)
{
log.Info($"C# manually triggered function called with input: {input}");
}
关于entity-framework - 带有 Entity Framework 的 Azure 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40671391/