Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically generate WSDL from WCF service (Integration Testing)

I am looking to write some integration tests to compare the WSDL generated by WCF services against previous (and published) versions. This is to ensure the service contracts don't differ from time of release.

I would like my tests to be self contained and not rely on any external resources such as hosting on IIS.

I am thinking that I could recreate my IIS hosting environment within the test with something like...

using (ServiceHost host = new ServiceHost(typeof(NSTest.HelloNS), new Uri("http://localhost:8000/Omega")))
{
    host.AddServiceEndpoint(typeof(NSTest.IMy_NS), new BasicHttpBinding(), "Primary");
    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;
    host.Description.Behaviors.Add(behavior);
    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    host.Open();
}

Does anyone else have any better ideas?

EDIT: Obviously this code is simply creating a host for the service, I am still missing the client code to obtain the WSDL definition.

like image 272
David Christiansen Avatar asked Oct 15 '22 04:10

David Christiansen


1 Answers

Just use WebClient and ?wsdl sufix in URL

using (ServiceHost host = new ServiceHost(typeof(NSTest.HelloNS), 
new Uri("http://localhost:8000/Omega")))
{
    host.AddServiceEndpoint(typeof(NSTest.IMy_NS), new BasicHttpBinding(), "Primary");
    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
    behavior.HttpGetEnabled = true;
    host.Description.Behaviors.Add(behavior);
    host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
    host.Open();

    string wsdl = null;
    using (WebClient wc = new WebClient())
    {
        using (var stream = wc.OpenRead("localhost:8000/Omega?wsdl"))
        {
            using (var sr = new StreamReader(stream))
            {
                wsdl = sr.ReadToEnd();
            }
        }
    }
    Console.Write(wsdl);
}

like image 159
Pavel Savara Avatar answered Oct 29 '22 03:10

Pavel Savara