400:糟糕的要求

时间:2013-03-26 11:44:14

标签: c# wcf azure

我正在使用csmanage来访问Azure Management 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

什么可能导致此错误?

1 个答案:

答案 0 :(得分:3)

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

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

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