.net azure sdk Getting Started Guide

Author

Reads 427

Computer server in data center room
Credit: pexels.com, Computer server in data center room

The .NET Azure SDK is a powerful tool that allows you to easily build and deploy cloud-based applications on the Azure platform.

First, you'll need to install the .NET Azure SDK, which can be done through the .NET CLI using the command `dotnet add package Azure.Storage.Blobs`. This will give you access to the Azure Blob Storage client library, which is just one of the many libraries available in the SDK.

Before you start building your application, make sure you have an Azure account and a subscription to the Azure services you plan to use. This will give you the necessary credentials to authenticate and authorize your application.

Next, you'll want to configure your Azure account to use the .NET Azure SDK. This involves setting up a connection string and other configuration settings that will allow your application to interact with Azure services.

Curious to learn more? Check out: .net Azure Sdk

Client Library

A service client should have the same shape as the code snippet, with the same name as the service it's for, followed by the Client suffix. For example, the service client for the Application Configuration service is called ConfigurationClient.

Here's an interesting read: Azure Container Client

Credit: youtube.com, Daniel Krzyczkowski - New NET Azure SDK In Action

Service clients should be classes, not structs, and should be immutable to prevent thread interference. They should also be placed in the root namespace of their corresponding component.

You can authenticate a client using DefaultAzureCredential, which is the default option for creating an authenticated client. This allows you to interact with resources without having to test service API versions.

If this caught your attention, see: Azure Kubernetes Service vs Azure Container Apps

Azure API Design

Azure services are exposed to .NET developers as one or more service client types. These client types are the primary interface for interacting with Azure services.

Service client types are accompanied by a set of supporting types, which include subclients that give structure to the API by organizing groups of related service operations.

Subclients are used to group related service operations, making it easier for developers to navigate and use the API.

Model types represent resources on the service, providing a clear and concise way to work with service data.

Recommended read: Azure Dev Ops Api

Client: New Releases

Credit: youtube.com, Jon Skeet - Client Library Design Stories | Øredev 2018

New releases of client libraries are announced as GA, and some are releasing in preview. These libraries follow the Azure SDK Design Guidelines for .NET.

The new client libraries share a number of core features such as HTTP retries, logging, transport protocols, and authentication protocols. You can learn about these shared features at Azure.Core.

Each new client library can be identified by its naming, which starts with Azure, followed by the service category, and then the name of the service. For example, Azure.Storage.Blobs.

If you need to ensure your code is ready for production, we strongly recommend using one of the stable, non-preview libraries.

Check this out: Azure Devops Features

Client: Previous Versions

Previous versions of client libraries are still available for use in production-ready applications. They provide similar functionalities to the preview packages and allow you to use and consume existing resources.

These libraries can be identified by their namespace, which typically contains 'Microsoft.Azure' in their names, for example 'Microsoft.Azure.KeyVault'. They might not implement the guidelines or have the same feature set as the new releases.

Credit: youtube.com, Client Libraries

Previous versions of client libraries can be used to interact with existing resources, such as uploading a storage blob. They offer wider coverage of services compared to the new releases.

If you need to ensure your code is ready for production, we strongly recommend using one of the stable, non-preview libraries.

Service Client

Creating a Service Client with the Azure SDK for .NET is a straightforward process. The main starting point for developers calling Azure services with the Azure SDK is the service client.

A service client should have the same shape as the code snippet provided, with a name that includes the "Client" suffix, like ConfigurationClient for the Application Configuration Service.

Place at least one service client in the root namespace of its corresponding component. This makes it easy to discover and use the client.

Service clients should be classes, not structs, and should be immutable. This ensures that client instances can be safely shared between threads.

Credit: youtube.com, Introducing the New and Improved Azure SDK for .NET

To choose the namespace for the client types, follow the guidelines in the Namespace Naming guidelines.

The Azure SDK for .NET provides a series of NuGet packages that can be used in both .NET Core and .NET Framework applications to access most Azure services.

The Azure SDK for .NET makes it easy to incorporate Azure services into your application by instantiating a client object and calling the appropriate methods.

To authenticate with Azure and create an ArmClient, instantiate an ArmClient given credentials. This is the default option to create an authenticated client.

