c# - WCF MessageContract 包装和列表

标签 c# wcf web-services xsd messagecontract

我收到了一位客户关于他们的网络服务客户端如何工作的规范。该规范是从服务以及相应的 XSD 发送和接收的实际 SOAP XML 消息。客户希望我实现符合客户要求的网络服务。客户端是用 axis2 ws-stack 编写的,我想做的是在 WCF 中创建一个 Web 服务,该服务将接受客户端发出的请求并返回符合他们期望的 XML 的响应。在这个问题中,我将只发布与请求关联的 XML 和 XSD,因为如果我能让它工作,响应将以类似的方式进行。

我收到的 XML 如下:

POST /axis2/services/SampleService HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "sendCommand"
User-Agent: Axis2
Host: 127.0.0.1:7777
Content-Length: 347
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
   <soapenv:Body> 
      <SendCommandRequest xmlns="http://something.org/"> 
         <CMD> 
            <Station Address="ABC"> 
               <Platform Address="DEF"> 
                  <Command>5</Command> 
               </Platform> 
             </Station> 
         </CMD> 
       </SendCommandRequest> 
    </soapenv:Body>
</soapenv:Envelope>

这是相应的 XSD 的样子:

<xsd:complexType name="SendCommandRequestType"> 
    <xsd:sequence>  
        <xsd:element name="Station"> 
            <xsd:complexType> 
                <xsd:attribute name="Address" type="xsd:string" use="required" /> 
                <xsd:sequence> 
                    <xsd:element minOccurs="0" maxOccurs="1" name="Platform"> 
                        <xsd:complexType> 
                            <xsd:attribute name="Address" type="xsd:string" use="required" /> 
                            <xsd:sequence> 
                                <xsd:element name="Command"> 
                                    <xsd:simpleType> 
                                        <xsd:restriction base="xsd:string"> 
                                            <xsd:enumeration value="-1"/> 
                                            <xsd:enumeration value="0"/> 
                                            <xsd:enumeration value="1"/> 
                                            <xsd:enumeration value="2"/> 
                                            <xsd:enumeration value="3"/> 
                                            <xsd:enumeration value="4"/> 
                                            <xsd:enumeration value="5"/> 
                                        </xsd:restriction> 
                                    </xsd:simpleType> 
                                </xsd:element> 
                            </xsd:sequence> 
                        </xsd:complexType> 
                    </xsd:element> 
                </xsd:sequence> 
            <xsd:complexType>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

我已经开始用 WCF/MessageContract 格式编写类型,但我在处理列表等方面遇到困难,因为它们是双重包装的。

我的 MessageContracts 看起来像这样:

[MessageContract(WrapperName = "SendCommandRequest", WrapperNamespace = "http://something.org/")]
public class SendCommandRequest
{
    [MessageBodyMember(Name="CMD")]
    public CMD cmd = new CMD();
}

[MessageContract(IsWrapped=false)]
public class CMD
{
    [MessageBodyMember(Name="Station")]
    public List<Station> stations = new List<Station>();
}

[MessageContract(IsWrapped=false)]
public class Station
{

    [MessageBodyMember]
    public List<Platform> platforms = new List<Platform>();
    [MessageBodyMember(Name="Address")]
    public String Address; 
}

[MessageContract(WrapperName = "Platform")]
public class Platform
{
    [MessageBodyMember(Name = "Address")]
    public String Address;
}

当我使用 SoapUI 时,我从网络服务得到以下响应:

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <SendCommandRequest xmlns="http://ttraflinariawebservice.org/">
         <CMD xmlns="http://tempuri.org/" xmlns:a="http://schemas.datacontract.org/2004/07/GeldImport" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:stations>
               <a:Station>
                  <a:Address>test</a:Address>
                  <a:platforms>
                     <a:Platform>
                        <a:Address>b</a:Address>
                     </a:Platform>
                  </a:platforms>
               </a:Station>
            </a:stations>
         </CMD>
      </SendCommandRequest>
   </s:Body>
