不要在WCF数据契约的所有参数,使其通过Web服务调用(Not all parameters in

2019-06-24 05:17发布

当创建一个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

我完全被这个困惑,任何帮助,将不胜感激。

Answer 1:

默认情况下,当您发送通过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; }        
}


文章来源: Not all parameters in WCF data contract make it through the web service call
标签: c# xml wcf rest