【问题标题】:TableServiceContext: Can't cast to unsupported type 'DateTimeOffset' exception when serializingTableServiceContext:序列化时无法转换为不支持的类型“DateTimeOffset”异常
【发布时间】:2013-02-01 23:59:05
【问题描述】:

我一直在尝试为 Azure 设置新的 MVC Web 角色,连接到 Azure 表服务。我在 VS 2012 中使用了 1.8 Azure SDK 和 Azure 项目模板。我将所有 nuget 包更新到最新版本,但我仍然怀疑 WCF 对 DateTimeOffset 的支持存在问题,这是版本 5 中的新功能。

我有一个像这样的简单对象:

[DataServiceEntity()]
[DataServiceKey("PartitionKey", "RowKey")]
public class AppUser : TableEntity
{
}

我有一个 MVC 操作来创建一个新的 AppUser,如下所示:

var connectionString = CloudConfigurationManager.GetSetting("DataStorageConnectionString");
var storageAccount = CloudStorageAccount.Parse(connectionString);
var tableClient = storageAccount.CreateCloudTableClient();
var ctx = new TableServiceContext(tableClient);

ctx.AddObject("Users", user);
var response = ctx.SaveChanges();

当此代码在我的 MVC Web 角色中执行时,我的 connectString 的结果是 UseDevelopmentStorage=true。

SaveChanges 调用导致以下异常:

System.NotSupportedException was caught
  HResult=-2146233067
  Message=Can't cast to unsupported type 'DateTimeOffset'
  Source=System.Data.Services.Client
  StackTrace:
       at System.Data.Services.Client.ClientConvert.GetEdmType(Type propertyType)
       at System.Data.Services.Client.DataServiceContext.WriteContentProperty(XmlWriter writer, String namespaceName, ClientProperty property, Object propertyValue)
       at System.Data.Services.Client.DataServiceContext.WriteContentProperties(XmlWriter writer, ClientType type, Object resource, EpmSourcePathSegment currentSegment, Boolean& propertiesWritten)
       at System.Data.Services.Client.DataServiceContext.CreateRequestData(EntityDescriptor box, Boolean newline)
       at System.Data.Services.Client.DataServiceContext.SaveResult.CreateChangeData(Int32 index, Boolean newline)
       at System.Data.Services.Client.DataServiceContext.SaveResult.BeginNextChange(Boolean replaceOnUpdate)
       at System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions options)
       at MvcWebRole1.Controllers.UserController.Create(AppUser user) in c:\src\Azure1\MvcWebRole1\Controllers\UserController.cs:line 67
  InnerException: 

查看 TableEntity 时,Timestamp 属性的类型为 DateTimeOffset。我一直在调查我的构建中的依赖项之一是旧的并导致问题的想法,但我无法识别任何旧版本。据我所知,我正在使用最新版本的SDK,最新版本的项目模板,并且更新了所有的包,所以我现在有点不知所措。

** 12/26 更新**

我创建了一个新的控制台应用程序,仅使用存储客户端进行测试,并且能够完美地复制该行为。 Azure 存储客户端 nuget 包仅在一个多星期前发布,可能还没有打算出来。我无法让它与开发存储或实时 Azure 一起使用,但从 10 月和 6 月分别恢复到 1.8 和 1.7 SDK 附带的 1.7 程序集需要一些重写,但它确实适用于两个开发存储和 Azure。

【问题讨论】:

    标签: c# azure azure-table-storage


    【解决方案1】:

    在我看来,您正在混合访问方法。 TableServiceContext 来自 1.x 存储 API,而 TableEntity 来自 2.0 存储 API。

    如果您想使用 1.x 库,请从 TableServiceEntity 而不是 TableEntity 派生,并且不要使用这些属性。 如果要使用 2.0 库,请使用 CloudTable 和 TableOperation 而不是 TableServiceContext。

    【讨论】:

      【解决方案2】:

      您是否尝试过不从 TableEntity 派生,而只是将自己的 PartitionKey/RowKey 属性实现为字符串,将时间戳实现为 DateTime?

      另外,请尝试针对真实的 Azure 存储帐户进行测试。在过去的几年里,我发现模拟存储帐户对于认真的 Azure 存储开发来说并不是一个有用的测试平台。

      【讨论】:

      • 我尝试了这两个建议,但它们并没有真正起作用。生产系统给了我同样的问题,尽管在发送请求之前,问题似乎在序列化过程中就显现出来了。这再次让我回到依赖关系的一些问题。
      • 抱歉,还没完成。我不能从 TableEntity 继承,但 API 要求它或 ITableEntity 仍然要求 Timestamp 属性为 DateTimeOffset 类型。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多