X

Break New Ground

OCI SDK for .NET is now available for your .NET Projects

Viral Modi
Principal Member of Technical Staff
This is a syndicated post, view the original post here

I’m excited to introduce the Oracle Cloud software development kit (SDK) for .NET. The Oracle Cloud SDK for .NET is a great addition to an existing set of Oracle Cloud developer tools. Oracle Cloud Infrastructure now supports SDKs in Java, Python, GO, Ruby, TypeScript, and .NET/C#. languages. It further boosts the partnership of Oracle Cloud and Microsoft Azure by providing .NET developers of Azure with a familiar platform for interacting with Oracle Cloud.

Prerequisites

Because I use multiple developer SDKs and command line interfaces (CLIs) for work, I use a config file for my setup instead of providing the config at runtime in the application code. Having a config file allows me to use a common configuration. My config file is located in default location at ~/.oci/config, as shown in the following code:

Using the SDK Within a .NET Application

You can use the .NET SDK in a .NET Console application, desktop application, or in any .NET class library. Most of the .NET-supported integrated development environments (IDEs) allow you to create a project of one of these types. In this blog, I create a Console application project.

Once the project is created, you can install Oracle Cloud .NET SDK packages using the NuGet package manager window provided by the IDE. You can search for Oracle Cloud .NET packages in your IDE NuGet window by using the string "OCI.DotNetSDK" or refer to the .NET SDK on NuGet package manager to see all the Oracle Cloud .NET packages.

You can learn how to install NuGet packagesfor some popular IDEs in the following resources:

If you aren’t using an IDE, you can install the .NET package for Oracle Cloud Infrastructure Identity service by going to the root directory of your project in a shell and using the following .NET CLI tool command:

dotnet add package OCI.DotNetSDK.Identity -v 1.0.0

Installation pulls all the dependencies attached to a package. In this example, we call the list compartments REST API of the Identity service using its .NET implementation, provided by the Oracle Cloud SDK for .NET.

I used Rider IDE for installation, as shown in the following screenshot:

A screenshot of the Rider IDE window.

Now that we’ve taken care of the installation and configuration required for using Oracle Cloud SDK for .NET, we can move onto writing code for our application. Most IDEs give an easy way to autopopulate after writing the source code. We’re using the following types in our application with "using" directive:

Next, initialize the compartment OCID:

The OCID value is truncated for security reasons.

Create an authentication provider that’s passed to service clients when constructed. As the name suggests, the authentication provider authenticates requests with Oracle Cloud Infrastructure services. In our example, we pass the provider when instantiating the Identity client. Since we have already created the config file as part of the pre-requisites, we use the ConfigFileAuthenticationDetailsProvider from the Oci.Common.Auth module. Here, we’re using the default profile from config file, but you can use any profile you need.

If passing a configuration at runtime, use the SimpleAuthenticationDetailsProvider.

Now, create an instance of the IdentityClient, passing our authProvider:

Construct a ListCompartmentRequest with any mandatory parameters required for the request. In this case, we pass the compartment OCID.

This example uses the pagination feature of .NET SDK, which allows a simpler way to get a large collection of items, such as a list of compartments. The following pagination mechanisms are supported by .NET SDK:

  • Making list calls and manually handling next page tokens to get all the records.

  • Using the pagination exposed by each service client to iterate through responses (Oracle Cloud SDK Response objects) to service calls.

  • Using the pagination exposed by each service client to iterate through the models and resources returned by service calls.

In this example, we use the pagination method to get the compartment records from our tenancy root compartment. The tenancy OCID is itself the root compartment OCID for all Oracle Cloud accounts.

Use the Identity client’s paginators and the RecordEnumerator list call within the class. The call returns an enumerator of type IEnumerable<Compartment> that you can use to iterate all the subcompartments in the compartment OCID passed in the request.

If we save, build, and run our console application now, we see the following input (compartment names are modified and OCIDs have been truncated for security purposes):

If you’re using an IDE, it gives you autocompletion for the methods used in the code and insight into the return type for each Oracle Cloud API call made, such as wherever we’re using "var". IDE decomplier also allows to you look into the code, and you can visit our GitHub and peek into the source code. The API reference of Oracle Cloud SDK for .NET is a complete reference for any .NET developer.

Example: Calling Asynchronous List Regions API

In this example, we call ListRegions to list all Oracle Cloud Infrastructure regions. This REST API doesn’t support pagination because the number of regions is a fixed short list. We use the “async and await” concept of C# while using this API. Most .NET SDK calls are asynchronous and can be tracked using the System.Threading.Tasks.Task, which forms the basis of the asynchronous programming model provided by .NET/C#.

The API gives the following output:

Summary

In this post, we looked at the recently released Oracle Cloud Infrastructure SDK for .NET. We learned how to create a request, create an authentication provider, use an Oracle Cloud Identity service client, and call the service APIs using the client. If you’re a .NET developer using Oracle Cloud Infrastructure, I urge you to give this .NET SDK a try. If you have any questions or feedback, leave a comment or refer to the documentation.

To get familiar with the .NET SDK, see the following resources:

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.