Can't load Entity Framework Core in Azure Function - nuget

I'm trying to use Entity Framework Core with a C# Azure Function, and I'm getting tons of errors with libraries. Starting from the default HTTP trigger template, I created a project.json file containing
{
"frameworks": {
"net46":{
"dependencies": {
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0"
}
}
}
}
When the package restore finishes, the compiler can no longer find the extension methods in HttpRequestMessageExtensions:
016-12-21T06:59:24.728 (9,19): error CS1929: 'HttpRequestMessage' does
not contain a definition for 'GetQueryNameValuePairs' and the best
extension method overload
'HttpRequestMessageExtensions.GetQueryNameValuePairs(HttpRequestMessage)'
requires a receiver of type 'HttpRequestMessage'
(and 3 other similar errors).
I don't really need to use those extension methods, and if I delete all code that calls them, instead I get errors about the version of System.Net.Http:
Exception during runtime resolution of assembly 'System.Net.Http,
Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a':
'System.BadImageFormatException: Cannot load a reference assembly for
execution.
at System.Reflection.RuntimeAssembly.nLoadFile(String path,
Evidence evidence)
at System.Reflection.Assembly.LoadFile(String path)
at
Microsoft.Azure.WebJobs.Script.Description.FunctionMetadataResolver.ResolveAssembly(String
assemblyName)
at
Microsoft.Azure.WebJobs.Script.Description.FunctionAssemblyLoadContext.ResolveAssembly(String
name)
at
Microsoft.Azure.WebJobs.Script.Description.FunctionAssemblyLoader.ResolveAssembly(Object
sender, ResolveEventArgs args)'
2016-12-21T07:00:00.522 Unable to find assembly 'System.Net.Http,
Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Are you missing a private assembly file?
When NuGet adds the EntityFrameworkCore package, it installs System.Net.Http version 4.3.0, whereas Azure Functions seems to be looking for the hardcoded version 4.1.1.
I see questions about using EF6 with Azure Functions, but I don't see anything about EF Core.

one solution is changing from Azure Function v2 core to Azure Function v1 framework.

Related

Using Accord.Imaging in UWP

(apologies for beginner question) I am trying to use Accord.Net Imaging function in an UWP windows 10 App. Any function I try gives me an error:
The type 'Bitmap' is defined in an assembly that is not referenced. You must add a reference to assembly 'CoreCompat.System.Drawing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.The type 'Bitmap' is defined in an assembly that is not referenced. You must add a reference to assembly 'CoreCompat.System.Drawing, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
although I included Using System.Drawing (which includes Bitmap)
UnmanagedImage unmanagedImage = UnmanagedImage.FromByteArray(bytes);
If you're using NuGet, you can install the following package to get System.Drawing, and thus access to the Bitmap class: https://www.nuget.org/packages/CoreCompat.System.Drawing.v2/5.2.0-preview1-r131
Note that this is a prerelease version, so you'll need to check the box that says "Include prerelease" next to the search bar if using the Visual Studio NuGet GUI.

EF Core tools System.Configuration.ConfigurationManager assembly not found

