C# 如何使用Windows Azure缓存服务(预览)在会话中存储Linq到Sql对象
我想利用新的WindowsAzure缓存服务(预览),并能够通过以下方式使用会话成功地在缓存中存储和检索内容。不幸的是,我们正在会话中存储Linq到Sql对象,目前无法更改。尝试使用Azure缓存服务在会话中存储此类对象时,我遇到以下错误: 无法序列化类型“System.Data.Linq.EntitySet`1[cachetest.Child]”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。如果类型是一个集合,请考虑用CopyDATAcOnTractAttult.p> 我在数据上下文中使用了“单向”序列化模式,因此所有声明都已经用“DataContract”和“DataMember”属性修饰(请参见下面的代码示例) 我已经研究过如何使用自定义序列化程序,但收到了一个与最后描述的错误完全匹配的错误,其中指出 缓存的ASP.NET提供程序不支持二进制或自定义序列化类型 由于自定义序列化程序不是一个选项,否则如何使用Windows Azure缓存在会话中存储Linq到Sql对象(具有父子关系) 我已经破解了下面说明问题的代码。C# 如何使用Windows Azure缓存服务(预览)在会话中存储Linq到Sql对象,c#,linq,session,caching,azure,C#,Linq,Session,Caching,Azure,我想利用新的WindowsAzure缓存服务(预览),并能够通过以下方式使用会话成功地在缓存中存储和检索内容。不幸的是,我们正在会话中存储Linq到Sql对象,目前无法更改。尝试使用Azure缓存服务在会话中存储此类对象时,我遇到以下错误: 无法序列化类型“System.Data.Linq.EntitySet`1[cachetest.Child]”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。如果类型是一个集合,请考虑
Linq数据上下文:
<?xml version="1.0" encoding="utf-8"?><Database Class="TestModelDataContext" Serialization="Unidirectional" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
<Table Name="" Member="Parents">
<Type Name="Parent">
<Column Member="Name" Type="System.String" CanBeNull="false" />
<Column Member="ParentId" Type="System.Int32" IsPrimaryKey="true" CanBeNull="false" />
<Association Name="Parent_Child" Member="Childs" ThisKey="ParentId" OtherKey="ParentId" Type="Child" />
</Type>
</Table>
<Table Name="" Member="Childs">
<Type Name="Child">
<Column Member="Name" Type="System.String" CanBeNull="false" />
<Column Member="ChildId" Type="System.Int32" IsPrimaryKey="true" CanBeNull="false" />
<Column Member="ParentId" Type="System.Int32" CanBeNull="false" />
<Association Name="Parent_Child" Member="Parent" ThisKey="ParentId" OtherKey="ParentId" Type="Parent" IsForeignKey="true" />
</Type>
</Table>
</Database>
}Web.Config(已删除敏感数据):
经过进一步研究,我在使用Windows Azure缓存服务时无法找到在会话中存储Linq to Sql对象的方法。我遇到了与其他进程外会话机制(如基于SQL Server的会话状态)相同的问题。对我来说,最好的解决方案是使用类似于的SessionHelper对象。我将对象存储到会话中的页面加载行更改为:
SessionHelper.Set("Parent", newParent);
最后,我的问题与Windows Azure缓存没有特别的关系,但与非常相似,这比两句话和没有研究要好@gunr2171谢谢您的关注!大部分代码是由linq到sql数据上下文自动生成的,所以我用Xml替换了它,更短@希茨基,我也犯了同样的错误。你能告诉我你是否能解决这个问题吗?@Tromax我发布了我的答案,希望能有所帮助!
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/>
<section name="cacheDiagnostics" type="Microsoft.ApplicationServer.Caching.AzureCommon.DiagnosticsConfigurationSection, Microsoft.ApplicationServer.Caching.AzureCommon" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
<connectionStrings>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime/>
<sessionState mode="Custom" customProvider="AFCacheSessionStateProvider">
<providers>
<add name="AFCacheSessionStateProvider" type="Microsoft.Web.DistributedCache.DistributedCacheSessionStateStoreProvider, Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" applicationName="AFCacheSessionState"/>
</providers>
</sessionState>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<dataCacheClients>
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="[cachename].cache.windows.net"/>
<securityProperties mode="Message" sslEnabled="false">
<messageSecurity authorizationInfo="[CacheKey]"/>
</securityProperties>
</dataCacheClient>
</dataCacheClients>
</configuration>
SessionHelper.Set("Parent", newParent);