X

Using Oracle Cloud Infrastructure Software Development Kit for .NET with Visual Studio IDE

Viral Modi
Principal Member of Technical Staff

In my previous blog, I introduced our new developer product Oracle Cloud software development kit (SDK) for .NET to you. In this one, I will demonstrate how to use it with one of the most popular and complete integrated development environments (IDEs) Visual Studio. Visual Studio is available on Windows and was introduced for Mac OS in 2019. In this blog, I use Windows.

I cover steps for onboarding to Oracle Cloud, general setup required to use Oracle Cloud developer tools, including config specific to .NET software development kit (SDK) in more detail. I conclude with how to create, run, and debug a .NET Console application using .NET SDK in Visual Studio IDE, which pushes an object at a web URL to Oracle Cloud Object Storage service.

Oracle Cloud Free Tier Offering

If you’re new to Oracle Cloud, the first step is creating an Oracle Cloud Infrastructure account. Oracle Cloud Free Tier is a great way to achieve that. Few highlights of Oracle Cloud Free Tier:

  • Access to many always free services available for an unlimited time with no interruption.

  • Free Trial period of 30 days with US$300 of free credits, in addition to the free services mentioned.

  • Always Free and Free Trial instances can be seamlessly upgraded to paid service at any time if and when you need to scale up.

Configuration for Authentication with Oracle Cloud

Oracle Cloud Infrastructure developer tools require a certain configuration for authentication to the cloud. The config appears in a file placed at a default location of ~/.oci/config, at runtime, or dynamically in the program source code when using SDKs. In this blog, I show you how to create this config file in Windows OS. Except for a minor change in key generation commands, everything else remains the same for other OS platforms. The configuration file appears as the following code block or similar:

user=ocid1.user.oc1..
key_file=/Users/viralmodi/.oci/my_oci_api_key.pem
fingerprint=f7:12:34:56:78:90:ab:bc:cd:de:ef:98:87:65:43:21
tenancy=ocid1.tenancy.oc1..
region=us-sanjose-1

The config file is INI format and starts with a profile name string. If no profile name is specified, Oracle Cloud developer tools uses DEFAULT is the default profile name. Now, let's populate all the other values for each key.

User

  • Sign in to the Oracle Cloud Infrastructure Console using the account credentials that you created using the previous instructions.

  • Go to Profile, then User Settings.

  • The user Oracle Cloud ID (OCID) is shown under User Information. OCIDs are Oracle Cloud Resource Identifiers. They’re unique to every resource.

2)key_file and Fingerprint

  • The key file includes the RSA key pair in PEM format. On Windows OS, install Git bash for Windows to get OpenSSL on your machine for key generation.

  • The following commands generate this key pair:

mkdir ~/.oci
openssl genrsa -out ~/.oci/oci_api_key.pem -aes128 -passout stdin 2048 --> Enter the passphrase when prompted. It will create RSA private key encrypted with passphrase.
chmod go-rwx ~/.oci/oci_api_key.pem  --> Remove rwx permissions for group and others.
openssl rsa -pubout -in ~/.oci/oci_api_key.pem -out ~/.oci/oci_api_key_public.pem -passin stdin --> Generate public key.

For macOS or linux, the only change to the above steps would be to open a Terminal (instead of Git Bash) and remove the "--passout stdin/--passin stdin" command line option from the above commands. For more details regarding key pair generation, please refer this documentation

  • Upload your public key to Oracle Cloud Console. Use the private for signing Oracle Cloud Infrastructure requests from your client machine, and Oracle uses the public key to authenticate your request.

  • For uploading the public API key, sign in to console, go to Profile, User Settings, API Keys, and then Add Public.

  • Once the public key is added, you can see the key fingerprint value on the Console screen.

Tenancy

Log in to Oracle Cloud Console. Go to Navigation Menu (shown as triple horizontal lines on the top left of screen). Then, under Administration, click Tenancy Information. The tenancy OCID is shown under Tenancy Information.

Region

  • This region refers to the Oracle Cloud Infrastructure Region.

  • Use the same region that you registered while creating your free tier account. To find the region name to be used in the config file, log in to Console.

  • In the Navigation Menu, go to Administration, and click

Now, the config file is complete and you can use it with any Oracle Cloud developer tools for authentication.

Using Oracle Cloud Infrastructure SDK for .NET with Visual Studio

When installing Visual Studio, install the “.NET desktop development” workload to ensure that Visual Studio IDE supports building and running .NET projects once installed.

Creating New .NET Core Application

  1. Open Visual Studio.

  2. Click Create a New Project.

  3. Visual Studio gives the option to pick from existing project templates. In the first column, filter by languages and choose C#.

  4. From the list of project templates, click Console App (.NET Core).

  5. In the next window, name the project “TestConsoleApp” and click Create. Two important files are created: TestConsoleApp.sln and TestConsoleApp.csproj. If there are multiple projects in single solution, the sln file contains information about all your projects. The csproj file has properties and information on each project that make up the sln. In this case, we have one csproj in our sln file.

Loading an Existing Project

  1. Open Visual Studio.

  2. Click Open a project or solution.

  3. In the file explorer window that opens, browse to the project path that you want to open.

  4. Go to the .sln file for that project. Click the solution (.sln) file and click Open.

Configuring your Project in Visual Studio IDE

Before you start coding, it’s useful to be familiar with some useful IDE features.

