c# - 并非 WCF 数据协定中的所有参数都通过 Web 服务调用

标签 c# xml wcf rest

在创建 WCF Rest 服务时,我注意到并非我的 Web 服务中的所有参数都进入了我的实现。

这是界面:

[ServiceContract(Namespace="http://example.com/recordservice")]
public interface IBosleySchedulingServiceImpl
{
    [OperationContract]
    [WebInvoke(UriTemplate = "Record/Create",
        RequestFormat = WebMessageFormat.Xml, 
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
    string CreateRecord(Record record);
}

[DataContract(Namespace="http://example.com/recordservice")]
public class Appointment
{
    [DataMember]
    public int ResponseType { get; set; }

    [DataMember]
    public int ServiceType { get; set; }

    [DataMember]
    public string ContactId { get; set; }

    [DataMember]
    public string Location { get; set; }

    [DataMember]
    public string Time { get; set; }        
}

我正在传递这个 XML:

<Appointment xmlns="http://ngs.bosley.com/BosleySchedulingService">
  <ContactId>1123-123</ContactId>
  <Location>Fresno</Location>
  <Time>2012-05-05T08:30:00</Time>
  <ResponseType>45</ResponseType>
  <ServiceType>45</ServiceType>
</Appointment>

在我的服务中,我只是将值输出到日志中,这样我就可以验证值是否暂时通过:

logger.Debug("ContactId: " + appointment.ContactId);
logger.Debug("Time Field: " + appointment.Time);
logger.Debug("Location: " + appointment.Location);
logger.Debug("Response Type: " + Convert.ToInt32(appointment.ResponseType));
logger.Debug("ServiceType: " + Convert.ToInt32(appointment.ServiceType));

但是,在我的输出中,整数值显示为零:

ContactId: 1123-123
Time Field: 2012-05-05T08:30:00
Location: Fresno
Response Type: 0
ServiceType: 0

当我从 DataContract 和服务实现中删除字符串时,整数值毫无问题地通过了。

Response Type: 45
ServiceType: 45

我对此感到非常困惑,非常感谢任何帮助。

最佳答案

默认情况下,当您通过 wcf 发送对象时,除非您指定顺序,否则属性将按字母顺序发送。

您可以指定属性的顺序或更改顺序,以便它们按字母顺序显示。

[DataContract(Namespace="http://example.com/recordservice")]
public class Appointment
{
    [DataMember(Order = 1)]
    public int ResponseType { get; set; }

    [DataMember(Order = 2)]
    public int ServiceType { get; set; }

    [DataMember(Order = 3)]
    public string ContactId { get; set; }

    [DataMember(Order = 4)]
    public string Location { get; set; }

    [DataMember(Order = 5)]
    public string Time { get; set; }        
}

关于c# - 并非 WCF 数据协定中的所有参数都通过 Web 服务调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474838/

相关文章:

c# - EFCore Linq ThenInclude 两个外键到同一个表

c# - 强类型对象列表到动态列表

android - 如何使 UC 浏览器像底部栏中的设置弹出菜单?

C# 如何在 WCF 应用程序的业务逻辑类中的两个方法之间实现标志消息传递系统?

c# - CLR 中的动态 sql 连接字符串

c# - 如何使用 gstreamer-sharp 捕获视频帧

javascript - 防止 jQuery 操作动态插入的 XML 字符串

java - 启动 100 个按钮的更有效方法

c# - WCF CustomBinding 双工无法打开客户端端口

wcf - 无法远程访问 WCF 服务