Click here to Skip to main content
15,884,629 members
Articles / Programming Languages / C#
Tip/Trick

Using Wisej.NET to Access Blob Storage with Microsoft Azure

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
16 Aug 2022CPOL3 min read 5K   33   6   2
This document describes how you can benefit from using Azure files in your .NET web application.
During a web-enabling process, one of our standard tasks that we analyze with our customers revolves around file management. Our experience has proven that desktop applications heavily use file manipulation, therefore we need to provide modern options to support this feature within web-enabled applications.

Introduction

Wisej.NET Framework is very flexible and extensible when it comes to file handling. It provides an abstract interface for file access enabling customers to use any file system that their web applications need: either the built-in default implementation for the standard disk-based file system or several alternative implementations for cloud-based file storage.

Nowadays, it is attractive to deploy web applications to an environment like Microsoft Azure. Therefore, it makes sense to use an Azure file integration as a supplement or total replacement for traditional on-premise file servers.

How to Use Azure Blob Storage with Wisej.NET

In this article, I am describing the way a Wisej.NET web application can interact with cloud files stored on Microsoft Azure Blob Storage. To connect your web application to Azure Blob Storage, there are a couple of easy steps to follow.

Prerequisites

  • Azure subscription
  • Azure storage account
  • Reference Wisej.NET
  • Install Azure.Storage.Blobs 12.10 library in your solution:

    Image 1

  • Install System.Runtime.CompilerServices.Unsafe version 4.6.0:

    Image 2

    The Compiler Services will require a binding redirect in the Web.config file:

    Image 3

  • Install System.Buffers version 4.5.1:

    Image 4

The System.Buffers will require a binding redirect in the Web.config file:

Image 5

The Implementation of AzureBlobProvider

The file system implementation for the Azure Blob storage needs to be placed in a class that implements the Wisej.NET IFileSystemProvider, I named my new class AzureBlobProvider:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Wisej.Core;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using System.Threading.Tasks;
using System.Text;
using System.IO;
using System.Configuration;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using Azure;

namespace AzureBlobStorageSample
{
    /// <summary>
    /// Implementation of the Azure Blob Storage provide.
    /// Provides access to the Azure Blog Storage as a file system.
    /// </summary>
    public class AzureBlobProvider : IFileSystemProvider
    {
        /// <summary>

The authentication to the Blob storage can be done in many ways like the Microsoft documentation indicates – the actual authentication is not in the current scope of this article, but rather the file access part is more relevant. Independent of the authentication type, for the actual Azure Blob storage connection, we need to know the account and container name – which is stored in our AzureBlobProvider class members:

C#
/// </summary>
public string AccountName
{
    get { return this.accountName; }
    set
    {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentNullException("accountName");
        
        this.accountName = value.Trim();
    }
}
private string accountName;

/// <summary>
/// Returns or sets the name of the Azure Blob Container
/// </summary>
internal string BlobContainer
{
    get { return this.blobContainer; }
    set
    {
        if (string.IsNullOrEmpty(value))
            throw new ArgumentNullException("blobContainer");
        this.blobContainer = value.Trim();
    }
}
private strong blobContainer;

Scenario 1: Import Operation

For a standard import operation, the end user should be able to select the file path from the Azure storage. To achieve the file import, I have used a Wisej.NET Open File Dialog where I added both a local folder and an Azure Blob storage container just to show case that both file systems can be used together:

C#
private void btOpenFile_Click(object sender, EventArgs e)
{
    var fileContent = string.Empty;
    var filePath = string.Empty;
    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.Title = "Select file to import";
        openFileDialog.Roots.Add(new FileSstemProvider("\\network shared path\subpath01\...  ", "My Local Files"));
        openFileDialog.Roots.Add(this.AzureBlobProvider);
        openFileDialog.Filter = "Text Files (*.txt)|*.txt|CSV Files (*.csv)|*.csv|XML Files (*.xml)|*.xml";
        openFileDialog.FilterIndex = 3;
        openFileDialog.AddExtension = true;

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            txtImportFilePath.Text = openFileDialog.FileName;
        }
    }
}

If we run the sample, the user is prompted with a file picker dialog – as shown, the Azure Blob Storage folder structure is displayed:

Image 6

This is possible because the Wisej.NET Open File Dialog calls GetDirectories and GetFiles methods from our AzureBlobProvider class:

Image 7

Image 8

For displaying the file properties, the AzureBlobProvider class implements methods like GetFileSize or GetCreationTime.

After the user selects the imported file, the web application may require processing the file content. On the server side, this operation means downloading the Azure Blob item and reading its content.

Image 9

The implementation for downloading and reading a Blob item’s content is here:

Image 10

Scenario 2: Export Operation

Another common scenario is when the web application is generating a report which should be uploaded to either a pre-defined or a user-defined Azure location. To simulate the export, in my sample, you can type in the file content and then click the Upload button:

Image 11

After clicking the Upload button, the Save File Dialog allows you to specify the location and the name of your file:

Image 12

The result will be uploaded in Azure Storage:

Image 13

Conclusion

Wisej.NET is a very powerful web application framework which enables taking advantage of the latest .NET technologies.

For further examples on how to implement cloud storage file access, I recommend the Amazon S3 file system implementation provided by ITG, which was also my support for developing the Azure Storage file system provider.

wisej-extensions · iceteagroup/wisej-extensions (github.com)

History

  • 16th August, 2022: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead fecher
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 986659918-Aug-22 4:31
Member 986659918-Aug-22 4:31 
GeneralMy vote of 5 Pin
Tavi Butisca16-Aug-22 9:23
Tavi Butisca16-Aug-22 9:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.