使用 .NET 进行媒体服务开发Media Services development with .NET

本文介绍了如何开始使用 .NET 开发媒体服务应用程序。This article discusses how to start developing Media Services applications using .NET.

Azure 媒体服务 .NET SDK 库允许使用 .NET 为媒体服务编程。The Azure Media Services .NET SDK library enables you to program against Media Services using .NET. 为了进一步方便使用 .NET 进行开发,提供了 Azure 媒体服务 .NET SDK 扩展库。To make it even easier to develop with .NET, the Azure Media Services .NET SDK Extensions library is provided. 此库包含一组扩展方法和帮助器函数,可简化 .NET 代码。This library contains a set of extension methods and helper functions that simplify your .NET code. 这两个库都通过 NuGetGitHub 提供。Both libraries are available through NuGet and GitHub.

先决条件Prerequisites

  • 在新的或现有的 Azure 订阅中拥有一个媒体服务帐户。A Media Services account in a new or existing Azure subscription. 请参阅如何创建媒体服务帐户一文。See the article How to Create a Media Services Account.
  • 操作系统:Windows 10、Windows 7、Windows 2008 R2 或 Windows 8。Operating Systems: Windows 10, Windows 7, Windows 2008 R2, or Windows 8.
  • .NET Framework 4.5 或更高版本。.NET Framework 4.5 or later.
  • Visual Studio。Visual Studio.

创建和配置 Visual Studio 项目Create and configure a Visual Studio project

本部分演示如何在 Visual Studio 中创建项目,以及如何为媒体服务开发设置该项目。This section shows you how to create a project in Visual Studio and set it up for Media Services development. 在本示例中,该项目为 C# Windows 控制台应用程序,但此处所示的设置步骤同样适用于针对媒体服务应用程序(例如,Windows 窗体应用程序或 ASP.NET Web 应用程序)创建的其他类型的项目。In this case, the project is a C# Windows console application, but the same setup steps shown here apply to other types of projects you can create for Media Services applications (for example, a Windows Forms application or an ASP.NET Web application).

本部分说明如何使用 NuGet 添加媒体服务 .NET SDK 扩展和其他依赖库。This section shows how to use NuGet to add Media Services .NET SDK extensions and other dependent libraries.

