You can use Server Message Block (SMB) Volumes in the .NET app. In the example presented here, you create an app that reads and writes to a note.txt file within an SMB Volume share.

Prerequisites

To use SMB Volumes in your .NET App, you must have:

  • Visual Studio.
  • An IaaS account.
  • VMware Tanzu Application Service for VMs [Windows].
  • An accessible SMB Volume in an IaaS with a UNC path.
  • An existing .NET Framework app.
  • An SMB user name.
  • An SMB password.

Using SMB volumes in .NET apps with Steeltoe

Steeltoe is a set of libraries that help your team write cloud-native apps. Steeltoe contains functions for mounting SMB Volumes. You must edit your app code to use Steeltoe.

For more information, see the Steeltoe documentation.

If you are using Steeltoe, add the following environment variables to your Config Server Provider:

  • SMB_PATH - the UNC path to your SMB
  • SMB_USERNAME - your IaaS account user name
  • SMB_PASSWORD - your IaaS account password

For more information about adding environment variables to your Config Server Provider, see Config Server Provider in the Steeltoe documentation.

After you add your environment variables to your Config Server Provider, go to SMB mounting.

Using SMB volumes in .NET apps with your batch profile

If you do not want to edit your app code to include Steeltoe, you can use mount SMB volumes without Steeltoe.

To mount SMB Volumes:

  1. Retrieve the UNC of your existing SMB share. The UNC is the fully-qualified domain name (FQDN) of your machine.

  2. Create a .profile.bat file in the root directory of your .NET app.

  3. Use Apps Manager or the Cloud Foundry Command Line Interface (cf CLI) to set these environment variables:

    • SMB_PATH
    • SMB_USERNAME
    • SMB_PASSWORD
  4. Add this command to your .profile.bat file:

    net use z: %SMB_PATH% %SMB_PASSWORD% /USER:%SMB_USERNAME%
    

    Where:

    • SMB_PATH is the UNC path to your SMB.
    • SMB_USERNAME is your IaaS account user name.
    • SMB_PASSWORD is your IaaS account password.

SMB mounting

To configure SMB mounting:

  1. Using the cf CLI or Apps Manager, add SMB_PATH, SMB_USERNAME, and SMB_PASSWORD as environment variables to your local computer or the computer you are using to deploy your app. If Visual Studio does not detect these new environment variables, start Visual Studio again.

    Important If you are using SMB Volumes with your batch profile and have already added these environment variables, ignore this step.

  2. In Visual Studio, create a new file in your solution named SMBConfiguration.cs. This file is the single representation of your SMB Volume configuration and reads the connection data from the environment variables you established previously. Include the following in your SMBConfiguration.cs file:

    // SMBConfiguration.cs
    using System;
    
    namespace NetFrameworkApp.Controllers
    {
        public class SMBConfiguration
        {
            public String GetSharePath()
            {
                return Environment.GetEnvironmentVariable("SMB_PATH");
            }
    
            public String GetUserName()
            {
                return Environment.GetEnvironmentVariable("SMB_USERNAME");
            }
    
            public String GetPassword()
            {
                return Environment.GetEnvironmentVariable("SMB_PASSWORD");
            }
        }
    }
    
  3. In Visual studio, create a new MVC Controller named NoteController. This file creates a controller endpoint that reads the example note file.

    For more information about creating a controller, see the Microsoft documentation.

  4. Use your package manager to add Steeltoe.Common and Steeltoe.Common.Net to your app. If you are not using Steeltoe, ignore this step.

  5. Edit NoteController.cs to read from a file named note.txt. This note.txt is created by the FileMode.OpenOrCreate method. See the following example code snippet, which reads the contents of the note file and stashes the note.txt content in the Viewbag. If you are not using Steeltoe, ignore the reference to Steeltoe.Common.Net.

    // NoteController.cs
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using Steeltoe.Common.Net;
    
    namespace NetFrameworkApp.Controllers
    {
        public class NoteController : Controller
        {
            SMBConfiguration configuration = new SMBConfiguration();
            public ActionResult Index()
            {
    
                var credential = new NetworkCredential(configuration.GetUserName(), configuration.GetPassword());
    
                using (var share = new WindowsNetworkFileShare(configuration.GetSharePath(), credential))
                using (var inputStream = new FileStream(Path.Combine(configuration.GetSharePath(), "note.txt"), FileMode.OpenOrCreate))
                using (var streamReader = new StreamReader(inputStream))
                {
    	    // Never display raw user input as HTML. Do not do this in production code.
                    ViewBag.Note = streamReader.ReadToEnd();
                }
    
    
                return View();
            }
        }
    }
    
  6. In Visual Studio, create a folder named Note in the Views folder.

  7. In Visual Studio, create a new View named Index.

For more information about Views, see Views in ASP.NET Core MVC and Add a view to an ASP.NET Core MVC app in the Microsoft documentation.

  1. In Visual Studio, create an Index.cshtml file that contains:

    // Index.cshtml
    
    @ViewBag.Note
    

    If you run the app now, you see an empty page with no errors.

  2. Edit the Index.cshtml file to contain a form. This form posts to a yet-to-be-created update endpoint and also displays the note inside a text area.

    <big>
    <xmp>
    // Index.cshtml
    ...
    <form action="/note/update" method="post">
        <textarea name="note">@ViewBag.Note</textarea>
        <div>
            <button type="submit">Update</button>
        </div>
    </form>
    </xmp>
    </big>
    
  3. Edit the NoteController.cs to have an update endpoint. This endpoint saves the data posted to the endpoint back into the note.txt. See the example code snippet in this procedure.

  4. Edit the NoteController.cs to include the ControllerBase class RedirectToAction method, which redirects to the index page so the user can see what was just saved. For more information about the ControllerBase class, see the Microsoft documentation.

    // NoteController.cs
    namespace NetFrameworkApp.Controllers
    {
        public class NoteController : Controller
        {
          ...
    
            [HttpPost]
            public ActionResult Update(String note)
            {
                var credential = new NetworkCredential(configuration.GetUserName(), configuration.GetPassword());
    
                using (var share = new WindowsNetworkFileShare(configuration.GetSharePath(), credential))
                using (var outputStream = new FileStream(Path.Combine(configuration.GetSharePath(), "note.txt"), FileMode.Create))
                using (var streamWriter = new StreamWriter(outputStream))
                {
                    streamWriter.Write(note);
                }
    
                return RedirectToAction("Index");
            }
        }
    }
    

Known issues

Here is a list of the known issues.

Inability to SSH into app instance

Even though you created the SMB mapping, you cannot run cf ssh into that app instance. Trying to access the created mapping through SSH causes an unspecified path error.

check-circle-line exclamation-circle-line close-line
Scroll to top icon