Sự kiện
21 giờ 17 thg 3 - 10 giờ 21 thg 3
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.
Đăng ký ngayTrình duyệt này không còn được hỗ trợ nữa.
Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.
The Azure SDK client libraries provide an interface to Azure services by translating method calls into messages sent via the respective service protocol. For REST API services, this means sending HTTP requests and converting the responses into runtime types. In this article, you'll learn about the different types of methods exposed by the client libraries and explore their implementation patterns.
An Azure SDK for .NET client library can expose two different categories of methods to make requests to an Azure service:
Protocol and convenience methods implement slightly different patterns based on the underlying package dependency chain of the respective library. An Azure SDK for .NET client library depends on one of two different foundational libraries:
Azure.Storage.Blobs
.System.ClientModel
library is a general purpose toolset designed to help build libraries for various platforms and services, whereas the Azure.Core
library is specifically designed for building Azure client libraries.Lưu ý
The Azure.Core
library itself also depends on System.ClientModel
for various client building blocks. In the context of this article, the key differentiator for method patterns is whether a client library depends on Azure.Core
or System.ClientModel
directly, rather than through a transitive dependency.
The following table compares some of the request and response types used by protocol and convenience methods, based on whether the library depends on Azure.Core
or System.ClientModel
.
Request or response concern | Azure.Core | System.ClientModel |
---|---|---|
Request body | RequestContent | BinaryContent |
Advanced request options | RequestContext | RequestOptions |
Raw HTTP response | Response | PipelineResponse |
Return type with output model | Response<T> | ClientResult<T> |
The sections ahead provide implementation examples of these concepts.
The coding patterns and types used by client library protocol and convenience methods vary slightly based on whether the library depends on Azure.Core
or System.ClientModel
. The differences primarily influence the .NET types used for handling request and response data.
Azure SDK client libraries adhering to the latest design guidelines depend on the Azure.Core
library. For example, the Azure.AI.ContentSafety
library depends on the Azure.Core
library and provides a ContentSafetyClient
class that exposes both protocol and convenience methods.
The following code uses a ContentSafetyClient
to call the AnalyzeText
convenience method:
using Azure.AI.ContentSafety;
using Azure.Identity;
// Create the client
ContentSafetyClient client = new(
new Uri("https://contentsafetyai.cognitiveservices.azure.com/"),
new DefaultAzureCredential());
// Call the convenience method
AnalyzeTextResult result = client.AnalyzeText("What is Microsoft Azure?");
// Display the results
foreach (TextCategoriesAnalysis item in result.CategoriesAnalysis)
{
Console.WriteLine($"{item.Category}: {item.Severity}");
}
The preceding code demonstrates the following Azure.Core
convenience method patterns:
Some client libraries that connect to non-Azure services use patterns similar to the libraries that depend on Azure.Core
. For example, the OpenAI
library provides a client that connects to the OpenAI services. These libraries are based on a library called System.ClientModel
that has patterns similar to Azure.Core
.
Consider the following code that uses a ChatClient
to call the CompleteChat
convenience method:
using OpenAI.Chat;
// Create the client
ChatClient client = new(
model: "gpt-4o-mini",
credential: Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);
// Call the convenience method
ChatCompletion completion = client.CompleteChat("What is Microsoft Azure?");
// Display the results
Console.WriteLine($"[{completion.Role}]: {completion}");
The preceding code demonstrates the following System.ClientModel
convenience method patterns:
ClientResult
type that represents the result of the operation.When a service call fails, the service client throws an exception that exposes the HTTP status code and the details of the service response, if available. A System.ClientModel
-dependent library throws a ClientResultException, while an Azure.Core
-dependent library throws a RequestFailedException.
using Azure.AI.ContentSafety;
using Azure.Identity;
using Azure;
// Create the client
ContentSafetyClient client = new(
new Uri("https://contentsafetyai.cognitiveservices.azure.com/"),
new DefaultAzureCredential());
try
{
// Call the convenience method
AnalyzeTextResult result = client.AnalyzeText("What is Microsoft Azure?");
// Display the results
foreach (TextCategoriesAnalysis item in result.CategoriesAnalysis)
{
Console.WriteLine($"{item.Category}: {item.Severity}");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Although the Azure SDK for .NET client libraries provide the option to use either protocol or convenience methods, prioritize using convenience methods in most scenarios. Convenience methods are designed to improve the development experience and provide flexibility for authoring requests and handling responses. However, both method types can be used in your app as needed. Consider the following criteria when deciding which type of method to use.
Convenience methods:
Protocol methods:
RequestContext
and RequestOptions
, which aren't available through convenience methods.Ý kiến phản hồi về .NET
.NET là một dự án nguồn mở. Chọn liên kết để cung cấp ý kiến phản hồi:
Sự kiện
21 giờ 17 thg 3 - 10 giờ 21 thg 3
Tham gia chuỗi buổi gặp gỡ để xây dựng các giải pháp AI có thể mở rộng dựa trên các trường hợp sử dụng trong thế giới thực với các nhà phát triển và chuyên gia đồng nghiệp.
Đăng ký ngayĐào tạo
Mô-đun
Zugreifen auf Daten in Azure Blob Storage mit verschiedenen Protokollen - Training
Sie können auf Ihre Daten in Azure Blob Storage mit verschiedenen Protokollen zugreifen, z. B. Representational State Transfer (REST), Hadoop Distributed File System (HDFS), Network File System (NFS) und Secure File Transfer Protocol (SFTP).
Tài liệu