# Using Wisej.NET to Access Blob Storage with Microsoft Azure

*by* [*Iulia Pitutiu*](/authors/iulia-pitutiu.md)

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](https://www.nuget.org/packages/Wisej-3/)
* Install `Azure.Storage.Blobs` 12.10 library in your solution:

  ![Image 1](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture1.png)
* Install `System.Runtime.CompilerServices.Unsafe` version 4.6.0:

  ![Image 2](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture2.png)

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

  ![Image 3](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture3.png)
* Install `System.Buffers` version 4.5.1:

  ![Image 4](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture4.png)

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

![Image 5](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture5.png)

#### 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`:

```csharp
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](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-create?toc=%2Fazure%2Fstorage%2Fblobs%2Ftoc.json\&tabs=azure-portal#create-a-storage-account-1) 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:

```csharp
/// </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:

```csharp
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](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture9.png)

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

![Image 7](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture10.png)

![Image 8](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture11.png)

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](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture12.png)

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

![Image 10](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture13.png)

**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](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture14.png)

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

![Image 12](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture15.png)

The result will be uploaded in Azure Storage:

![Image 13](https://www.codeproject.com/KB/TipsnTricks/5336545/Picture16.png)

### 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)](https://github.com/iceteagroup/wisej-extensions)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.learnwisej.net/data-access-with-wisej.net/using-wisej.net-to-access-blob-storage-with-microsoft-azure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
