繁体   English   中英

C# Restful WCF 服务。 无法反序列化帖子正文中的 XML

[英]C# Restful WCF service. Can't Deserialize XML in post body

我在 C# 上创建了 WCF 服务。 我需要接受带有 XML 正文和反序列化 XML 的 POST 请求。

[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>

这是我的演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace ConsoleApp84
{ 
    [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);
    }
    public class Service1 : IService1
    {
        public string Test(EmployeeInformation1 xmlstring)
        {
            Console.WriteLine(xmlstring.Employee.Common.ID);
            return xmlstring.Employee.Common.ID;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost selfHost = new ServiceHost(typeof(Service1));
            selfHost.Open();
            Console.WriteLine("Service open");
            Console.ReadKey();
            selfHost.Close();
        }
    }
}

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>

    <system.serviceModel>
        <services>

            <service name="ConsoleApp84.Service1" behaviorConfiguration="ServiceBehavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8012/ServiceModelSamples/service"/>
                    </baseAddresses>
                </host>

                <endpoint address=""
                          binding="webHttpBinding"
                          contract="ConsoleApp84.IService1"
                          behaviorConfiguration="ESEndPointBehavior" />
            </service>
        </services>


        <behaviors>
            <endpointBehaviors>
                <behavior name="ESEndPointBehavior">
                 <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>

            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>

        </behaviors>

    </system.serviceModel>
</configuration>

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM