不WCF数据契约的所有参数,使其通过Web服务调用 [英] Not all parameters in WCF data contract make it through the web service call

查看:112
本文介绍了不WCF数据契约的所有参数,使其通过Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个WCF REST服务,我注意到,并非所有的在我的web服务的参数都使它成为我的执行



下面的接口:



<预类=郎-CSH prettyprint-覆盖> [的ServiceContract(命名空间=htt​​p://example.com/recordservice)]
公共接口IBosleySchedulingServiceImpl
{
[OperationContract的]
[WebInvoke(UriTemplate =记录/新建,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat。 XML,
BodyStyle = WebMessageBodyStyle.Bare,方法=POST)]
串CreateRecord(录音记录);
}

[DataContract(命名空间=htt​​p://example.com/recordservice)]
公共类任命
{
[数据成员]
公众诠释的responseType {搞定;组; }

[数据成员]
公众诠释服务类型{搞定;组; }

[数据成员]
公共字符串的ContactID {搞定;组; }

[数据成员]
公共字符串位置{搞定;组; }

[数据成员]
公共字符串时间{获得;组; }
}



我通过这个XML的:



<预类=郎咸平的XML prettyprint-覆盖> <任命的xmlns =htt​​p://ngs.bosley.com/BosleySchedulingService>
<&的ContactID GT; 1123-123< /&的ContactID GT;
<地点>&弗雷斯诺LT; /地点>
<时间> 2012-05-05T08:30:00< /时间>
<&responseTyp的GT; 45℃; / responseTyp的>
<&的ServiceType GT; 45℃; /服务类型>
< /预约>

在我的服务,我只是输出值到一个日志,所以我可以验证值未来通过暂且:



<预类=郎-CSH prettyprint-覆盖> logger.Debug(的ContactID:+ appointment.ContactId );
logger.Debug(时间字段:+ appointment.Time);
logger.Debug(位置:+ appointment.Location);
logger.Debug(响应类型:+ Convert.ToInt32(appointment.ResponseType));
logger.Debug(服务类型:+ Convert.ToInt32(appointment.ServiceType));



不过,在我的输出,整数值跨越作为零来:

 的ContactID:1123-123 
时间字段:2012-05-05T08:30:00
位置:弗雷斯诺
响应类型:0
服务类型:0

当我删除从DataContract和字符串服务实现,该整数值来通过没有问题。

 响应类型:45 
服务类型:45

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


解决方案

在默认情况下,当您发送通过WCF的属性将按照字母顺序,除非你指定的顺序发送的对象。



您可以指定属性的顺序或让他们按字母顺序出现改变排序。

  [DataContract(命名空间=htt​​p://example.com/recordservice )] 
公共类任命
{
[数据成员(订单= 1)]
公众诠释的responseType {搞定;组; }

[数据成员(订单= 2)]
公众诠释服务类型{搞定;组; }

[数据成员(令= 3)
公共字符串的ContactID {搞定;组; }

[数据成员(订单= 4)]
公共字符串位置{搞定;组; }

[数据成员(订单= 5)]
公共字符串时间{获得;组; }
}


While creating a WCF Rest service, I've noticed that not all the parameters in my web service are making it into my implementation.

Here's the interface:

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

I'm passing this XML in:

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

In my service, I'm just outputting the values to a log so I can verify the values are coming through for the time being:

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

However, in my output, the integer values are coming across as zeroes:

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

When I remove the strings from the DataContract and the service implementation, the integer values come through without a problem.

Response Type: 45
ServiceType: 45

I am utterly confused by this and any help would be greatly appreciated.

解决方案

By default when you send an object through wcf the properties will be sent in Alphabetical order unless you specify the order.

You can specify the order of the properties or change the ordering so that they appear in alphabetical order.

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

这篇关于不WCF数据契约的所有参数,使其通过Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