Service Client Operations

To build effective service client operations in your .NET Azure SDK, it's essential to follow best practices for service method parameters. Simple methods should have up to six parameters, mostly simple BCL primitives, and follow standard .NET Framework Design Guidelines. Complex methods, on the other hand, should use the options parameter pattern to represent the request payload.

When designing service methods, consider adding convenience simple overloads for common scenarios and use the options parameter pattern to make your code more maintainable. You can also use the Options class to model required service method parameters as constructor parameters and get-only properties, and optional parameters as get-set properties.

Credit: youtube.com, Azure Service Bus and the .NET Standard SDK

Here are some key takeaways for service client operations:

  • DO use the options parameter pattern for complex service methods.
  • YOU MAY use the options parameter pattern for simple service methods that you expect to grow in the future.
  • DO validate client parameters, such as null checks, empty string checks, and range checks.
  • DO make all properties virtual to allow mocking.
  • DO use Azure.Core ETag to represent ETags.

Service Methods

Service methods are a crucial part of any service client operation, and understanding how to design them effectively is key to building robust and efficient services.

DO use the options parameter pattern for complex service methods, as it allows for a clear and concise way to represent complex request payloads. This pattern is designed similarly to .NET custom attributes, where required service method parameters are modeled as Options class constructor parameters and get-only properties, and optional parameters are get-set properties.

Simple service methods should follow standard .NET Framework Design Guidelines for parameter list and overload design. Service methods take two kinds of parameters: service parameters and client parameters, with service parameters being sent across the wire to the service as URL segments, query parameters, request header values, and request bodies.

DO validate client parameters, which include null checks, empty string checks, and range checks. Let the service validate its parameters, and make sure to test the developer experience when invalid service parameters are passed in. Ensure clear error messages are generated by the client.

Computer server in data center room
Credit: pexels.com, Computer server in data center room

The options parameter type can be named with the 'Options' suffix, and you may use the options parameter pattern for simple service methods that you expect to grow in the future. Consider adding simple overloads of methods using the options parameter pattern, especially if in common scenarios, users are likely to pass just a small subset of what the options parameter represents.

Here's a summary of service method best practices:

DO make all properties virtual to allow mocking, and consider using Azure.Core ETag to represent ETags.

Service Client Support

Service clients are the main starting points for developers calling Azure services with the Azure SDK. Each client library should have at least one client in its main namespace, so it’s easy to discover.

The guidelines for designing a service client are straightforward. A service client should have the same shape as the code snippet provided, with the client type name ending in "Client".

Credit: youtube.com, Exploring the new Azure .NET SDKs for .NET

For example, the service client for the Application Configuration service is called ConfigurationClient. This is a good practice to follow.

Service clients should be classes, not structs, and should be immutable. This ensures that client instances are thread-safe and can be shared between threads without affecting each other.

Client instances are often stored in application statics, so making them immutable is crucial to prevent unintended behavior.

The Azure SDK for .NET provides a series of NuGet packages that can be used in both .NET Core and .NET Framework applications. This makes it easy to incorporate Azure services into your application.

To authenticate with Azure and create an ArmClient, you can instantiate an ArmClient given credentials. This is the default option for creating an authenticated client.

Common Libraries and Guidelines

When designing common libraries for .NET Azure SDK, it's essential to engage the Architecture Board to discuss how to design such common library. This will ensure that the shared code is well-structured and meets the needs of all client libraries.

Credit: youtube.com, Introducing the new Azure SDKs

You should design common libraries to share a set of exceptions or models, as this can greatly reduce code duplication and improve maintainability. This is especially useful when multiple client libraries need to cooperate and share common functionality.

Engaging the Architecture Board will help you make informed decisions about how to structure and share common code, ultimately leading to more efficient and scalable .NET Azure SDK solutions.

For another approach, see: Code Interpreter Azure

Common Libraries

When designing common libraries, it's essential to engage the Architecture Board to discuss how to design such common libraries. This ensures everyone is on the same page and helps avoid potential issues down the line.

Common libraries can contain shared code between several client libraries, such as a set of cooperating client libraries that may wish to share a set of exceptions or models. This can help reduce code duplication and make maintenance easier.

