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 SMBSMB_USERNAME
- your IaaS account user nameSMB_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:
-
Retrieve the UNC of your existing SMB share. The UNC is the fully-qualified domain name (FQDN) of your machine.
-
Create a
.profile.bat
file in the root directory of your .NET app. -
Use Apps Manager or the Cloud Foundry Command Line Interface (cf CLI) to set these environment variables:
SMB_PATH
SMB_USERNAME
SMB_PASSWORD
-
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:
-
Using the cf CLI or Apps Manager, add
SMB_PATH
,SMB_USERNAME
, andSMB_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.
-
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 yourSMBConfiguration.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"); } } }
-
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.
-
Use your package manager to add
Steeltoe.Common
andSteeltoe.Common.Net
to your app. If you are not using Steeltoe, ignore this step. -
Edit
NoteController.cs
to read from a file namednote.txt
. Thisnote.txt
is created by theFileMode.OpenOrCreate
method. See the following example code snippet, which reads the contents of the note file and stashes thenote.txt
content in the Viewbag. If you are not using Steeltoe, ignore the reference toSteeltoe.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(); } } }
-
In Visual Studio, create a folder named
Note
in the Views folder. -
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.
-
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.
-
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>
-
Edit the
NoteController.cs
to have an update endpoint. This endpoint saves the data posted to the endpoint back into thenote.txt
. See the example code snippet in this procedure. -
Edit the
NoteController.cs
to include the ControllerBase classRedirectToAction
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.
Content feedback and comments