此外,可以从 GitHub(github.com/Azure/azure-sdk-for-media-servicesgithub.com/Azure/azure-sdk-for-media-services-extensions)获取最新的媒体服务 .NET SDK 资料、生成解决方案并添加对客户端项目的引用。Alternatively, you can get the latest Media Services .NET SDK bits from GitHub (github.com/Azure/azure-sdk-for-media-services or github.com/Azure/azure-sdk-for-media-services-extensions), build the solution, and add the references to the client project. 自动下载并提取所有必需的依赖项。All the necessary dependencies get downloaded and extracted automatically.

  1. 在 Visual Studio 中创建新的 C# 控制台应用程序。Create a new C# Console Application in Visual Studio. 输入“名称” 、“位置” 和“解决方案名称” ,并单击“确定”。Enter the Name, Location, and Solution name, and then click OK.

  2. 生成解决方案。Build the solution.

  3. 使用 NuGet 安装和添加 Azure 媒体服务 .NET SDK 扩展 (windowsazure.mediaservices.extensions)。Use NuGet to install and add Azure Media Services .NET SDK Extensions (windowsazure.mediaservices.extensions). 安装此包也会安装 媒体服务 .NET SDK 并添加所有其他必需的依赖项。Installing this package, also installs Media Services .NET SDK and adds all other required dependencies.

    确保已安装最新版本的 NuGet。Ensure that you have the newest version of NuGet installed. 有关详细信息和安装说明,请参阅 NuGetFor more information and installation instructions, see NuGet.

    1. 在“解决方案资源管理器”中,右键单击项目名称,并选择“管理 NuGet 包” 。In Solution Explorer, right-click the name of the project and choose Manage NuGet Packages.

    2. 此时显示“管理 NuGet 包”对话框。The Manage NuGet Packages dialog box appears.

    3. 在联机库中,搜索 Azure 媒体服务扩展,选择“Azure 媒体服务.NET SDK 扩展” (windowsazure.mediaservices.extensions ),然后单击“安装” 按钮。In the Online gallery, search for Azure MediaServices Extensions, choose Azure Media Services .NET SDK Extensions (windowsazure.mediaservices.extensions), and then click the Install button.

    4. 此时会修改项目并添加对媒体服务 .NET SDK 扩展、媒体服务 .NET SDK 和其他依赖程序集的引用。The project is modified and references to the Media Services .NET SDK Extensions, Media Services .NET SDK, and other dependent assemblies are added.

  4. 若要升级更干净的开发环境,请考虑启用 NuGet 包还原。To promote a cleaner development environment, consider enabling NuGet Package Restore. 有关详细信息,请参阅 NuGet 包还原For more information, see NuGet Package Restore".

  5. 添加对 System.Configuration 程序集的引用。Add a reference to System.Configuration assembly. 此程序集包含用于访问配置文件(例如,App.config)的 System.Configuration.ConfigurationManager 类。This assembly contains the System.Configuration.ConfigurationManager class that is used to access configuration files (for example, App.config).

    1. 若要使用“管理引用”对话框添加引用,请在“解决方案资源管理器”中右键单击项目名称。To add references using the Manage References dialog, right-click the project name in the Solution Explorer. 然后单击“添加” ,并单击“引用...” 。Then, click Add, then click Reference....

    2. 此时显示“管理引用”对话框。The Manage References dialog appears.

    3. 在 .NET Framework 程序集下,找到并选择 System.Configuration 程序集,并按“确定” 。Under .NET framework assemblies, find and select the System.Configuration assembly and press OK.

  6. 打开 App.config 文件并将 appSettings 节添加到文件。Open the App.config file and add an appSettings section to the file. 设置连接到媒体服务 API 所需的值。Set the values that are needed to connect to the Media Services API. 有关详细信息,请参阅通过 Azure AD 身份验证访问 Azure 媒体服务 APIFor more information, see Access the Azure Media Services API with Azure AD authentication.

    设置使用服务主体身份验证方法进行连接所需的值。Set the values that are needed to connect using the Service principal authentication method.

     ```csharp
             <configuration>
             ...
                 <appSettings>
                     <add key="AMSAADTenantDomain" value="tenant"/>
                     <add key="AMSRESTAPIEndpoint" value="endpoint"/>
                     <add key="AMSClientId" value="id"/>
                     <add key="AMSClientSecret" value="secret"/>
                 </appSettings>
             </configuration>
     ```
    
  7. 向你的项目中添加 System.Configuration 引用。Add the System.Configuration reference to your project.

  8. 使用以下代码覆盖位于 Program.cs 文件开头的现有 using 语句:Overwrite the existing using statements at the beginning of the Program.cs file with the following code:

            using System;
            using System.Configuration;
            using System.IO;
            using Microsoft.WindowsAzure.MediaServices.Client;
            using System.Threading;
            using System.Collections.Generic;
            using System.Linq;
    

    现在,可以开始开发媒体服务应用程序了。At this point, you are ready to start developing a Media Services application.

示例Example

下面是一个小型示例,该示例连接到 AMS API 并列出所有可用的媒体处理器。Here is a small example that connects to the AMS API and lists all available Media Processors.

        class Program
        {
            // Read values from the App.config file.

            private static readonly string _AADTenantDomain =
                ConfigurationManager.AppSettings["AMSAADTenantDomain"];
            private static readonly string _RESTAPIEndpoint =
                ConfigurationManager.AppSettings["AMSRESTAPIEndpoint"];
            private static readonly string _AMSClientId =
                ConfigurationManager.AppSettings["AMSClientId"];
            private static readonly string _AMSClientSecret =
                ConfigurationManager.AppSettings["AMSClientSecret"];
        
            private static CloudMediaContext _context = null;
            static void Main(string[] args)
            {
                AzureAdTokenCredentials tokenCredentials = 
                    new AzureAdTokenCredentials(_AADTenantDomain,
                        new AzureAdClientSymmetricKey(_AMSClientId, _AMSClientSecret),
                        AzureEnvironments.AzureChinaCloudEnvironment);

                var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

                _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);
        
                // List all available Media Processors
                foreach (var mp in _context.MediaProcessors)
                {
                    Console.WriteLine(mp.Name);
                }
        
            }

后续步骤Next steps

现在可以连接到 AMS API 并开始开发Now you can connect to the AMS API and start developing.