获得在.NET中的特定架构的SOAP消息架构、消息、NET、SOAP

2023-09-04 06:27:13 作者:我爱你不怕万人阻拦@

我写在一个SOAP信封,将采取这一信息,并响应与使用相同架构的SOAP消息接收XML对象在.NET中的服务。例如:

I am writing a service in .NET that receives XML objects in a SOAP envelope that will take that message and response with a SOAP message using the same schema. For example:

要求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns="http://schemas.mystuff.org/myschema">
    <Message>
      <Header>...</Header>
      <MessageContent>...</MessageContent>
    </Message>
  </s:Body>
</s:Envelope>

响应:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns="http://schemas.mystuff.org/myschema">
    <Message>
      <Header>...</Header>
      <MessageContent>...</MessageContent>
    </Message>
  </s:Body>
</s:Envelope>

另外,我想最终没有与对象的层次结构,但是XML只是字符串。

Also, I would like to end up not with a hierarchy of objects, but just the string of the XML.

当我使用WCF ServiceContracts这样做,则WCF定义包装额外的一层为它生成WSDL服务,例如:

When I use WCF ServiceContracts to do this then WCF defines an extra layer of wrappers for the WSDL service it generated, for example:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <SendMessageResponse xmlns="http://tempuri.org/">
      <SendMessageResult xmlns:a="http://schemas.datacontract.org/2004/07/CxMessageReceiverRole" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <a:Message>
          ...
        </a:Message>
      </SendMessageResult>
    </SendMessageResponse>
  </s:Body>
</s:Envelope>

什么是对我最好的方式来接收和.NET发送SOAP消息我在哪里控制的消息是什么样子,而不是WCF?它没有被WCF如果这不是最好的方法。另外,我不希望实现一个完整的类结构重新present我的XML架构,而只是接收SOAP体为字符串。

What is the best way for me to receive and send SOAP messages in .NET where I am controlling what the messages look like rather than WCF? It doesn't have to be WCF if that's not the best way. Also, I don't want to implement an entire class structure to represent my XML schema, but instead just receive the SOAP body as a string.

推荐答案

首先,你不能接收SOAP身体的字符串,如果你不发送的字符串。你只能得到XML,但在这种情况下,你的自动生成WSDL将不包含你的身体格式描述。它只会定义 XSD:任何这意味着任何格式良好的XML

First you can't receive SOAP body as string if you do not send string. You can only get XML but in such case your autogenerated WSDL will doesn't contain description of your body format. It will only define xsd:any which means any well formed XML.

当你不希望有包装的元素,你必须使用 MessageContract ,而不是 DataContract 的。 DataContracts始终使用基于操作名称默认包围件。 MessageContracts重写此行为,并定义了顶级元素。

When you don't want to have wrapping element you have to use MessageContract instead of DataContract. DataContracts always use default wrapping elements based on operation name. MessageContracts overrides this behavior and defines top level element.

我认为是这样的:

[MessageContract]
public class Message
{
  [MessageBodyMember]
  public XElement Header { get; set; }
  [MessageBodyMember]
  public XElement MessageContent { get; set; }
}

另一种方法是使用 System.ServiceModel.Channels。消息类和读取邮件内容手动。

Other approach is using System.ServiceModel.Channels.Message class and read content from the message manually.

 
精彩推荐