الاتصال إلى Media Services v3 API - .NET
هل تبحث عن وثائق خدمات الوسائط v2 ؟
هل تواجه مشكلة؟ راجع دليل استكشاف الأخطاء وإصلاحها للحصول على حلول للمشكلات المتعلقة باستخدام خدمات الوسائط.
يمكن العثور على نماذج التعليمات البرمجية في صفحة العينات .
توضح لك هذه المقالة كيفية الاتصال ب Azure Media Services v3 .NET SDK باستخدام أسلوب تسجيل الدخول الأساسي للخدمة.
المتطلبات الأساسية
- أنشئ حساباً لـMedia Services . تأكد من تذكر اسم مجموعة الموارد واسم حساب خدمات الوسائط
- تثبيت أداة ترغب في استخدامها لتطوير .NET. توضح الخطوات الواردة في هذه المقالة كيفية استخدام إصدار المجتمع Visual Studio 2019. يمكنك استخدام Visual Studio Code، راجع العمل باستخدام C#. أو يمكنك استخدام محرر تعليمات برمجية مختلفة.
هام
راجع اصطلاحات التسمية.
قم بإنشاء تطبيق وحدة تحكم
- بدء Visual Studio.
- من القائمة ملف، انقر فوق جديد>Project.
- إنشاء تطبيق وحدة تحكم .NET Core .
يستهدف netcoreapp2.0نموذج التطبيق في هذا الموضوع . تستخدم التعليمات البرمجية "async main"، والتي تتوفر بدءا من C# 7.1. راجع هذه المدونة لمزيد من التفاصيل.
إضافة حزم/تجميعات NuGet المطلوبة
- في Visual Studio، حدد ToolsNuGet> مدير الحِزَم >NuGet Manager Console.
- في نافذة وحدة التحكم مدير الحِزَم، استخدم
Install-Packageالأمر لإضافة حزم NuGet التالية. على سبيل المثال، Install-Package Microsoft.Azure.Management.Media.
| الحزمة | الوصف |
|---|---|
Microsoft.Azure.Management.Media |
Azure Media Services SDK. للتأكد من أنك تستخدم أحدث حزمة Azure Media Services، تحقق من Microsoft.Azure.Management.Media. |
التجميعات المطلوبة الأخرى
- Azure.Storage.Blobs
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.EnvironmentVariables
- Microsoft.Extensions.Configuration.Json
- Microsoft.Rest.ClientRuntime.Azure.Authentication
إنشاء ملف إعدادات التطبيق وتكوينه
إنشاء appsettings.json
- انتقل إلى ملف GeneralText>.
- قم بتسمية "appsettings.json".
- قم بتعيين الخاصية "Copy to Output Directory" لملف .json إلى "Copy if newer" (بحيث يتمكن التطبيق من الوصول إليه عند نشره).
تعيين القيم في appsettings.json
az ams account sp create قم بتشغيل الأمر كما هو موضح في واجهات برمجة تطبيقات الوصول. يقوم الأمر بإرجاع json الذي يجب نسخه إلى "appsettings.json".
إضافة ملف التكوين
للراحة، أضف ملف تكوين مسؤول عن قراءة القيم من "appsettings.json".
- أضف فئة .cs جديدة إلى مشروعك. وقم بتسميته
ConfigWrapper. - الصق التعليمات البرمجية التالية في هذا الملف (يفترض هذا المثال أن لديك مساحة الاسم هي
ConsoleApp1).
using System;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp1
{
public class ConfigWrapper
{
private readonly IConfiguration _config;
public ConfigWrapper(IConfiguration config)
{
_config = config;
}
public string SubscriptionId
{
get { return _config["SubscriptionId"]; }
}
public string ResourceGroup
{
get { return _config["ResourceGroup"]; }
}
public string AccountName
{
get { return _config["AccountName"]; }
}
public string AadTenantId
{
get { return _config["AadTenantId"]; }
}
public string AadClientId
{
get { return _config["AadClientId"]; }
}
public string AadSecret
{
get { return _config["AadSecret"]; }
}
public Uri ArmAadAudience
{
get { return new Uri(_config["ArmAadAudience"]); }
}
public Uri AadEndpoint
{
get { return new Uri(_config["AadEndpoint"]); }
}
public Uri ArmEndpoint
{
get { return new Uri(_config["ArmEndpoint"]); }
}
public string Location
{
get { return _config["Location"]; }
}
}
}
الاتصال إلى عميل .NET
لبدء استخدام واجهات برمجة تطبيقات خدمات الوسائط مع .NET، تحتاج إلى إنشاء كائن AzureMediaServicesClient . لإنشاء الكائن، تحتاج إلى توفير بيانات الاعتماد اللازمة للعميل للاتصال بـ Azure باستخدام Azure AD. في التعليمات البرمجية أدناه، تنشئ الدالة GetCredentialsAsync كائن ServiceClientCredentials استنادا إلى بيانات الاعتماد المتوفرة في ملف التكوين المحلي.
- افتح
Program.cs. - الصق التعليمات البرمجية التالية:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;
using Microsoft.Rest.Azure.Authentication;
namespace ConsoleApp1
{
class Program
{
public static async Task Main(string[] args)
{
ConfigWrapper config = new ConfigWrapper(new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build());
try
{
IAzureMediaServicesClient client = await CreateMediaServicesClientAsync(config);
Console.WriteLine("connected");
}
catch (Exception exception)
{
if (exception.Source.Contains("ActiveDirectory"))
{
Console.Error.WriteLine("TIP: Make sure that you have filled out the appsettings.json file before running this sample.");
}
Console.Error.WriteLine($"{exception.Message}");
if (exception.GetBaseException() is ErrorResponseException apiException)
{
Console.Error.WriteLine(
$"ERROR: API call failed with error code '{apiException.Body.Error.Code}' and message '{apiException.Body.Error.Message}'.");
}
}
Console.WriteLine("Press Enter to continue.");
Console.ReadLine();
}
private static async Task<ServiceClientCredentials> GetCredentialsAsync(ConfigWrapper config)
{
// Use ApplicationTokenProvider.LoginSilentWithCertificateAsync or UserTokenProvider.LoginSilentAsync to get a token using service principal with certificate
//// ClientAssertionCertificate
//// ApplicationTokenProvider.LoginSilentWithCertificateAsync
// Use ApplicationTokenProvider.LoginSilentAsync to get a token using a service principal with symmetric key
ClientCredential clientCredential = new ClientCredential(config.AadClientId, config.AadSecret);
return await ApplicationTokenProvider.LoginSilentAsync(config.AadTenantId, clientCredential, ActiveDirectoryServiceSettings.Azure);
}
private static async Task<IAzureMediaServicesClient> CreateMediaServicesClientAsync(ConfigWrapper config)
{
var credentials = await GetCredentialsAsync(config);
return new AzureMediaServicesClient(config.ArmEndpoint, credentials)
{
SubscriptionId = config.SubscriptionId,
};
}
}
}