Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Functions with Entity Framework

I use project.json to import EntityFramework 6.1.3 NuGet package. I can confirm that the NuGet package is successfully restored in my function app.

According to the document, I can directly use EntityFramework. However, I keep getting error like:

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.

It seems that my function can't load the EntityFramework dll properly. Even though I copied both EntityFramework.dll and EntityFramework.SqlServer.dll into the bin directory, it still complains with the same exception.

What am I missing?

Update

Here's my project.json file:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Autofac": "4.2.0",
        "AutoMapper": "5.1.1",
        "EntityFramework": "6.1.3",
        "Newtonsoft.Json": "9.0.1"
      }
    }
   }
}

As I mentioned above, this was not working. So, I manually copied both EntityFramework.dll and EntityFramework.SqlServer.dll into the bin directory and reference it within the run.csx file like:

#r "EntityFramework.dll"
#r "EntityFramework.SqlServer.dll"

But still complaining. The whole line of log message is:

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.

Update 2

Here's my complete function code:

#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}");
}

Note that Tournaments.EntityModels.dll is the actual database entity models. And here's my project.json:

{
  "frameworks": {
    "net46": {
      "dependencies": {
        "EntityFramework": "6.1.3"
      }
    }
  }
}

I was able to compile the function. There's no issue at all. The problem occurred at the runtime. when I ran this function, I got the error:

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.

I looked into the logs in KUDU and found this from the logstream:

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.

And here's the stack trace message from the function log:

...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 ---

I think I can provide all the necessary information I can here. Could you give me an advice, please?

like image 580
justinyoo Avatar asked Nov 18 '16 07:11

justinyoo


1 Answers

The following steps work for me on the latest Functions runtime (1.0). Create a new function (e.g. ManualTrigger), and use the "view files" UI to add a new project.json file with the following content:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "EntityFramework": "6.1.3"
      }
    }
   }
}

After saving the file you should see package restore happening in the functions log window, e.g.:

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.

After that you can simply add the EF using statements to your code and it will compile successfully:

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}");
}
like image 150
mathewc Avatar answered Oct 20 '22 14:10

mathewc