</s:Envelope>

如您所见,它不符合客户期望的 XML 格式。如何让 MessageContract 符合客户期望的 XML?我不知何故需要使列表不像它们那样双重换行,并且类的属性似乎被添加到类名中。

如果您希望我提供更多信息和代码,我可以这样做。不想用可能与问题无关的内容填满整个帖子。

编辑:

提供的 XSD 文件格式不正确。为了解决这个问题,我从提供的 XML 文件中重新生成了一个 XSD。然后我使用 WSCF.blue 工具为 XSD 生成数据契约代码。

我进行了更改,以便服务契约(Contract)使用 doc litteral 格式来符合 axis2 soap1.1

[XmlSerializerFormat(Use = OperationFormatUse.Literal, Style = OperationFormatStyle.Document, SupportFaults = true)]
[ServiceContract]
public interface MyService

我还更改了操作契约(Contract),将 System.ServiceModel.Channels.Message 作为输入和输出消息,然后使用生成的类(我从 XSD 生成)手动序列化和反序列化 xml。

最佳答案

最初尝试的问题是您试图使用[MessageContract] 类来定义消息的数据 协定(架构)。消息契约仅用作最顶层的类(定义什么进入消息头和什么进入消息体)。其他类需要使用您正在使用的任何序列化程序的属性(并且由于您具有 XML 属性,因此需要使用 XmlSerializer)。下面的代码显示了如何获得符合您提供的架构的对象模型。您可以使用诸如 Fiddler 之类的工具来查看它发送的请求。

public class StackOverflow_13739729
{
    [ServiceContract(Namespace = "http://something.org")]
    public interface ITest
    {
        [XmlSerializerFormat, OperationContract(Name = "sendCommand")]
        void SendCommand(SendCommandRequest req);
    }

    public class Service : ITest
    {
        public void SendCommand(SendCommandRequest req)
        {
            Console.WriteLine("In service");
        }
    }

    [MessageContract(WrapperName = "SendCommandRequest", WrapperNamespace = "http://something.org")]
    public class SendCommandRequest
    {
        [MessageBodyMember]
        public CMD CMD { get; set; }
    }

    [XmlType]
    public class CMD
    {
        [XmlElement]
        public Station Station { get; set; }
    }

    public class Station
    {
        [XmlAttribute]
        public string Address { get; set; }

        [XmlElement]
        public Platform Platform { get; set; }
    }

    public class Platform
    {
        string[] validCommands = new[] { "-1", "0", "1", "2", "3", "4", "5" };
        string[] command;

        [XmlAttribute]
        public string Address { get; set; }

        [XmlElement]
        public string[] Command
        {
            get { return this.command; }
            set
            {
                if (value != null)
                {
                    if (!value.All(c => validCommands.Contains(c)))
                    {
                        throw new ArgumentException("Invalid command");
                    }
                }

                this.command = value;
            }
        }
    }

    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();

        SendCommandRequest req = new SendCommandRequest
        {
            CMD = new CMD
            {
                Station = new Station
                {
                    Address = "ABC",
                    Platform = new Platform
                    {
                        Address = "DEF",
                        Command = new string[] { "5" }
                    }
                }
            }
        };

        proxy.SendCommand(req);

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

关于c# - WCF MessageContract 包装和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739729/

相关文章:

php - 在android中使用okhttp将数组或jsonobject传递给php url?

c# - 将 WebClient 错误作为字符串获取

c# - 如何在 AWS Lambda C# 实现中使用依赖注入(inject)

c# - asp .net core 中的自动 Controller 检测

c# - WCF 中的FaultException 与异常

java - 部署在 tomcat 7.0 中的 websphere 7.0 JAXWS webservice 不工作

java - 以复杂数据类型作为输入的 Web 服务

c# - 无法从 GetCustomAttributes 取回属性

Azure 服务 WebRole 中托管的 WCF REST 服务 : AccessControlService?

.net - 如何从 AfterReceiveReply 中的 WCF 消息中提取错误代码