Import NuGet Packages

Because this is a .NET Project, our application might need some NuGet packages such as Oracle Cloud .NET SDK packages for managing Oracle Cloud resources. To add packages, open Project and select Manage NuGet Packages from the top menu items.

The NuGet Packages window opens. Click the Browse tab within it. In the search box, search for “OCI.” You can see all Oracle Cloud .NET SDK packages.

My .NET application pushes an object to Oracle Cloud Object Storage Service. So let’s install “OCI.DotNetSDK.Objectstorage” package. Search for this string and click the package. On the right side on the window, details for this package appear, including drop-down of versions and the Install button. Click Install to install the latest version of the package. Installing it also installs all its dependencies. Installed packages for your application show up in the Installed tab on this window. Verify that the ObjectStorage package is installed there.

Environment Variables

While using the IDE, you might need to add environment variables, which are used later in your source code. For example, we can save the Object Storage Bucket Name and Object Name as environment variables.

For storing environment variables within Visual Studio IDE, right-click the TestConsoleApp project name on the right in Solution Explorer, and click Properties.

Go to Debug, then Environment Variables, and click Add. Add the following variables:

  • BUCKET_NAME=testbucket

  • OBJECT_NAME=testobject.jpg (because we work with a jpg image)

You can also add these variables as string variables in our program.

Visual Studio doesn't autosave like some other IDEs. Press Ctrl+S to save the settings.

Build and Execute

Open the Program.cs from Solution Explorer. To access and print the environment variables we created to learn how to run and execute our program, we add this small piece of code to our main program.

static void Main(string[] args)
{
	var bucketName = Environment.GetEnvironmentVariable("BUCKET_NAME");
	var objectName = Environment.GetEnvironmentVariable("OBJECT_NAME");
	Console.WriteLine("Storing " + objectName + " in " + bucketName + " on Oracle Cloud");
}

Press Ctrl+F5 to build and run the program, or Click Debug Menu, and then Start without Debugging. You should see the output window and our two environment variables printed.

Logging

Oracle Cloud Infrastructure SDK for .NET uses NLog package for logging. We can log in our test project using the same method. It also ensures that the Oracle Cloud .NET SDK logs also get printed on Console.

Use the same NLog.config that the Oracle Cloud SDK for .NET is using. Copy this NLog.config to the root folder of the TestConsoleApp project. You can do either use the file explorer of your operating system or by right-clicking the TestConsole App, then choosing Add, New Item ..., and the text file. Rename the text file to “NLog.config,” and copy the contents from the .NET SDK NLog.config to this one, you just created.

Now, we need to update our projects .csproj file to point to this file. In the Solution Explorer, right-click TestConsoleApp, and click Edit Project File.

The TestConsoleApp.csproj file opens in editor. Update the file by adding the following section:


     
          PreserveNewest
     

With the previous steps done, we can see any logs printed by .NET SDK calls from within our application. To further use logging in our application itself, create a logger with the following command:

Press Ctrl+F5 to run the program and verify that the log is still printed.

Debug

Visual Studio makes it easy to debug our program. We can insert a breakpoint on any line by pressing F9. F9 toggles breakpoints. We can also right-click any line of the code and select Breakpoints, then Insert Breakpoint from the menu.

To run in debug mode, press F5 (instead of Ctrl+F5) to hit the breakpoints.

Oracle Cloud Infrastructure .NET Application for Pushing a Web Image to Oracle Cloud Object Storage Service

Now, I attach the entire code snippet of my program that pushes an Oracle logo image to Oracle Cloud Object Storage service. In this process, we perform the following steps:

  • Get an Object Storage namespace, an uneditable and unique Oracle Cloud system-generated string

  • Create a bucket, a logical container for storing objects

  • Download and push the image, pointed to by the web URL, to Object Storage bucket

In my source code, I used all the concepts that I introduced in the "Configure" section. I have also liberally commented my code for better understanding. Consider the following concepts when you code:

Use Intellisense in Visual Studio whenever possible. When using an Oracle Cloud .NET package API call, if you see a call underlined in red, press Alt+Enter or right-click for IDE suggestions on a fix. IDE automatically adds the “using” statement for the classes you’re using when you do this.

When using Visual Studio, save after every change to config files or source code files. Press Ctrl+S to save changes or your changes aren’t saved.



Note: The above source code is scrollable

Press Ctrl+F5, and you should see the following output.

As a final step, I leave it up to you to add the getObject call in the previous code to verify the uploaded image. Or you can log in to Console, and verify that your bucket was created and the image uploaded within it. This is what you should see if you login to console.

Summary

In this blog, we learned how to do the following processes:

  • Create and use an Oracle Free Tier account.

  • Configure and authenticate with Oracle Cloud when using Oracle Cloud developer tools.

  • Use Visual Studio with Oracle Cloud SDK for .NET to create a .NET Console application.

  • Push a web image to Object Storage service in a .NET application.

A fully fledged IDE like Visual Studio, used along with Oracle Cloud SDK for .NET, can greatly enhance your productivity when working on Oracle Cloud. I encourage you to try Oracle Cloud Infrastructure SDK for .NET with Visual Studio on Windows or Mac platforms. If you have another favorite IDE, you can easily follow these instructions to do the same on that IDE. If you want us to cover another Oracle Cloud developer tool (with another IDE) in our blog, let me know in comments.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.