1

我正在使用 csmanage 访问 Azure 管理 API。这是我的代码:

    private const string subscriberID = "<id>";

    static void Main(string[] args)
    {
        // The thumbprint value of the management certificate.
        // You must replace the string with the thumbprint of a 
        // management certificate associated with your subscription.
        string certThumbprint = "<thumbprint>";

        // Create a reference to the My certificate store.
        X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);

        // Try to open the store.
        try
        {
            certStore.Open(OpenFlags.ReadOnly);
        }
        catch (Exception e)
        {
            throw;
        }

        // Find the certificate that matches the thumbprint.
        X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, certThumbprint, false);
        certStore.Close();

        // Check to see if our certificate was added to the collection. If no, throw an error, if yes, create a certificate using it.
        if (0 == certCollection.Count)
        {
            throw new Exception("Error: No certificate found containing thumbprint " + certThumbprint);
        }

        // Create an X509Certificate2 object using our matching certificate.
        X509Certificate2 certificate = certCollection[0];



        var serviceManagment = ServiceManagementHelper.CreateServiceManagementChannel("WindowsAzureEndPoint", new X509Certificate2(certificate));
        var x = serviceManagment.ListHostedServices(subscriberID);

        foreach (HostedService s in x)
        {
            Console.WriteLine(s.ServiceName);
        }
    }

这在控制台应用程序中运行良好。但是,当我在 WCF 项目(作为服务实现)中执行完全相同的代码时,我得到400 - Bad Request了结果。

什么可能导致此错误?

4

1 回答 1

4

不是真正的答案,但您可以做的一件事是通过使用类似于以下的代码捕获 Web 异常来查看有关 400 错误的更多详细信息:

    catch (WebException webEx)
    {
        string errorDetail = string.Empty;
        using (StreamReader streamReader = new StreamReader(webEx.Response.GetResponseStream(), true))
        {
            errorDetail = streamReader.ReadToEnd();
        }
    }

errorDetail将是一个可以为您提供更多信息的 XML。

于 2013-03-26T13:19:20.417 回答