3.0 KiB
Initializing Your C# Client
The first step to using your generated client in code is to import and initialize your client. Our SDKs are modelled such that the client is the main point of access to the generated code.
Initializing and Authenticating Your Client
You can use your client if you import the namespace you specified when generating (under flag --namespace
). For the sake of this example,
let's say the namespace is Azure.Pets
and your client is PetsClient
. You could then access the client if you import the namespace.
using Azure.Pets;
using Azure.Core;
using Azure.Core.Pipeline;
const string endpoint = "http://localhost:3000";
var client = new PetsClient(endpoint);
When generating your client in ARM mode, we have defaults for clientDiagnostics
, pipeline
, and endpoint
. We also add a parameter tokenCredential
of type
TokenCredential
, where you pass in an OAuth token. We always recommend
using a credential type obtained from the Azure.Identity
for AAD authentication. For this example,
we use the most common DefaultAzureCredential
.
using Azure.Pets;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Identity;
const string endpoint = "http://localhost:3000";
var credential = new DefaultAzureCredential()
var client = new PetsClient(endpoint, credential);
You can also pass in client options through the options
parameter. This is used for exposing various
common client options, like policies for diagnostics, retry, and transport.
Finally, you can define client parameters in swagger, so these parameters would also be passed in at initialization time.