I am creating a new application that is using EF Core 2 with migrations.
The application itself is .net Framework but the model is in a separate .net core 2.0 assembly. Everything is working fine I have defined a designtime context factory:
public class MyDesignTimeContextFactory : IDesignTimeDbContextFactory<MyContext>
{
public MyContext CreateDbContext(string[] args)
{
return new MyContext("Server=localhost\\SQLEXPRESS;Database=DBName;User ID=Test;Password=0000;");
}
}
And I can generate migrations and apply/revert them to the DB.
Now if I replace hardcoded connection string with a call to config file
return new MyContext(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionString");
I have an error when calling EF Core tools:
Add-Migration -Project MyProject.Model -Name Initialization
System.IO.FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=4.0.1.0 ....,
However the nuget is there and I can access ConfigurationManager in ContextFactory (not the designtime one) with no problem when launching the application itself. Looks like EF core tools are not looking for the dependencies of the model assembly...
Is there something I am missing? Maybe it is not possible to use ConfigurationManager in DesignTime context factory?
Finally the problem was in the application project. I had to add the nuget package for System.Configuration.ConfigurationManager to the .Net Framework app so the PackatManager can find it. A bit weired that it works at runtime but not in "design mode".

Is there a way to migrate EF in .netstandard with Xamarin.Forms package?

I have a .netstandard Model layer that referenced from Xamarin.forms.
I set Xamarin.Forms package in Model layer => PrivateAssets="All".
My .netstandard Data layer referenced from Model layer.
When I run this command in PowerShell :
dotnet ef --startup-project ../Eela.Web/ migrations add v1
I have this error :
C:\Users*.nuget\packages\xamarin.forms\2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.targets(40,3): error MSB4062: The
"Xamarin.Forms.Build.Tasks.FixedCreateCSharpManifestResourceName" task
could not be loaded from the assembly
C:\Users*.nuget\packages\xamarin.forms\2.3.4.231\build\portable-win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20\Xamarin.Forms.Build.Tasks.dll.
Could not load file or assembly 'Microsoft.Build.Utilities.v4.0,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
The system cannot find the file specified. Confirm that the
declaration is correct, that the assembly and all its dependencies are
available, and that the task contains a public class that implements
Microsoft.Build.Framework.ITask. [.....\Eela.Model.csproj]
Even I'm using :
<PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
in Data layer but the problem is persisted,
Is there any way to resolve this problem ?
I did not use the migrations with EntityFramework core in a xamarin forms app. I just used
await db.Database.EnsureCreatedAsync()
If you add a .net core console app to the solution I think you can you can use migrations.

How do I load Nuget packages that a custom DLL depends on?

I'm developing a Function App via the Portal (not local development). I have a custom DLL that depends on 1 nuget package: Entity Framework 6.1.3
I have uploaded my DLL to "../bin" and my code compiles successfully when I reference my DbContext object. So far, so good.
I also have a Project.json file and I see it acquiring the nuget packages when I save. So far, so good.
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
My Run.csx code compiles successfully and looks like this:
#r "../bin/Library.dll"
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info("My code: Started");
log.Info(typeof(Library.MyContext).ToString());
}
However, the code doesn't actually run (I don't even see the "My code: Started" log item). The error I receive is:
2017-02-27T06:37:28.731 Exception while executing function:
Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by
the target of an invocation. f-TimerTriggerCSharp1__-205940111: 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.
My custom DLL is the simplest possible EF-referencing DLL I can possibly make. All you need to recreate it is this:
Custom DLL packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net46" />
</packages>
Custom DLL Class1.cs
using System.Data.Entity;
namespace Library
{
public class MyContext : DbContext
{
}
}
What am I doing wrong that is keeping my custom DLL from being able to utilize the downloaded EntityFramework nuget package?
Just to verify that my nuget references are actually working, if I comment out most of my Run.csx code and replace it with this line, all executes correctly and logs what you would expect:
log.Info($"My code: {typeof(System.Data.Entity.DbContext).ToString()}");
As somebody suggested, I have tried changing my Run.csx references to look like this and it doesn't change the runtime error I get (it does compile so the path is correct):
#r "../../../data/Functions/packages/nuget/entityframework/6.1.3/lib/net45/EntityFramework.dll"
#r "../bin/My.dll"
I can also change my Run.csx file to contain this code and it does successfully execute:
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info(typeof(MyContext).ToString());
}
public class MyContext : System.Data.Entity.DbContext
{
}
In order to consume the assemblies coming from your referenced packages, you can deploy your custom dependency as a private assembly:
Deploy the assembly into a bin folder, in your function folder (e.g. wwwroot\myfunction\bin)
Reference the assembly without the relative path, by file name only (e.g. Library.dll)
If you wish to use shared assemblies, deployed to a common location and referenced as you have above, you'd need to deploy the assembly with its dependencies (essentially the output from your project build).
Another option that I would recommend if you want to take advantage of the shared model is to deploy your dependency as a NuGet package (which you can deploy to either a custom source hosted somewhere or as a file), that package would then specify its package dependencies and they would all be resolved automatically.
Looks like configuration problem, you need to define the reference in the project file to be copied to output.
In the Solution Explorer, expand the project, References, right click the EntityFramework, Properties, Set Copy Local = true.
Sometimes the default value is false, so then it will not in the output folder of the project.
I hope this helps.

How do I reference a UWP+NET46 portable library from a .NET 4.6 console application?

I have a portable class library project that targets .NET 4.6 and Universal Windows Platform. This class library contains just one class with the following line of code in its constructor:
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
Now I create a new .NET 4.6 console application project in the same solution and add a project reference to the portable class library. Calling the method that houses the above line of code results in the following exception at runtime:
Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
What am I doing wrong here? There are no compile-time errors or warnings.
Things I have tried: add missing(?) NuGet package manually
It seems that System.IO.FileSystem is a library delivered via NuGet, as part of the Microsoft.NETCore mega-package. Okay, perhaps I need to explicitly add this package to any project that uses my portable class library. I attempt to do so.
Could not install package 'Microsoft.NETCore.Platforms 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
No luck with this approach.
Things I have tried: create a project.json file
While there is no clear info on the web, I read a few tidbits about a new project.json based NuGet harness or build system. Just to experiment, I created the following project.json file in my console application project:
{
"dependencies": {
},
"frameworks": {
"net46": { }
},
"runtimes": {
"win-anycpu": { }
}
}
It works! The runtime error goes away! However, I soon found that this was either not the right solution or not a complete solution. I started writing some code to read configuration section values, which involved making use of the IConfigurationSectionHandler interface, and got the following compile-time error:
error CS0246: The type or namespace name 'IConfigurationSectionHandler' could not be found (are you missing a using directive or an assembly reference?)
This interface is part of the System assembly. I see a reference to this assembly, but it has a yellow exclamation mark icon, and a warning appears in the warnings window:
The referenced component 'System' could not be found.
This is where I ran out of ideas. Am I missing something totally obvious?
I have found the solution. My initial attempt was to install the Microsoft.NETCore package into the console application, resulting in the error shown in my original post.
However, if I install only the narrowly-scoped packages, e.g. System.IO.FileSystem, then I achieve success and the application works correctly. Apparently there is something special about the Microsoft.NETCore "master package" that prevents it from correctly installing into dependent projects.