Connect to Media Services Using the Media Services SDK
This topic describes how to obtain a programmatic connection to Windows Azure Media Services when you are programming with the Media Services SDK for .NET.
Connecting to Media Services
To connect to Media Services programmatically, you must have previously set up an Azure account, configured Media Services on that account, and then set up a Visual Studio project for development with the Media Services SDK for .NET. For more information, see Setup for Development on the Media Services SDK for .NET.
At the end of the Media Services account setup process, you obtained the following required connection values. Use these to make programmatic connections to Media Services.
-
Your Media Services account name.
-
Your Media Services account key.
Note |
|---|
| To find these values, go to the Windows Azure Portal, select your Media Service account, and click on the “MANAGE KEYS” icon on the bottom of the portal window. Clicking on the icon next to each text box copies the value to the system clipboard. |
Storing Connection Values in Configuration
To create a console or desktop client application, it is suggested that you add the required connection values to a configuration file in an <appSettings> section. This is not required but is a secure way to store connection data.
Caution |
|---|
| It is a highly recommended practice to store connection values, especially sensitive values such as your account name and password, in configuration. Also, it is a recommended practice to encrypt sensitive configuration data. You can encrypt the entire configuration file by using the Windows Encrypting File System (EFS). To enable EFS on a file, right-click the file, select Properties, and enable encryption in the Advanced settings tab. Or you can create a custom solution for encrypting selected portions of a configuration file by using protected configuration. See Encrypting Configuration Information Using Protected Configuration. |
The following App.config file contains the required connection values. The values in the <appSettings> element are the required values that you got from the Media Services account setup process.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<appSettings>
<add key="accountName" value="Add-Media-Services-Account-Name" />
<add key="accountKey" value="Add-Media-Services-Account-Key" />
</appSettings>
</configuration>
To retrieve connection values from configuration, you can use the ConfigurationManager class and then assign the values to fields in your code:
private static readonly string _accountName = ConfigurationManager.AppSettings["accountName"]; private static readonly string _accountKey = ConfigurationManager.AppSettings["accountKey"];
Creating a Server Context Instance
To start programming against Media Services you need to create a CloudMediaContext instance that represents the server context. The CloudMediaContext includes references to important collections including jobs, assets, files, access policies, and locators.
To create an instance of the server context in code, declare a new CloudMediaContext object, and pass to it the required connection values. The following code example shows a helper method that gets a new instance of the CloudMediaContext passing to it the required connection values that were read from the application configuration file as shown in the previous example.
Note |
|---|
| The CloudMediaContext class is not thread safe. |
static CloudMediaContext GetContext()
{
return new CloudMediaContext(_accountName, _accountKey);
}
You are ready to proceed to the next topic: Ingest Assets with the Media Services SDK for .NET.
See Also
Concepts
Building Applications with the Media Services SDK for .NETSetup for Development on the Media Services SDK for .NET
Ingest Assets with the Media Services SDK for .NET
Process Assets with the Media Services SDK for .NET
Manage Assets with the Media Services SDK for .NET
Deliver Assets with the Media Services SDK for .NET
Media Services Considerations
Build Date:
Note
Caution