Azure

Create an Azure Perform Triggered By Blob Storage Utilizing C#

On this article, we’ll offer you an illustration of a primary Azure Perform that can set off an event when a document will get transferred to Azure Blob storage and moreover will monitor and save these qualities in Database using EntityFrameworkCore, Dependency Injection, and SQL Database.

  • An Azure Storage account
  • SQL Database 
  • Visual Studio with the Azure Growth workload enabled. you can too create Azure Perform instantly within the portal, however the visible studio is most popular if you need straightforward supply management integration.

Create a Storage account in Azure

 

I’ve already blogged in my earlier article that Add information to Azure blob storage through which I’ve clearly outlined all of the steps to create the storage account in azure  Azure Blob Storage.

 

Create an Azure Perform utilizing Visual Studio 

 

Open the Visual Studio and click on on the create a brand new challenge. Select Azure Capabilities from the record of accessible challenge templates.

 

 

 

Within the subsequent part, present your challenge identify and placement to retailer the challenge in your machine.

 

Create A Azure Function Triggered By Blob Storage Using C#

 

Within the following display, you may select the goal framework for Azure capabilities (v2 .Web Core) and Blob Set off as the kind of set off.

 

Create A Azure Function Triggered By Blob Storage Using C# 

 

Regardless that v3 is the most recent model, we’re selecting the v2 as a result of some packages usually are not supporting in v3. Select the Blob set off template. On the right-hand aspect, you may select the storage account since we already configured the storage account from the portal itself we simply want so as to add the connectionstring within the software itself.

 

we’re utilizing EntityFrameworkCore and Dependency Injection to work together with SQL Database

 

 

Required Packages  

 

It is essential to pick out the proper model quantity that is appropriate with the model of .Web you are working on and I’ve additionally referenced Microsoft.Azure.Capabilities.Extensions NuGet bundle which supplies us entry to the dependency injection function.

 

Create A Azure Function Triggered By Blob Storage Using C# 

 

Database – Desk schema 

  1. CREATE TABLE [dbo].[FileRecords]  
  2. (  
  3.     [Id] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY,   
  4.     [FileName] NVARCHAR(50) NOT NULL,   
  5.     [IsCompleted] BIT NOT NULL,   
  6.     [CreatedTime] DATETIME NOT NULL  
  7. )  

Create an EF Core mannequin for FileRecords merchandise entity and customized DbContext.

 

FileRecord.cs 

 

For the reason that Id and Created Date values are being generated by the machine dynamically so we outlined them within the Mannequin itself.  

  1. utilizing System;  
  2.   
  3. namespace BlobTrigger_AzureFunction.Fashions  
  4. {  
  5.    public class FileRecords  
  6.     {  
  7.         public Guid Id { getset; } = System.Guid.NewGuid();  
  8.         public string FileName { getset; }  
  9.         public bool IsCompleted { getset; }  
  10.         public DateTime CreatedDate { getset; } = DateTime.UtcNow;  
  11.     }  
  12. }  

AppDbContext.cs

  1. utilizing Microsoft.EntityFrameworkCore;  
  2.   
  3. namespace BlobTrigger_AzureFunction.Fashions  
  4. {  
  5.     public class AppDbContext : DbContext  
  6.     {  
  7.         public AppDbContext(DbContextOptions<AppDbContext> choices) : base(choices) { }  
  8.         public DbSet<FileRecords> FileRecords { getset; }  
  9.     }  
  10. }  

Initialize Dependency Injection

 

To arrange the dependency injection for our perform app we use the FunctionStartup attribute on the meeting to point a startup class that can run when the perform app begins. Create a startup class and outline the  SQL connection string and register a DbContext within the providers, which can permit us to inject the AppDbContext  into our perform.

 

Startup.cs 

  1. utilizing BlobTrigger_AzureFunction.Fashions;  
  2. utilizing Microsoft.Azure.Capabilities.Extensions.DependencyInjection;  
  3. utilizing Microsoft.EntityFrameworkCore;  
  4. utilizing Microsoft.Extensions.DependencyInjection;  
  5.   
  6. [assembly: FunctionsStartup(typeof(BlobTrigger_AzureFunction.Startup))]  
  7.   
  8. namespace BlobTrigger_AzureFunction  
  9. {  
  10.     public class Startup : FunctionsStartup  
  11.     {  
  12.         public override void Configure(IFunctionsHostBuilder builder)  
  13.         {  
  14.             string connectionString = “Your SQL Connection String”  
  15.             builder.Providers.AddDbContext<AppDbContext>(  
  16.                 choices => SqlServerDbContextOptionsExtensions.UseSqlServer(choices, connectionString));  
  17.         }  
  18.     }  
  19. }  