If you're creating a common library, consider the following:

  • DO engage the Architecture Board to discuss how to design such common library.
  • This is especially important when sharing code between multiple client libraries.

By engaging the Architecture Board, you can ensure your common library is well-designed and meets the needs of all parties involved. This can help prevent issues and make maintenance easier in the long run.

Package Version Numbers

Credit: youtube.com, Effective Strategies for Managing Common/Utility Libraries in Software Development

Use the MAJOR.MINOR.PATCH format for the version of your library, and include a -beta._N suffix for beta package versions, like 1.0.0-beta.2.

Consistent version number schemes allow consumers to determine what to expect from a new version of the library. This is crucial for maintaining trust and ensuring that users can rely on your library.

DO change the version number of the client library whenever ANYTHING changes in the client library. This includes both big and small changes, so be sure to update the version number accordingly.

Increment the patch version when fixing a bug, but DO NOT include new APIs in a patch release. This helps ensure that users know exactly what changes have been made and can plan accordingly.

DO increment the major or minor version when adding support for a service API version, or when adding a new method to the public API. This helps users understand the scope of the changes and plan accordingly.

You might like: Azure Library

Credit: youtube.com, How to Read a NuGet Package Library Version in the Target Assembly

Increment the major version when making large feature changes, as this indicates a significant shift in the library's functionality. This helps users understand the impact of the changes and plan accordingly.

Select a version number greater than the highest version number of any other released Track 1 package for the service in any other scope or language.

Target Frameworks

All Azure SDK libraries must include a target for .NET Standard 2.0, ensuring they're compatible with all supported versions of .NET.

This means that libraries built by the Azure SDK engineering system will also target the oldest supported long term support (LTS) version of .NET, allowing them to take advantage of modern runtime features.

You should strongly encourage self-published libraries to include this target framework as well.

To achieve this, use the following target setting in the .csproj file for projects built by the Azure SDK engineering system: DO build all libraries for .NET Standard 2.0.

Take a look at this: Azure 2.0

Credit: youtube.com, Choosing the Right NuGet Package Target Framework for Your Project: An In-Depth Guide

If you're not building with the Azure SDK engineering system, use the following target setting in the .csproj file: YOU SHOULD build libraries for the oldest supported LTS version of .NET.

For example, if the oldest supported LTS version of .NET is 8.0, use the target setting: DO define the same APIs for all target framework monikers (TFMs).

The public API of client libraries must be the same for all targets, including class, interface, parameter, and return types.

Engage the Architecture Board if your client library has special needs for targeting additional frameworks or is unable to target the required set.

Repository Guidelines

When working with repositories, it's essential to follow guidelines to ensure smooth collaboration and maintainability. DO locate all source code and README in the azure/azure-sdk-for-net GitHub repository.

Repository guidelines are in place to help developers work efficiently and effectively. DO follow Azure SDK engineering systems guidelines for working in the azure/azure-sdk-for-net GitHub repository.

Additional reading: Azure Devops Code Repository

Prerequisites

Credit: youtube.com, What are Prerequisites

To get started with common libraries and guidelines, you'll need to meet some basic prerequisites.

First and foremost, you'll need an Azure subscription - this will give you access to the various tools and services you'll be using.

Having a TokenCredential implementation, such as an Azure Identity library credential type, will also be essential for authenticating with Azure services.

This credential type will allow you to securely connect to Azure resources and perform actions on your behalf.

Accessing Azure Services

Accessing Azure Services is as simple as installing the correct NuGet package. The Azure SDK for .NET is provided as a series of NuGet packages that can be used in both .NET Core (2.1 and higher) and .NET Framework (4.6.1 and higher) applications.

You can use the Azure SDK for .NET to access most Azure services, whether your app is hosted in Azure or on-premises. This is made possible by the Azure SDK for .NET, which provides a consistent and familiar interface to access Azure services.

To access Azure services, you can instantiate a client object and call the appropriate methods. This is as easy as installing the correct NuGet package and following the guidelines for the Azure SDK for .NET.

Authentication

Credit: youtube.com, AZ-900 Episode 25 | Azure Identity Services | Authentication, Authorization & Active Directory (AD)

