Calling WCF Services Using SOAP

I am trying to test a WCF service without creating a WCF client. I have similar code / problems presented here .

I need full control over the SOAP payload, so I want to be able to send my own web requests / responses and substitute my own XML as parameters in any method. I also want the returned SOAP XML to be exactly as it is, without creating response objects, exception objects, etc.

As a point of reference, I want to do exactly what the SoapUI is executing the moment you click the execute button and get the answer back. I assume that SoapUI does NOT create a WCF client, builds a request and calls this method, but rather issues a full SOAP call to the WCF service and displays the result.

In response to questions in the comments, the reason why I do not want to create a WCF client is that I want to be isolated from any changes in the service, I do not need to reassemble the links, change my own code, create a separate code for each new service / method, etc., because this process starts automatically after each assembly without interaction.

So, I have hundreds of thousands of XML test parameters that I pass to hundreds of methods without worrying about what they are. We have been working on ASMX web services for many years, and one method (very similar to the link above) handles all web services / methods / test parameters.

When I switch to WCF, I get internal server errors, especially when testing invalid XML nodes: lack of necessary nodes, errors in the creation method for duplicate names, etc. (any error condition). I think it makes sense that there is an easy way to do this with WCF in the same way.

I want EXACTLY what SoapUI sends back, I just need to know how it does it.

+3
source share
2 answers

@ . ASMX, WCF. , / -, SOAP.

WCF - , - . , , , , - SOAP.

SoapUI, Java, -. WSDL, , - ( ) Http Client.

, - WCF. SOAP-, , :

public class Program
{
    public static void Main(string[] args)
    {
        // OK, this is not a WCF web service, but that should not matter :D
        string endpoint = "http://www.html2xml.nl/Services/Calculator/Version1/Calculator.asmx";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
        request.ContentType = "text/xml"; // or application/soap+xml for SOAP 1.2
        request.Method = "POST";
        request.KeepAlive = false;

        //In case you have a proxy to resolve the server name also add these lines
        var proxyServer = new WebProxy("XX.XX.XX.XX", 1234);
        proxyServer.Credentials = CredentialCache.DefaultCredentials; // or username + password
        request.Proxy = proxyServer;

        // you can read these from files
        string payload = @"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:tem=""http://tempuri.org/"">
                               <soapenv:Header/>
                               <soapenv:Body>
                                  <tem:Add>
                                     <tem:a>1</tem:a>
                                     <tem:b>2</tem:b>
                                  </tem:Add>
                               </soapenv:Body>
                            </soapenv:Envelope>";

        byte[] byteArray = Encoding.UTF8.GetBytes(payload);
        request.ContentLength = byteArray.Length;

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(byteArray, 0, byteArray.Length);
        requestStream.Close();

        HttpWebResponse response = null;
        try
        {
             response = (HttpWebResponse)request.GetResponse();
        }
        catch (WebException ex)
        {
             response = (HttpWebResponse)ex.Response;
        }

        Console.WriteLine(string.Format("HTTP/{0} {1} {2}\n", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription));

        // you can write this to files
        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        // cleanp
        reader.Close();
        requestStream.Close();
        responseStream.Close();
        response.Close();
    }
}

SOAP, :

HTTP/1.1 200 OK

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AddResponse xmlns="http://tempuri.org/">
            <AddResult>3</AddResult>
        </AddResponse>
    </soap:Body>
</soap:Envelope>

, ASMX, , WCF - . HTTP-.

, :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:Add>
         <tem:a>x</tem:a>
         <tem:b>y</tem:b>
      </tem:Add>
   </soapenv:Body>
</soapenv:Envelope>

, - :

HTTP/1.1 500 Internal Server Error

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring> ... exception stacktrace here ... </faultstring>
            <detail />
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

SoapUI Junit, - JMeter, , - (, SoapUI) SOAP. , .

+3

, , , ... : (500) Internal Server , . , , , , WebException. WebException , , .. , WebException. , -, , , ( ) .

, , ? , , /, .. WebException? , - , , , , WebException ( , "web" ).

.

0

All Articles