Injecting DbContext right into a perform – Constructor Injection

 

Function1.cs 

  1. public class Function1  
  2.     {  
  3.         #area Property  
  4.         non-public readonly AppDbContext appDbContext;  
  5.         #endregion  
  6.  
  7.         #area Constructor  
  8.         public Function1(AppDbContext appDbContext)  
  9.         {  
  10.             this.appDbContext = appDbContext;  
  11.         }  
  12.         #endregion  
  13.   
  14.         // ….. perform outlined right here 
  15.     }  

Organising the connection string for Blob Storage

 

When Azure Perform runs, it must understand how to hook up with the blob container. Visual Studio has created a file known as native.settings.json, we initially created the Storage account from my earlier article there I’ve talked about copying the connection string from Entry keys. Now we have now so as to add the connection string in native.settings.json  
  1. {  
  2.   “IsEncrypted”false,  
  3.   “Values”: {  
  4.     “AzureWebJobsStorage”Your Blob storage connection string,  
  5.     “FUNCTIONS_WORKER_RUNTIME”“dotnet”  
  6.   
  7.   }    
  8. }  

Within the boilerplate code, there are two essential variables created, identify and myBlob

 

Create A Azure Function Triggered By Blob Storage Using C#
  • identify – holds the identify of the file discovered within the blob container.
  • myBlob – holds the content material of the file.
  • filecontainer – precise container identify created in blob storage.
  • connection – blob storage connection string.

Saving the Particulars to Database

  1. utilizing System.IO;  
  2. utilizing BlobTrigger_AzureFunction.Fashions;  
  3. utilizing Microsoft.Azure.WebJobs;  
  4. utilizing Microsoft.Extensions.Logging;  
  5.   
  6.   
  7. namespace BlobTrigger_AzureFunction  
  8. {  
  9.     public class Function1  
  10.     {  
  11.         #area Property  
  12.         non-public readonly AppDbContext appDbContext;  
  13.         #endregion  
  14.  
  15.         #area Constructor  
  16.         public Function1(AppDbContext appDbContext)  
  17.         {  
  18.             this.appDbContext = appDbContext;  
  19.         }  
  20.         #endregion  
  21.   
  22.         [FunctionName(“Triggerwhenfileuploads”)]  
  23.         public void Run([BlobTrigger(“filecontainer/{name}”, Connection = “AzureWebJobsStorage”)] Stream myBlob, string identify, ILogger log)  
  24.         {  
  25.             log.LogInformation($“C# Blob set off perform Processed blobn Identify:{identify} n Measurement: {myBlob.Size} Bytes”);  
  26.             appDbContext.FileRecords.Add(new FileRecords  
  27.             {  
  28.                 FileName = identify,  
  29.                 IsCompleted = true  
  30.             });  
  31.              appDbContext.SaveChanges();  
  32.         }  
  33.     }  
  34. }  

Run the perform  

 

Press F5 after profitable Construct. We are able to see the next particulars within the command window.

 

Create A Azure Function Triggered By Blob Storage Using C#

 

Let’s add a file within the blob container by way of the Azure portal and see the result’s being up to date within the Database.

 

Create A Azure Function Triggered By Blob Storage Using C#

 

After profitable add of a file, right here we will see an occasion is triggered for our perform we will see the log within the command window,

 

Create A Azure Function Triggered By Blob Storage Using C# 

 

After executing the perform under is the ultimate output with the file identify and its dimension is displayed as outlined within the logs,

 

Create A Azure Function Triggered By Blob Storage Using C# 

 

Information being saved within the Database as anticipated.

 

Create A Azure Function Triggered By Blob Storage Using C#

 

 

Abstract

 

The brand new dependency injection function of Azure Capabilities makes it quite simple to work with Entity Framework Core database contexts inside an Azure Capabilities app, although there is no such thing as a specific EF Core binding for Azure Capabilities.

 

Thanks for studying, please let me know your questions, ideas, or suggestions within the feedback part. I respect your suggestions and encouragement.

 

Continue to learn….!

Tags
Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close