2

这是代码,但它提示错误:

图像 o3lceiy3.ioa201305211013360129.vhd 不存在。

subscriptionIdX509Certificate2是有效的

    internal class Program
      {
        public static X509Certificate2 Certificate { get; set; }

        private static void Main(string[] args)
         {
          const string subscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";

                             //@"https://management.core.windows.net/<subscription-id>/services/hostedservices/<cloudservice-name>/deployments";
           var url = string.Format("https://management.core.windows.net/{0}/services/hostedservices/{1}/deployments",
                            subscriptionId, "edoc2cloudtest");
          var myReq = (HttpWebRequest)WebRequest.Create(url);
          myReq.Method = "POST";
          myReq.Headers.Add("x-ms-version", "2012-03-01");
          myReq.Proxy = null;
          myReq.Timeout = 30000;
          myReq.ContentType = "application/xml";
          var postData = ReadConfig();
          using (var reqStream = myReq.GetRequestStream())
          {
            var data = Encoding.UTF8.GetBytes(postData);
            reqStream.Write(data, 0, data.Length);
            reqStream.Flush();
          }
            Certificate = GetX509Certificate();
            myReq.ClientCertificates.Add(Certificate);
          try
          {
            var myRes = (HttpWebResponse) myReq.GetResponse();
          }
          catch (WebException exWeb)
          {
            // Parse the web response.
            Stream responseStream = exWeb.Response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);

            XmlDocument xDocResp = new XmlDocument();
            xDocResp.Load(reader);

            HttpWebResponse responseStatus = (HttpWebResponse)exWeb.Response;

            responseStream.Close();
            reader.Close();

            var result = NiceFormatXml(xDocResp);

            Console.WriteLine(result);
          }

      }

    private static string NiceFormatXml(XmlDocument xDoc)
    {
        StringBuilder niceString = new StringBuilder();

        StringWriter strWriter = new StringWriter(niceString);

        XmlTextWriter xmlWriter = new XmlTextWriter(strWriter);

        xmlWriter.Formatting = Formatting.Indented;

        xDoc.WriteTo(xmlWriter);

        xmlWriter.Close();
        strWriter.Close();

        return niceString.ToString();
    }

    private static X509Certificate2 GetX509Certificate()
    {
        X509Certificate2 certificate2 = null;
        var store = new X509Store("MY", StoreLocation.CurrentUser);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        var collection = store.Certificates;
        var fcollection = collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
        const string certificateThumbprint = "‎‎‎‎7dfbc7369306ed096b7e5c7b4ba6e99f190240e9";
        store.Close();
        if (fcollection.Count > 0)
        {
            foreach (var variable in fcollection)
            {
                if (variable.Thumbprint != null &&
                    variable.Thumbprint.Equals(certificateThumbprint, StringComparison.InvariantCultureIgnoreCase))
                {
                    certificate2 = variable;
                }
            }
        }
        return certificate2;
    }

    private static string ReadConfig()
    {
        string path = System.AppDomain.CurrentDomain.BaseDirectory + "Edoc2Cloud.xml";
        //string path = System.AppDomain.CurrentDomain.BaseDirectory + "VM-CreateVM.xml";
        string s;
        using (var sr = new StreamReader(path, Encoding.GetEncoding("GB2312")))
        {
            s = sr.ReadToEnd();
        }
        return s;
    }
}

这是 XML:

    <Deployment xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Name>EDoc2Test</Name>
    <DeploymentSlot>Staging</DeploymentSlot>
    <Label>EDoc2Testlabe</Label>      
    <RoleList>
    <Role>
    <RoleName>EDoc2TestRoleName</RoleName>
    <RoleType>PersistentVMRole</RoleType>   
    <ConfigurationSets>  
      <ConfigurationSet>        
             <ConfigurationSetType>WindowsProvisioningConfiguration</ConfigurationSetType>
        <ComputerName>computer-name</ComputerName>
        <AdminPassword>APasswor_324d</AdminPassword> 
        <EnableAutomaticUpdates>true</EnableAutomaticUpdates>  
      </ConfigurationSet>
     </ConfigurationSets>
  <AvailabilitySetName>EDoc2TestSetName</AvailabilitySetName>
    <OSVirtualHardDisk>
     <HostCaching>ReadWrite</HostCaching>    
    <DiskName>SomeName-0-20121007173943</DiskName>
    <MediaLink>http://portalvhdsx4flx9dhmjyt1.blob.core.windows.net/vhds/o3lceiy3.ioa201305211013360129.vhd</MediaLink>
    <SourceImageName>o3lceiy3.ioa201305211013360129.vhd</SourceImageName>
  </OSVirtualHardDisk>    
  <RoleSize>Medium</RoleSize>      
</Role>

在此处输入图像描述

4

1 回答 1

0

根据您收到的错误和您指定的 XML,您能否检查一下o3lceiy3.ioa201305211013360129.vhd您的自定义图像中是否有按名称命名的图像?您可以通过登录门户并转到虚拟机 --> 图像来找到该信息。

在此处输入图像描述

有关参数的文档<SourceImageName>指出,当您想使用系统或自定义映像创建虚拟机时需要它。

您可以在此处阅读完整的文档:http: //msdn.microsoft.com/en-us/library/windowsazure/jj157186.aspx#OSVirtualHardDisk

于 2013-06-03T04:09:38.777 回答