This topic tells you how to use Server Message Block (SMB) Volumes in .NET app. In this example, 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 username.
  • 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 functionality for mounting SMB Volumes. Using Steeltoe requires modifying your app code. For more information, see the Steeltoe documentation.

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

  • SMB_PATH
  • SMB_USERNAME
  • SMB_PASSWORD

Where:

  • SMB_PATH is the UNC path to your SMB.
  • SMB_USERNAME is your IaaS account username.
  • SMB_PASSWORD is 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 adding your environment variables to your Config Server Provider, proceed to SMB Mounting.

Using SMB Volumes in .NET Apps with your Batch Profile

If you do not want to modify 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 your machine’s fully-qualified domain name (FQDN).

  2. Create a .profile.bat file in your .NET app’s root directory.

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

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

    net use z: %SMB_PATH% %SMB_PASSWORD% /USER:%SMB_USERNAME%
    
  5. Proceed to SMB Mounting.

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, restart Visual Studio.

    Important If you are using SMB Volumes with your batch profile and have already added these environment variables, you can skip 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 Add a controller to an ASP.NET Core MVC app in 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 does not exist yet but will be 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 in the following code snippet.

    // 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 subdirectory in Views named Note.

  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.

  8. In Visual Studio, create an Index.cshtml file that contains the following:

    // Index.cshtml
    
    @ViewBag.Note
    

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

  9. Modify the Index.cshtml file to contain a form. This form posts to a yet-to-be-created update endpoint and also displays our 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>
    
  10. Modify 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 the following step.

  11. Modify 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 ControllerBase.RedirectToAction Method in the Microsoft documentation. See the following example code snippet:

    // 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

Inability to SSH into App Instance

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

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