简体   繁体   English

对PetaPoco实体和WebApi问题使用[可序列化]

[英]Using [Serializable] with PetaPoco Entities and WebApi Issues

I am trying to use [Serializable] attribute with a PetaPoco Entity so it can be stored in a REDIS Cache. 我试图将[Serializable]属性与PetaPoco实体一起使用,以便可以将其存储在REDIS缓存中。

Adding the [Serializable] attribute to the PetaPoca Entity class allows it to be cached with a REDIS Caching provider but then changes the way the WebAPI serializes the Entity to JSON which breaks. 将[Serializable]属性添加到PetaPoca Entity类后,就可以使用REDIS Caching提供程序对其进行缓存,但是随后更改了WebAPI将实体序列化为JSON的方式,这会中断。

Is there a way to specify how the WebAPI serializes the object to JSON? 有没有办法指定WebAPI如何将对象序列化为JSON?

Examples below, for specifics this is within the DAL2 for DNN and using the https://github.com/davidjrh/dnn.rediscachingprovider REDIS Caching Provider for DNN. 下面的示例,具体而言,在DAL的DAL2中,并使用https://github.com/davidjrh/dnn.rediscachingprovider REDIS DNN缓存提供程序。

DNN DAL2 / PetaPoco Object: DNN DAL2 / PetaPoco对象:

[TableName("sbCurrencyRates")]
//setup the primary key for table
[PrimaryKey("CurrID", AutoIncrement = true)]
//configure caching using PetaPoco
[Cacheable("sbCurrencyRates", CacheItemPriority.Default, 20)]
//scope the objects to the ModuleId of a module on a page (or copy of a module on a page)

[Serializable]
public class CurrRateInfo
{
    public int CurrID { get; set; }
    public string CurrCode { get; set; }
    public double CurrRate { get; set; }
    public DateTime LastUpdate { get; set; }
}

WebApi Controller: WebApi控制器:

public class RatesController : DnnApiController
{
    [AllowAnonymous]
    [HttpGet]
    public HttpResponseMessage GetRates()
    {
        CurrRatesController crc = new CurrRatesController();
        return Request.CreateResponse(HttpStatusCode.OK, crc.Get());
    }        
}

Thanks in advance. 提前致谢。

Assuming you are using a fairly recent version of , then the Json.NET serializer is being used to serialize your class . 假设您使用的是新版本,那么将使用Json.NET序列化程序对您的类进行序列化 By default Json.NET ignores [Serializable] , however it is possible to force it to serialize types with this attribute in the same way that BinaryFormatter does (ie by serializing their public and private fields ) by setting IgnoreSerializableAttribute on DefaultContractResolver to false . 默认情况下,Json.NET会忽略[Serializable] ,但是可以通过将DefaultContractResolver上的IgnoreSerializableAttribute设置为false ,以与BinaryFormatter相同的方式(即通过序列化其公共字段和私有字段 )来强制它使用此属性来序列化类型。 And in fact, asp.net does just this, which globally changes how [Serializable] classes are converted to JSON -- as you are seeing. 实际上,asp.net就是这样做的,正如您所看到的那样,它可以全局更改[Serializable]类转换为JSON的方式。

To prevent this, your options include the following: 为防止这种情况,您可以选择以下选项:

  1. Set IgnoreSerializableAttribute = true globally in asp.net. 在asp.net中全局设置IgnoreSerializableAttribute = true To do this see ASP.NET Web API and [Serializable] class or Setting IgnoreSerializableAttribute Globally in Json.net . 为此,请参见ASP.NET Web API和[Serializable]类在Json.net中全局设置IgnoreSerializableAttribute

  2. Mark your class with [JsonObject(MemberSerialization = MemberSerialization.OptOut)] : [JsonObject(MemberSerialization = MemberSerialization.OptOut)]标记您的班级:

     [Serializable] [JsonObject(MemberSerialization = MemberSerialization.OptOut)] public class CurrRateInfo { } 
  3. Annotate your class with [DataContract] and [DataMember] attributes. [DataContract][DataMember]属性注释您的类。 These take precedence over [Serializable] . 这些优先于[Serializable]

     [Serializable] [DataContract] public class CurrRateInfo { [DataMember] public int CurrID { get; set; } [DataMember] public string CurrCode { get; set; } [DataMember] public double CurrRate { get; set; } [DataMember] public DateTime LastUpdate { get; set; } } 

    Do this if you don't want a direct dependency on Json.NET from your models, and don't want to change the global behavior of asp.net. 如果您不想从模型中直接依赖Json.NET,并且不想更改asp.net的全局行为,请执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM