WCF &JSON:将长序列化为字符串 [英] WCF & JSON: Serialize long as string

查看:35
本文介绍了WCF &JSON:将长序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 wcf 数据服务和一个正常的休息 wcf 服务.两种服务都返回相同的实体对象.

I have a wcf data service and a normal rest wcf service. Both services return the same entity object.

[DataContract]
public partial class MyEntity
{
    #region Primitive Properties
    [DataMember]
    public virtual long ID
    {
        get;
        set;
    }
    ....

正常的 rest wcf 服务使用以下服务契约:

The normal rest wcf service uses the following service contract:

[ServiceContract]
public interface MyService
{
    [OperationContract]
    [WebGet(UriTemplate="MyEntity/{id}",ResponseFormat=WebMessageFormat.Json)]
    public MyEntity GetMyEntity(string id)
}

而 wcf 数据服务将长值作为字符串返回:

while the wcf data service returns the long value as string:

{{"id": ... ,"uri": ... ,"type":"Model.MyEntity"},"ID":"865176660053852161"}

MyService ServiceContract 返回数字:

the MyService ServiceContract returns the long as number:

{ "ID":865176660053852161 }

似乎 wcf 数据服务和普通"休息服务使用两种不同的序列化机制.

Seems like the wcf data service and the "normal" rest service use two different kind of serialization mechanisms.

问题是:我的客户端应用程序使用 JavaScript,因此无法处理 64 位数字.我原以为,正常"的休息服务也会以字符串形式返回 64 位数字.

Problem is: My client app uses JavaScript and can therefore not handle 64bit numbers. I would have expected, that the "normal" rest service also would return 64bit numbers as string.

  • 在哪里可以将序列化/反序列化过程中的数字转换为字符串?
  • 万一有人知道:为什么 wcf 数据之间的行为不同基于服务/休息的 wcf?

为了一致性,我更喜欢在服务器端的序列化过程中进行转换,但我不知道这是否可行.

For consistency I would prefer a conversion during the serialization process on the serverside, but I don't know if this is feasible.

推荐答案

我现在的解决方法是为 poco 生成调整 t4 模板,以便像这样生成实体对象:

My workaround now is to adapt the t4 template for the poco generation, so that the entity objects are genereated like this:

[DataContract]
public partial class MyEntity
{
    #region Primitive Properties

    public virtual long ID
    {
        get { return _iD; }
        set { _iD = value; _iDStr = value.ToString(); }
    }

    [DataMember(Name="ID")]
    private string _iDStr;
    private long _iD;

    ...

这将在 WCF 响应中以字符串形式返回 ID,而实体框架仍然使用长值....

This will return the ID as string in the WCF response, while the entity framework still works with the long value....

这篇关于WCF &JSON:将长序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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