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

I want to write some integration tests to compare the WSDL generated by WCF services with previous (and published) versions. This is to ensure that service contracts do not differ from the time of issue.

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

I think I could recreate my IIS hosting environment in a 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 have any other ideas?

EDIT: Obviously this code just creates a host for the service, I am still skipping the client code to get the WSDL definition.

+4
source share
6 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);
 }

+2
source

Check WsdlExporter on MSDN. It was used to generate wsdl in WCF. You can also look in svcutil with a reflector to find out how to generate wsdl information, since the tool can generate wsdl from a dll file.

Another way of comparing would be to use the svcutil tool to generate wsdl and compare it with the saved / base version of the service. Run svcutil in your test and check the output for old files. Not a completely standalone test, since you will need svcutil ...

+1
source

How about something like that? Creating WSDL using C #

+1
source

One thing you must be careful about is comparing the entire WSDL. WCF breaks WSDL, unlike WSDL of classical web services (asmx). This means that the core of the information is on the WSDL page, however there will also be several XSD (.svc? XSD = XSD0, 1, 2 ...) and, possibly, several WSDL pages (? WSDL and? WSDL = WSDL0, for example )

One way to achieve this is to create a web request to retrieve data from the root wsdl. Then you can search for WSDL for something like (yourServicename) .svc? WSDL = WSLD0 and (yourServicename)? XSD = XSD0, etc., creating additional web requests for each WSDL and XSD.

0
source

You might be better off using SoapUI to check WSDL and not directly on NUnit.

If you want to call SoapUI from NUnit, this is possible, but a little awkward. See http://enthusiasm.soapui.org/forum/viewtopic.php?f=2&t=15 for details.

0
source

Same answer translated to VB

  Using host = New ServiceHost(GetType(MyHelloWorldWcfLib.HelloWorldServer), New Uri("http://localhost:8000/Omega")) host.AddServiceEndpoint(GetType(MyHelloWorldWcfLib.IHelloWorld), New BasicHttpBinding(), "Primary") Dim behavior = New ServiceMetadataBehavior() behavior.HttpGetEnabled = True host.Description.Behaviors.Add(behavior) host.AddServiceEndpoint(GetType(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex") host.Open() Dim wsdl As String = Nothing Using wc = New System.Net.WebClient() Using stream = wc.OpenRead("http://localhost:8000/Omega?wsdl") Using sr = New IO.StreamReader(stream) wsdl = sr.ReadToEnd() End Using End Using End Using Console.Write(wsdl) End Using 
0
source

Source: https://habr.com/ru/post/1312952/


All Articles