Bv202
Bv202

Reputation: 4044

400: bad request

I'm using csmanage to access the Azure Management API. This is my code:

    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);
        }
    }

This works fine in a console application. However, when I execute the exact same code in a WCF project (as a service implementation) I'm getting 400 - Bad Request as a result.

What could cause this error?

Upvotes: 1

Views: 2087

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

Not really an answer but one thing you could do is see more details about 400 error by catching the web exception using code similar to the following:

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

Here errorDetail will be an XML that should give you more information.

Upvotes: 4

Related Questions