使用SOAP调用WCF服务 [英] calling WCF services using SOAP

查看:112
本文介绍了使用SOAP调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在不创建WCF客户端的情况下测试WCF服务.我在此处中显示了类似的代码/问题.

I'm trying to test a WCF service without creating a WCF client. I have similar code/issues as presented here.

我想要完全控制SOAP有效负载,因此我希望能够发出自己的Web请求/响应,并将自己的XML替换为任何方法中的参数.我还希望返回SOAP XML完全照原样,而不创建响应对象,故障异常对象等.

I want full control on the SOAP payload so I want to be able to issue my own web requests/responses and substitute my own XML as the parameters into any method. I also want the return SOAP XML exactly as is, without creating response objects, fault exception objects, etc.

作为参考,我想准确地完成 SoapUI 在您单击时所做的操作执行按钮并返回响应.我只是假设SoapUI不会创建WCF客户端,构建请求并调用该方法,而是向WCF服务发出完整的SOAP调用并显示结果.

As a point of reference, I want to do exactly what SoapUI is doing at the point you click the execute button and get the response back. I'm only assuming that SoapUI is NOT creating a WCF client, building the request and calling the method, but rather issuing a full SOAP call to the WCF service and displaying the result.

为回答评论中的问题,我不想创建WCF客户端的原因是,我希望不受服务的任何所有更改的影响,而不必重建引用,修改自己的代码,为每个新服务/方法等创建单独的代码等,因为此过程在每次构建后都会自动启动,而无需任何交互.

In answer to the questions in the comments, the reason I don't want to create a WCF client is that I want to be insulated from any and all changes to the service, not have to rebuild references, modify my own code, create separate code for every new service/method, etc. because this process is kicked off automatically after every build, with no interaction.

因此,我有成千上万个测试XML参数,可以将它们传递给数百种方法,而无需关心它们的含义.我们已经在ASMX Web服务上完成了多年,并且一种方法(与上面的链接非常相​​似)处理了所有Web服务/方法/测试参数.

So, I have hundreds of thousands of test XML parameters that I pass into hundreds of methods, without caring about what they are. We have done it for years on ASMX web services and the one method (very similar to the above link) handled all web services/methods/test parameters.

切换到WCF时,我会收到内部服务器错误,尤其是在测试无效的XML节点时:缺少必需的节点,名称重复的创建方法中的错误等(任何错误情况).我认为使用WCF以相同的方式执行此操作很简单,这是有道理的.

With switching to WCF I get internal server errors especially when testing invalid XML nodes: missing required nodes, errors in a create 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 manner.

我确切地想要SoapUI发送回什么,我只需要知道它的工作方式即可.

I want EXACTLY what SoapUI is sending back, I just need to know how it's doing it.

推荐答案

@John Saunders的评论正确.无论您使用ASMX做什么,都应该能够使用WCF.实际上,只要您执行适当的SOAP请求,Web服务使用哪种框架/技术都没有关系.

@John Saunders is correct in his comment. Whatever you do with ASMX, you should be able to do with WCF. In fact it does not matter what kind of framework/technology your web service uses as long as you do a proper SOAP request.

WCF 只是一个有助于构建面向服务的框架应用程序.像其他任何此类框架一样,它使您能够专注于将要提供的实际服务,并照顾了将该服务公开为SOAP Web服务所需的所有管道功能.

WCF is just a framework that helps build service-oriented applications. Like any other framework of that kind, it lets you concentrate on the actual service that you will provide, taking care of all the plumbing functionality needed to expose that service as a SOAP web service.

对于SoapUI,它是一个Java工具,可让您测试Web服务.当您将其输入WSDL时,它会动态创建请求样本,然后使用以下样本(如果我没有记错的话)将样本发送到Web服务: Http客户端.

As for SoapUI, its a Java tool that allows you to test web services. When you feed it a WSDL, it dynamically creates request samples which are then sent to the web service with (if I'm not mistaken) Http Client.

如果您有WCF Web服务,则不会发生任何事情.即使使用这样的基本客户端,仍然可以进行SOAP通信:

Nothing fancy happens if you have a WCF web service. It's still SOAP communication that you can do even with a basic client like this:

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响应,在这种情况下,是这样的:

You get back a SOAP response, in this case this:

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请求的响应.

and it does not matter if it was an ASMX that generated it, or WCF or whatever. It's the response to a HTTP request.

相反,如果您发送的是无效消息,例如:

If instead you send an invalid message, like:

<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 ,您甚至可以使用

You can automate the tests with SoapUI or even integrate them with Junit, you could even use something like JMeter which although not designed specially for web services (like SoapUI) it can test SOAP. And you can off course use that basic client I added to my answer.

这篇关于使用SOAP调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