Authentication is key to accessing Azure services. You should use credential types provided in the Azure.Core package.

Azure.Core provides TokenCredential for OAuth style tokens, including MSI credentials. This is a good option for authentication.

You should support changing credentials without having to create a new client instance. This makes it easier to manage access to your Azure services.

Credentials passed to the constructors must be read before every request. For example, by calling TokenCredential.GetToken(). This ensures that your credentials are up to date.

It's a good idea to contact adparch if you want to add a new credential type. They can provide guidance on the process.

To authenticate with Azure and create an ArmClient, you can instantiate an ArmClient given credentials. This is the default option for creating an authenticated client.

The Azure.Identity.DefaultAzureCredential class is a good place to start for more information about default Azure credentials.

Here's an interesting read: Azure Functions Core Tools

Access Azure Services

The Azure SDK for .NET makes it easy to use Azure services from your .NET applications.

Credit: youtube.com, AZ-900 Episode 27 | Azure Key Vault | Secret, Key and Certificate Management

Whether your app is hosted in Azure or on-premises, access to most Azure services is provided through the Azure SDK for .NET.

The Azure SDK for .NET is a collection of NuGet packages that can be used in applications targeting .NET variants that implement .NET Standard 2.0.

The Azure SDK for .NET provides a consistent and familiar interface to access Azure services, making it easy to upload and download files to Blob Storage, retrieve application secrets from Azure Key Vault, or process notifications from Azure Event Hubs.

The Azure SDK for .NET is provided as a series of NuGet packages and can be used in both .NET Core (2.1 and higher) and .NET Framework (4.6.1 and higher) applications.

Incorporating Azure services into your application is as easy as installing the correct NuGet package, instantiating a client object, and calling the appropriate methods.

Resource Management

Resource management is a crucial aspect of working with the .NET Azure SDK. To get started, you can use the management SDK cheat sheet to create, list, update, or delete a typical Azure service bus namespace.

Credit: youtube.com, ARM Templates Tutorial | Infrastructure as Code (IaC) for Beginners | Azure Resource Manager

You'll need to authenticate to the subscription and resource group you want to work on. To do this, you can use the client.GetServiceBusQueueResource() method to get a resource with a resource identifier.

Here's a list of common operations you can perform on an Azure resource:

To manage existing resources, you can use the resource identifier and extension methods provided by the client. This approach is more efficient and requires less code than making multiple API calls to Azure.

Resource Management Operations

Resource Management Operations are a crucial part of working with the .NET Azure SDK. You can authenticate to a subscription and resource group with a single call, making it easier to manage Azure resources.

To get started with resource management, you'll need to understand the object hierarchy in the SDK, which mimics the object hierarchy in Azure. This hierarchy includes three standard types for all resources: Resource, ResourceCollection, and ResourceIdentifier.

Credit: youtube.com, Intro to Python SDK on Azure CRUD operations using Resource Groups

You can access a resource with its resource identifier using the GetServiceBusQueueResource method, or by using extension methods that retrieve a scoped resource client. These methods return a Resource object that can be used to perform operations on the resource.

Here are some key operations you can perform on a resource:

The ResourceCollection class provides most of the logical collection operations, such as iterate/list, index, add, contains, and delete. You can use the Exists() method to check if a resource exists in a collection.

Performing operations on existing resources can be done efficiently using the new object hierarchy and extension methods. This approach reduces the number of API calls and code required to manage resources.

Examples

The .NET Azure SDK offers a range of code samples to help you get started with managing your Azure resources.

You can find sample code for managing Resource Groups, which allows you to organize and manage your Azure resources in a logical and structured way.

Here are some specific code samples you can explore:

  • Managing Resource Groups
  • Creating a Virtual Network
  • .NET Management Library Code Samples

These code samples provide a great starting point for learning how to use the .NET Azure SDK to manage your Azure resources.

Lamar Smitham

Writer

Lamar Smitham is a seasoned writer with a passion for crafting informative and engaging content. With a keen eye for detail and a knack for simplifying complex topics, Lamar has established himself as a trusted voice in the industry. Lamar's areas of expertise include Microsoft Licensing, where he has written in-depth articles that provide valuable insights for businesses and individuals alike.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.