PCF – PASW natively supports SMB volume shares

Alfus Jaganathan
Cloud Solutions Architect
March 26, 2019
Rate this article
[Total: 3    Average: 4/5]
Views    429

SMB is basically a protocol that allows applications running on a windows based computer to access a network share, which is from the same network or same domain. Apart from network shares, it also supports sharing resources like printers and serial ports across the computers within the same network or domain. Hope it gave a quick idea on what SMB is all about.

According to cloud native application principles/factors, a cloud native application should be not be doing file I/O operations within the container itself. This is because the containers are highly volatile and could disappear and gets recreated any time. During this occurance, the data stored in the file system of that particular container is also lost. Based on this recommendation, it’s always advisable to access the file system externally. Usually we use object storages when dealing with cloud native applications. But let’s say we have a simple use case; we have a network share (within the same network or domain), where an application running in a container needs to access this filesystem, where we may not need a whole big implementation of object storage, just for this use case. Here is where we see some challenges because the windows containers (Windows Diego Cells) where we run the .NET full framework applications, are not attached to any specific domain.

But the good news is that the from PCF v 2.4 (PASW), we get a native support for SMB, which eliminates the usage of 3rd party SMB drivers like SharpCIFS to access these shares. On a side note, there is a service broker available for non-windows containers (PAS), which helps a .NET core application using a SMB share.

In general, we can use net use command to create a local drives, out of a network share and make use of it to access the file system from an application. But thanks to Steeltoe.io providing an API WindowsNetworkFileShare a wrapper around Windows Networking API to handle this in code, very easily just by providing few configurations like username, password, domain, path, etc. Will look int a code sample below.

Note: As per Steeltoe and Pivotal docs, this is currently in beta/experimental mode.

To implement this, first we need to install the NuGet package Steeltoe.Common.Net, latest version is preferable.

Now you can simply wrap all the File I/O operations using the Steeltoe wrapper as in the below code sample.

using System;
using System.IO;
using System.Net;
using Microsoft.Extensions.Configuration;
using Steeltoe.Common.Net;

namespace Smb.Sample
{
    public class FileService
    {
        readonly IConfiguration configuration;
        readonly NetworkCredential credential;

        public FileService(IConfiguration configuration)
        {
            this.configuration = configuration;
            credential = new NetworkCredential(configuration["smb_user"], configuration["smb_password"], configuration["smb_domain"]);
        }

        public void Create(string uncPath, string fileName, string content)
        {
            using (var networkPath = new WindowsNetworkFileShare(uncPath, credential))
            {
                File.WriteAllText(Path.Combine(uncPath, fileName), content);
            }
        }
    }
}

Hope you had fun coding! Appreciate your feedback and comments too!

Subscribe To Our Newsletter

Leave a comment