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

Posted

技术标签:

【中文标题】C# Restful WCF 服务。无法在帖子正文中反序列化 XML【英文标题】:C# Restful WCF service. Can't Deserialize XML in post body 【发布时间】: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; 邮递员没有收到数据吗? 你的配置文件是什么? 【参考方案1】:

这是我的演示:

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();
        
    

App.config:

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

【讨论】:

谢谢!我的问题出在不正确的 XML 中。 需要高于

以上是关于C# Restful WCF 服务。无法在帖子正文中反序列化 XML的主要内容,如果未能解决你的问题,请参考以下文章

带有图像和正文的 C# RESTful POST

使用 restsharp 在 WCF Web api 服务上发布 http 帖子

Restful WCF 无法达到 operationcontract

在 Android 中使用 Restful WCF 服务

使用 RESTful WCF 和 Windows 窗体的用户/通过身份验证

使用 transportCredentialOnly 安全性对 RESTful WCF 服务的跨域 Ajax JSON POST 支持