This topic describes how to install and configure the SDK for .NET.
This topic describes how to install and configure the SDK for .NET.
To use a specific Oracle Cloud Infrastructure service in your project, you can use the dotnet add package command from the root directory of your project workspace that contains the project file. The syntax for the add package command is:
Next, you need to set up the client to use the credentials. The credentials are
abstracted through an IAuthenticationDetailsProvider interface that
the client needs to implement.
These examples shows implementations of
ConfigFileAuthenticationDetailsProvider and
SimpleAuthenticationDetailsProvider.
If you use standard config file keys and the standard config file location,
you can use ConfigFileAuthenticationDetailsProvider:
Copy
using Oci.Common.Auth;
// uses DEFAULT profile in default file location: i.e ~/.oci/config
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
// uses custom configuration
var provider = new ConfigFileAuthenticationDetailsProvider("custom_file_location", "CUSTOM_PROFILE");
If you are using custom key names in the config file, you can use
SimpleAuthenticationDetailsProvider
To load a config with or without a profile:
Copy
using Oci.Common;
var config = ConfigFileReader.Parse(ConfigFileReader.DEFAULT_FILE_PATH);
var configWithProfile = ConfigFileReader.Parse(ConfigFileReader.DEFAULT_FILE_PATH, "DEFAULT");
Next, create an Auth provider using
SimpleAuthenticationDetailsProvider:
Copy
using Oci.Common;
using Oci.Common.Auth;
var config = ConfigFileReader.Parse(ConfigFileReader.DEFAULT_FILE_PATH);
// The private key supplier can be created with the file path,or using the config file
var provider = new SimpleAuthenticationDetailsProvider {
TenantId = config.GetValue("tenancy"),
UserId = config.GetValue("user"),
Fingerprint = config.GetValue("fingerprint"),
Region = Region.FromRegionId(config.GetValue("region")),
PrivateKeySupplier = new FilePrivateKeySupplier(config.GetValue("key_file"), config.GetValue("pass_phrase"))
};
Configuring Client-Side Options
Create a client-side configuration through the ClientConfiguration
class. If you do not provide your own configuration, the SDK for .NET uses
a default configuration.
The following example shows how to provide your own configuration:
Copy
var clientConfiguration = new ClientConfiguration
{
ClientUserAgent = "DotNet-SDK-Example",
RetryConfiguration = new RetryConfiguration
{
// maximum number of attempts to retry the same request
MaxAttempts = 5,
// retries the request if the response status code is in the range [400-499] or [500-599]
RetryableStatusCodeFamilies = new List<int> { 4, 5 }
}
};
Configuring Custom Options
In the configuration file, you can insert custom key-value pairs that you define, and
then reference them as necessary. For example, you could specify a frequently used
compartment ID in the config file:
using Oci.Common;
var config = ConfigFileReader.parse(ConfigFileReader.DEFAULT_FILE_PATH);
String compartmentId = config.GetValue("customCompartmentId");
Enabling Logging
The OCI SDK for .NET uses the NLog package for logging.
NLog is automatically installed with the .NET SDK, so no
additional installation is required.
To enable logging in your project:
Add an NLog.config file at the project's root directory.
You can find an example NLog.config file here
Add an ItemGroup section in the project file. For
example:
<ItemGroup>
<Content Include="PATH TO NLog.config File" >
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
To log from an application, create a Logger and use the
Info() method. For
example:
var logger = NLog.LogManager.GetCurrentClassLogger();
logger.Info("Hello World");
Note
Only SDK logging will be captured if you don't create a logger from an
application.