/// <summary>
        /// EntitleResource is used only for 3rd party Store providers. Each
        /// subscription must be entitled for the resource before creating
        /// that particular type of resource.
        /// </summary>
        /// <param name='parameters'>
        /// Required. Parameters provided to the EntitleResource method.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// A standard service response including an HTTP status code and
        /// request ID.
        /// </returns>
        public async System.Threading.Tasks.Task <OperationResponse> EntitleResourceAsync(EntitleResourceParameters parameters, CancellationToken cancellationToken)
        {
            // Validate
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            if (parameters.ResourceNamespace == null)
            {
                throw new ArgumentNullException("parameters.ResourceNamespace");
            }
            if (parameters.ResourceType == null)
            {
                throw new ArgumentNullException("parameters.ResourceType");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("parameters", parameters);
                Tracing.Enter(invocationId, this, "EntitleResourceAsync", tracingParameters);
            }

            // Construct URL
            string url     = (this.Credentials.SubscriptionId != null ? this.Credentials.SubscriptionId.Trim() : "") + "/EntitleResource";
            string baseUrl = this.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Post;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("x-ms-version", "2013-03-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Serialize Request
                string    requestContent = null;
                XDocument requestDoc     = new XDocument();

                XElement entitleResourceElement = new XElement(XName.Get("EntitleResource", "http://schemas.microsoft.com/windowsazure"));
                requestDoc.Add(entitleResourceElement);

                XElement resourceProviderNameSpaceElement = new XElement(XName.Get("ResourceProviderNameSpace", "http://schemas.microsoft.com/windowsazure"));
                resourceProviderNameSpaceElement.Value = parameters.ResourceNamespace;
                entitleResourceElement.Add(resourceProviderNameSpaceElement);

                XElement typeElement = new XElement(XName.Get("Type", "http://schemas.microsoft.com/windowsazure"));
                typeElement.Value = parameters.ResourceType;
                entitleResourceElement.Add(typeElement);

                XElement registrationDateElement = new XElement(XName.Get("RegistrationDate", "http://schemas.microsoft.com/windowsazure"));
                registrationDateElement.Value = parameters.RegistrationDate.ToString();
                entitleResourceElement.Add(registrationDateElement);

                requestContent      = requestDoc.ToString();
                httpRequest.Content = new StringContent(requestContent, Encoding.UTF8);
                httpRequest.Content.Headers.ContentType         = new MediaTypeHeaderValue("application/xml");
                httpRequest.Content.Headers.ContentType.CharSet = "utf-8";

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.Accepted)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    OperationResponse result = null;
                    result            = new OperationResponse();
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// EntitleResource is used only for 3rd party Store providers. Each
 /// subscription must be entitled for the resource before creating
 /// that particular type of resource.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.ICloudServiceManagementClient.
 /// </param>
 /// <param name='parameters'>
 /// Parameters provided to the EntitleResource method.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse EntitleResource(this ICloudServiceManagementClient operations, EntitleResourceParameters parameters)
 {
     try
     {
         return(operations.EntitleResourceAsync(parameters).Result);
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// EntitleResource is used only for 3rd party Store providers. Each
 /// subscription must be entitled for the resource before creating
 /// that particular type of resource.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.ICloudServiceManagementClient.
 /// </param>
 /// <param name='parameters'>
 /// Parameters provided to the EntitleResource method.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static Task <OperationResponse> EntitleResourceAsync(this ICloudServiceManagementClient operations, EntitleResourceParameters parameters)
 {
     return(operations.EntitleResourceAsync(parameters, CancellationToken.None));
 }
 /// <summary>
 /// EntitleResource is used only for 3rd party Store providers. Each
 /// subscription must be entitled for the resource before creating
 /// that particular type of resource.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Scheduler.ICloudServiceManagementClient.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters provided to the EntitleResource method.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static OperationResponse EntitleResource(this ICloudServiceManagementClient operations, EntitleResourceParameters parameters)
 {
     return(Task.Factory.StartNew((object s) =>
     {
         return ((ICloudServiceManagementClient)s).EntitleResourceAsync(parameters);
     }
                                  , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult());
 }