There's a slightly easier way

Apr 18, 2009 at 3:48 PM
There are few examples of using the SOAP API from C# so it was good to find this project. However the code in the project is redundant if you use a version of C# that supports extension methods. The advice to use WSE is good and when installed you can use the following code to list the EC2 to availability zones:

using System;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Web.Services3.Security.Tokens;

namespace AWSSoap
{
    class Class1
    {
        void Main(string[] args)
        {
            var ec2 = new AWSSoap.EC2.AmazonEC2();
            var cert = new X509Certificate2(@"cert.pem", "");
            ec2.RequestSoapContext.Security.Tokens.Add(new X509SecurityToken(cert));
            var z = new EC2.DescribeAvailabilityZonesType();
            var zones = ec2.DescribeAvailabilityZones(z);
        }
    }
}

Where:

EC2 is the name of the web service you create when referencing the EC2 WSDL file; and
cert.pem is the EC2 X509 certificate you downloaded from your EC2 account

I have not found it necessary to follow the instructions in the code convert the EC2 .pem file to a pkcs12 file.

RequestSoapContext is an extension method provided by the WSE library which seems to do what most of the code attached to this project does.

Finally if like me you've had an EC2 account for a long time but you're just beginning to use the soap API you may see an exception complaining that the certificate has expired even though the same certificate works when bundling AMIs.  If so, you need to delete and recreate your certificate because it's too old and the .net SOAP API will not let you ignore this fact.