【发布时间】:2021-01-26 11:12:51
【问题描述】:
我在 C# 上创建了 WCF 服务。 我需要接受带有 XML 正文的 POST 请求并反序列化 XML。
[DataContract(Name = "EmployeeInformation", Namespace = "urn:test")]
public class EmployeeInformation1
{
[DataMember(Name = "Employee")]
public Employee Employee { get; set; }
}
[DataContract(Name = "Employee", Namespace = "urn:test")]
[KnownType(typeof(Common))]
public class Employee
{
[DataMember(Name = "ID")]
public string ID { get; set; }
[DataMember(Name = "Common")]
public Common Common { get; set; }
}
[DataContract(Name = "Common", Namespace = "urn:test")]
public class Common
{
[DataMember(Name = "ID")]
public string ID { get; set; }
}
//-----------------------------------------------------
[ServiceContract]
public interface IService1
{
[OperationContract]
[ServiceKnownType(typeof(Common))]
[WebInvoke(Method = "POST", UriTemplate = "/test",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare)]
string Test(EmployeeInformation1 xmlstring);
}
服务正确地接收和反序列化对象的根属性。但是,Common 的属性都是空的。 XML
<?xml version="1.0" encoding="utf-8"?>
<EmployeeInformation xmlns="urn:test">
<Employee>
<ID>ID1</ID>
<Common>
<ID2>ID2</ID2>
</Common>
</Employee>
</EmployeeInformation>
【问题讨论】:
-
经过我的测试,我发现你的服务没有问题。我使用 Postman 发送数据。服务可以正常接收数据且不为空。你如何发送数据?我认为这可能是客户端问题。
-
谢谢!我也使用邮递员。也许测试功能不正确?
-
公共字符串测试(EmployeeInformation1 xmlstring) { string param2 = xmlstring.Employee.Common.ID;返回参数2; }
-
邮递员没有收到数据吗?
-
你的配置文件是什么?