I'm trying to implement azure functions v2 with connections to a blob storage and an SQL Database.
There are a lot of packages that are not supported anymore; and I found out that Microsoft.WindowsAzure.Configuration
is not supported anymore and that I need to use ASP.NET Core Configuration
.
I tried implementing that as described in this article, but I keep getting an error inside the azure cli:
System.Private.CoreLib: Exception while executing function: QueueProcessor. QueueProcessorAzureFunction: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Extensions.Configuration.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Also when trying to access the sql database like usual:
using (var context = new DBEntities)
{
[…]
}
I get another error in the cli:
System.Private.CoreLib: Exception while executing function: QueueProcessor. EntityFramework: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. EntityFramework: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
What am I missing? Could anyone please help?
Thank you
EDIT:
Requested code:
namespace QueueProcessorAzureFunction
{
public class StorageContext
{
private CloudStorageAccount _storageAccount;
public StorageContext(ExecutionContext context)
{
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
_storageAccount = CloudStorageAccount.Parse(config.GetConnectionString("BlobStorage"));
}
public CloudBlobClient BlobClient
{
get { return _storageAccount.CreateCloudBlobClient(); }
}
public CloudTableClient TableClient
{
get { return _storageAccount.CreateCloudTableClient(); }
}
public CloudQueueClient QueueClient
{
get { return _storageAccount.CreateCloudQueueClient(); }
}
}
}
Inside the queue trigger azure function:
public static void Run([QueueTrigger("stack", Connection = "AzureWebJobsStorage")]string myQueueItem, TraceWriter log, ExecutionContext context)
{
var storageContext = new StorageContext(context);
[…]
}
Inside the local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "[…]",
"AzureWebJobsDashboard": "",
"BlobStorage": "[…]"
},
"ConnectionStrings": {
"DBEntities": {
"ConnectionString": "[…]",
"ProviderName": "System.Data.EntityCLient"
},
"BlobStorage": "[…]"
}
}
EDIT 2
OK, so the error occurs when I define var config; for now I'll simply pass the connection string in the Storage context constructor as such:
public StorageContext(ExecutionContext context)
{
_storageAccount = CloudStorageAccount.Parse("CompleteConnectionString");
}
EDIT 3
So for the problem with the SQL Database, given that my database is in a second project, where I add an ADO.NET Entity Data Model; I'm not sure how I can resolve the problem.
From what I figured, in .NET Standard I need to use Dependencies Injection into the DB Context now. But given that I have a database first model, I don't have an DB context...
EDIT 4
Entity Framework Core... I guess you need to use Entity Framework Core with Azure function v2... try THIS.
EDIT 5
Ok.
First, trying to use Scaffold-DbContext with NuGet console to generate your DBContext in a .NET Standard project does not work. Consequently, Azure functions v2 that are of .NET Standard 2.0 can't actually use Scaffold-Dbcontext
to generate their DbContext.
To work around that I made a second .Net Core project used the Scaffold-DBcontext command to generate the Dbcontext and the tables and then simply copied them to my Azure function project. Actually I used the Developer Command prompt for VS, but I assume using NuGet console would be the same. Here is a useful link to do it.
Next you have to use dependency injection. And this is where I got sort of stuck... And that is due to NuGet packet version management. Here is useful link to understand how to implement Dependency Injection in azure functions.
Simply: After installing the package AzureFunctions.Autofac.2.0.0
(NOTE THE VERSION), and Autofac.4.8.1
, add a new class file (lets call it config.cs
);Inside:
public class Config
{
public Config()
{
DependencyInjection.Initialize(builder =>
{
builder.RegisterType<DBContext>();
});
}
}
Now you pass the Context as a parameter, such as [Inject]DBContext context
, then you need to add [DependencyInjectionConfig(typeof(AutofacConfig))]
right below the namespace and I guess you could access the Database now; using Linq I can write stuff like:
var a = context.Studies.Where(s => s.Id == 10569).FirstOrDefault();
This is why I assume this works.
Overall this is how my function looks like:
namespace FunctionApp10
{
[DependencyInjectionConfig(typeof(Config))]
public static class Function1
{
[FunctionName("Function1")]
public static void Run([QueueTrigger("queue", Connection = "AzureWebJobsStorage")]string myQueueItem, TraceWriter log, [Inject]DBContext context)
{
var a = context.Studies.Where(s => s.Id == 10569).FirstOrDefault();
}
}
}
But when I build I get the error
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Azure.WebJobs, Version=2.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
And I guess that is because it is looking for version 2.1.0 while I have 3.0.0-beta5. I tried editing the csproj with no success.
Now if you try to download AzureFunctions.Autofac.3.0.5
it uninstalls Windows.Azure.Storage.8.6.0
and even if you reinstall it manually, you get errors like:
The type or namespace name 'QueueTriggerAttribute' could not be found (are you missing a using directive or an assembly reference?)
My Issue on GitHub.
That's about it... Hope someone figures it out.
User contributions licensed under CC BY-SA 3.0