2012-07-24 3 views
0

아래 명시된 Create Deployment 메서드에 액세스하려고합니다. http://msdn.microsoft.com/en-us/library/windowsazure/ee460813 패키지를 BLOB에 업로드하고 구성 파일을 탐색했습니다. Azure 포털에서 수동으로 패키지 및 설정 파일을 업로드하려고 시도했는지 확인했습니다. 아래는 AzureEcoystemCloudService가 패키지를 배포하려는 클라우드 서비스 이름 인 배포 작성을 위해 작성한 코드입니다.REST API : 배포 오류 발생 BadRequest

byte[] bytes = new byte[fupldConfig.PostedFile.ContentLength + 1]; 
     fupldConfig.PostedFile.InputStream.Read(bytes, 0, bytes.Length); 
     string a = Encoding.UTF8.GetString(bytes, 0, bytes.Length); 
     string base64ConfigurationFile = a.ToBase64(); 
     X509Certificate2 certificate = CertificateUtility.GetStoreCertificate 
     (ConfigurationManager.AppSettings["thumbprint"].ToString()); 


     HostedService.CreateNewDeployment(certificate, ConfigurationManager.AppSettings 
     ["SubscriptionId"].ToString(), "2012-03-01", "AzureEcoystemCloudService", 
     Infosys.AzureEcosystem.Entities.Enums.DeploymentSlot.staging, 
     "AzureEcoystemDeployment", "http://shubhendustorage.blob.core.windows.net/shubhendu 
     storage/Infosys.AzureEcoystem.Web.cspkg", "AzureEcoystemDeployment", 
     base64ConfigurationFile, true, false); 



    /// <summary> 
    /// 
    /// </summary> 
    /// <param name="certificate"></param> 
    /// <param name="subscriptionId"></param> 
    /// <param name="version"></param> 
    /// <param name="serviceName"></param> 
    /// <param name="deploymentSlot"></param> 
    /// <param name="name"></param> 
    /// <param name="packageUrl"></param> 
    /// <param name="label"></param> 
    /// <param name="base64Configuration"></param> 
    /// <param name="startDeployment"></param> 
    /// <param name="treatWarningsAsError"></param> 
    public static void CreateNewDeployment(X509Certificate2 certificate, string 
    subscriptionId,string version, string serviceName, 
    Infosys.AzureEcosystem.Entities.Enums.DeploymentSlot deploymentSlot, string name, 
    string packageUrl, string label, string base64Configuration, 
     bool startDeployment, bool treatWarningsAsError) 
    { 
     Uri uri = new Uri(String.Format(Constants.CreateDeploymentUrlTemplate, 
     subscriptionId, serviceName, deploymentSlot.ToString())); 
     XNamespace wa = Constants.xmlNamespace; 
     XDocument requestBody = new XDocument(); 

     String base64ConfigurationFile = base64Configuration; 
     String base64Label = label.ToBase64(); 
     XElement xName = new XElement(wa + "Name", name); 
     XElement xPackageUrl = new XElement(wa + "PackageUrl", packageUrl); 
     XElement xLabel = new XElement(wa + "Label", base64Label); 
     XElement xConfiguration = new XElement(wa + "Configuration", 
     base64ConfigurationFile); 
     XElement xStartDeployment = new XElement(wa + "StartDeployment", 
     startDeployment.ToString().ToLower()); 
     XElement xTreatWarningsAsError = new XElement(wa + "TreatWarningsAsError", 
     treatWarningsAsError.ToString().ToLower()); 
     XElement createDeployment = new XElement(wa + "CreateDeployment"); 

     createDeployment.Add(xName); 
     createDeployment.Add(xPackageUrl); 
     createDeployment.Add(xLabel); 
     createDeployment.Add(xConfiguration); 
     createDeployment.Add(xStartDeployment); 
     createDeployment.Add(xTreatWarningsAsError); 
     requestBody.Add(createDeployment); 
     requestBody.Declaration = new XDeclaration("1.0", "UTF-8", "no"); 
     XDocument responseBody; 
     RestApiUtility.InvokeRequest(
      uri, Infosys.AzureEcosystem.Entities.Enums.RequestMethod.POST.ToString(), 
     HttpStatusCode.Accepted, requestBody, certificate, version, out responseBody); 
    } 
    /// <summary> 
    /// A helper function to invoke a Service Management REST API operation. 
    /// Throws an ApplicationException on unexpected status code results. 
    /// </summary> 
    /// <param name="uri">The URI of the operation to invoke using a web request.</param> 
    /// <param name="method">The method of the web request, GET, PUT, POST, or 
    DELETE.</param> 
    /// <param name="expectedCode">The expected status code.</param> 
    /// <param name="requestBody">The XML body to send with the web request. Use null to 
    send no request body.</param> 
    /// <param name="responseBody">The XML body returned by the request, if any.</param> 
    /// <returns>The requestId returned by the operation.</returns> 
    public static string InvokeRequest(
     Uri uri, 
     string method, 
     HttpStatusCode expectedCode, 
     XDocument requestBody, 
     X509Certificate2 certificate, 
     string version, 
     out XDocument responseBody) 
    { 
     responseBody = null; 
     string requestId = String.Empty; 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri); 
     request.Method = method; 
     request.Headers.Add("x-ms-Version", version); 
     request.ClientCertificates.Add(certificate); 
     request.ContentType = "application/xml"; 
     if (requestBody != null) 
     { 
      using (Stream requestStream = request.GetRequestStream()) 
      { 
       using (StreamWriter streamWriter = new StreamWriter(
        requestStream, System.Text.UTF8Encoding.UTF8)) 
       { 
        requestBody.Save(streamWriter, SaveOptions.DisableFormatting); 
       } 
      } 
     } 
     HttpWebResponse response; 
     HttpStatusCode statusCode = HttpStatusCode.Unused; 
     try 
     { 
      response = (HttpWebResponse)request.GetResponse(); 
     } 
     catch (WebException ex) 
     { 
      // GetResponse throws a WebException for 4XX and 5XX status codes 
      response = (HttpWebResponse)ex.Response; 
     } 
     try 
     { 
      statusCode = response.StatusCode; 
      if (response.ContentLength > 0) 
      { 
       using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) 
       { 
        responseBody = XDocument.Load(reader); 
       } 
      } 
      if (response.Headers != null) 
      { 
       requestId = response.Headers["x-ms-request-id"]; 
      } 
     } 
     finally 
     { 
      response.Close(); 
     } 
     if (!statusCode.Equals(expectedCode)) 
     { 
      throw new ApplicationException(string.Format(
       "Call to {0} returned an error:{1}Status Code: {2} ({3}):{1}{4}", 
       uri.ToString(), 
       Environment.NewLine, 
       (int)statusCode, 
       statusCode, 
       responseBody.ToString(SaveOptions.OmitDuplicateNamespaces))); 
     } 
     return requestId; 
    } 




    But every time we are getting the below error from the line 

    response = (HttpWebResponse)request.GetResponse(); 

    <Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
     <Code>BadRequest</Code> 
     <Message>The specified configuration settings for Settings are invalid. Verify that the service configuration file is a valid XML file, and that role instance counts are specified as positive integers.</Message> 

</Error> 

감사합니다, Shubhendu

+0

참조 http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/08ea3289-0f10-4bc5-8f39-6124a1afd22c/ – smarx

답변

4

나는 푸른 나머지 API에 대한 요청과 문제를 했어. WebException의 응답을 읽는 것이 매우 중요하다는 것을 알게되었습니다.

try 
{ 
    //Call Azure REST API 
} 
catch(WebException ex) 
{ 
    var err = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); 

    Console.WriteLine(err); 

    throw; 
} 

이렇게하면 요청의 기본 문제에 대한 충분한 정보를 얻을 수 있습니다.

0

Azure SDK 2.2부터는 문서가 끝나고 잘못된 구성 문자열을 base64로 인코딩 할 필요가 없습니다. 따라서 모든 byte [] 변환을 base64로하는 대신 UFT8 문자열을 전달하십시오.