【发布时间】: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(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/…