【问题标题】:Problem with building SOAP message on C# WCF DataMember collection在 C# WCF DataMember 集合上构建 SOAP 消息的问题
【发布时间】:2020-09-05 13:26:23
【问题描述】:

我正在创建一个简单的 WCF 服务来接受下面的 SOAP 消息。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jvm="http://siph.com/JVM">
   <soapenv:Header/>
   <soapenv:Body>
      <jvm:MT_MedicationOrder>
         <Patientid>?</Patientid>
         <Case_number>?</Case_number>
         <DateTime>?</DateTime>
         <Order>
            <MedicationOrder_id>?</MedicationOrder_id>
            <Descr_of_order>?</Descr_of_order>
            <!--Optional:-->
            <Medication_req_type>?</Medication_req_type>
            <!--Zero or more repetitions:-->
            <Infusion_ingredient>
               <Order_Id>?</Order_Id>
               <DrugID>?</DrugID>
               <DrugType>?</DrugType>
            </Infusion_ingredient>
         </Order>
      </jvm:MT_MedicationOrder>
   </soapenv:Body>
</soapenv:Envelope>

我在 节点处遇到问题,我无法将其设为“零次或多次重复”。这是我的 ServiceContract 和 DataContract 的部分代码

服务合同

[ServiceContract(Namespace = "http://siph.com/JVM")]
    public interface IMedicationOrder
    {
        [OperationContract]
        MedicationOrderResponse ProcessOrder(MedicationOrderRequest req);
    }

    [MessageContract(WrapperName = "MT_MedicationOrder")]
    public class MedicationOrderRequest
    {
        //[MessageHeader]
        //public string Dummy;

        #region Message Body
        [MessageBodyMember(Namespace = "", Name = "Patientid", Order = 1)]
        public string PatientId;

        [MessageBodyMember(Namespace = "", Name = "Case_number", Order = 2)]
        public string CaseNumber;

        [MessageBodyMember(Namespace = "", Name = "DateTime", Order = 3)]
        public string RequestDateTime;

        [MessageBodyMember(Namespace = "", Name = "Order", Order = 4)]
        public MedicationOrderTransaction OrderTransaction;
        #endregion
    }

MedicationOrderTransaction 服务合同

[DataContract(Namespace = "")]
    public class MedicationOrderTransaction
    {
        [DataMember(Name = "MedicationOrder_id", Order = 1, IsRequired = true)]
        public string MedicationOrderID;
        [DataMember(Name = "Descr_of_order", Order = 2)]
        public string DescriptionOfOrder;
        
        [DataMember(Name = "Medication_req_type", Order = 55)]
        public string MedicationReqType;
        [DataMember(Name = "Infusion_ingredient", Order = 56)]
        public List<InfusionIngredientTransactionList> infusionIngredient;
    }

DataContract InfusionIngredientTransactionList

[DataContract]
    public class InfusionIngredientTransactionList
    {
        [DataMember(Name = "Order_Id", Order = 1)]
        public string OrderID;
        [DataMember(Name = "Drug_Id", Order = 2)]
        public string DrugID;
        [DataMember(Name = "DrugType", Order = 3)]
        public string DrugType;
    }

结果:请注意,在 Infusion_Ingredient 节点内创建了一个 siph:InfusionIngredientTransactionList 集合,而不是应该是一个集合的 Infusion_ingredient 本身。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:jvm="http://siph.com/JVM" xmlns:siph="http://schemas.datacontract.org/2004/07/SIPH.WebService.SAP.Model">
   <soapenv:Header/>
   <soapenv:Body>
      <jvm:MT_MedicationOrder>
         <Patientid>?</Patientid>
         <Case_number>?</Case_number>
         <DateTime>?</DateTime>
         <Order>
            <MedicationOrder_id>?</MedicationOrder_id>
            <Descr_of_order>?</Descr_of_order>
            <Medication_req_type>?</Medication_req_type>
            <!--Optional:-->
            <Infusion_ingredient>
             <!--Zero or more repetitions:-->
             <siph:InfusionIngredientTransactionList>
              <!--Optional:-->
              <siph:Order_Id>?</siph:Order_Id>
              <!--Optional:-->
              <siph:Drug_Id>?</siph:Drug_Id>
              <!--Optional:-->
              <siph:DrugType>?</siph:DrugType>
             </siph:InfusionIngredientTransactionList>
           </Infusion_ingredient>
         </Order>
      </jvm:MT_MedicationOrder>
   </soapenv:Body>
</soapenv:Envelope>

请告诉我我做错了什么。谢谢。

【问题讨论】:

  • Xml 序列化列表自动创建两个 xml 元素 。两个防止你需要将两个xml元素放入列表属性上方的c#类中:[XmlElement()]声明一个元素只会创建一个标签。
  • 我认为您可以使用消息契约,它允许您指定所需的 SOAP 消息的精确结构。更多关于消息合约的信息,可以参考这个链接:docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/…

标签: c# wcf soap


【解决方案1】:

我还不能“评论”,所以尝试回答。所以这个建议可能有点过时了,但这会有所帮助吗:

    [DataMember(Name = "Infusion_ingredient", Order = 56)]
    public List<InfusionIngredientTransactionList> infusionIngredient { get; set; } = new List<InfusionIngredientTransactionList>();

我也同意 C# 类应该位于 List 属性之上,因为该类的对象已添加到 List 集合中。如果不是主题或有帮助,我深表歉意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2016-07-29
    • 1970-01-01
    相关资源
    最近更新 更多