【问题标题】:XmlElement wrapped in extra tag in SOAP requestXmlElement 包裹在 SOAP 请求中的额外标记中
【发布时间】:2010-06-30 08:58:57
【问题描述】:

您好,我有一个使用一种方法的服务代理:

void SendRequest(MyMessage msg);

MyMessage 定义如下:

[MessageContract(IsWrapped=false)]
public class MyMessage{
    [MessageBodyMember(Order=0)]
    public XmlElement Body;

    public MyMessage(XmlElement Body){
        this.Body = Body;
    }
}

现在,问题是当我发送请求时,Body 被包裹在一个标签中,如下所示:

<s:Body>
    <Body>
         <MyMessage>
               <SomeData>Hello world</SomeData>
         </MyMessage>
    </Body>
</s:Body>

当我真正想要的是:

<s:Body>
    <MyMessage>
               <SomeData>Hello world</SomeData>
    </MyMessage>
</s:Body>

有人可以帮忙吗?我开始绝望了! :/

编辑:我想发送 XmlElement 的原因是该服务将接受各种 XML 格式,并将在服务器端进行 xsd 验证和转换。这应该只是一种包装。

我也无法让端点服务器简单地接受“错误”的 xml 结构,因为我无法控制它。

【问题讨论】:

    标签: c# .net wcf soap c#-4.0


    【解决方案1】:

    好吧,看来我必须完全放弃 WCF 才能让它工作。

    我所做的是创建了两个简单的方法:

        XDocument ConstructSoapEnvelope(string messageId, string action, XDocument body)
        {
            XDocument xd = new XDocument();
            XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";
            XNamespace a = "http://www.w3.org/2005/08/addressing";
    
            XElement soapEnvelope = new XElement(s + "Envelope", new XAttribute(XNamespace.Xmlns + "s", s), new XAttribute(XNamespace.Xmlns + "a", a));
            XElement header = new XElement(s + "Header");
            XElement xmsgId = new XElement(a + "MessageID", "uuid:" + messageId);
            XElement xaction = new XElement(a + "Action", action);
            header.Add(xmsgId);
            header.Add(xaction);
    
            XElement soapBody = new XElement(s + "Body", body.Root);
            soapEnvelope.Add(header);
            soapEnvelope.Add(soapBody);
            xd = new XDocument(soapEnvelope);
            return xd;
        }
    
        string HttpSOAPRequest(XmlDocument doc, string add, string proxy, X509Certificate2Collection certs)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(add);
            req.ClientCertificates = certs;
            if (proxy != null) req.Proxy = new WebProxy("", true);
    
            req.Headers.Add("SOAPAction", "\"\"");
    
            req.ContentType = "text/xml;charset=\"utf-8\"";
            req.Accept = "text/xml";
            req.Method = "POST";
            Stream stm = req.GetRequestStream();
            doc.Save(stm);
            stm.Close();
            WebResponse resp = req.GetResponse();
            stm = resp.GetResponseStream();
            StreamReader r = new StreamReader(stm);
    
           return r.ReadToEnd();
        }
    

    ConstructSoapEnvelope 只是创建一个带有标题和正文的肥皂信封(通过 ws-addressing 为我的需要量身定制)

    并且 HttpSOAPRequest 是此处找到的稍作修改的版本: http://www.eggheadcafe.com/articles/20011103.asp

    我必须修改它以接受客户端证书,以便我的 SSL 通信正常工作..

    希望这对其他人也有帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-15
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      相关资源
      最近更新 更多