【问题标题】:WCF Data Client does not send entity properties valuesWCF 数据客户端不发送实体属性值
【发布时间】:2013-06-17 17:16:49
【问题描述】:

当我尝试发送 MULTIMEDIA 类型的实例时,使用

hasStream="true"

属性设置为 true,WCF 数据服务器似乎没有接收实体数据。

在客户端,我遍历对象集合并尝试将它们发送到另一个 wcf 数据服务。对“其他wcf数据服务”的引用是:

this.centralCtx

我还为每个新实体设置保存的流并初始化从源实体复制它们的所有属性:

foreach (LOCAL_TYPE localObject in localObjects)
{
  if (entityName == "MULTIMEDIA")
       {
           CentralService.ARTICOLI article = null;
           CentralService.MULTIMEDIA multimedia = new CentralService.MULTIMEDIA();
           LocalService.MULTIMEDIA lMultimedia = localObject as LocalService.MULTIMEDIA;
           multimedia.ID_MULTIMEDIA = lMultimedia.ID_MULTIMEDIA;
           multimedia.DATA_CREAZIONE = lMultimedia.DATA_CREAZIONE;
           multimedia.DATA_ULTIMA_MODIFICA = lMultimedia.DATA_ULTIMA_MODIFICA;
           multimedia.ARTICOLO_ID = lMultimedia.ARTICOLO_ID;

           this.centralCtx.TryGetEntity(
           new Uri(this.centralCtx.BaseUri + "ARTICOLI('" + multimedia.ARTICOLO_ID 
           + "')", UriKind.Absolute), out article);

           article.MULTIMEDIA.Add(multimedia);
           this.centralCtx.AddRelatedObject(article, "MULTIMEDIA", multimedia);
           DataServiceStreamResponse streamResponse = this.localCtx.GetReadStream(localObject);
           this.centralCtx.SetSaveStream(multimedia, streamResponse.Stream, 
                      true, "image/jpeg", "");
           //this.centralCtx.UpdateObject(article);
        }
        else {
          CENTRAL_TYPE cloned = DbHelper.FlatCloneFromType<LOCAL_TYPE, CENTRAL_TYPE>
                                     (localObject, centralCtx);
          this.centralCtx.AddObject(entityName, cloned);
        }
      }

      try
      {
         this.centralCtx.SaveChanges();
         Notify(progressAction, "Exported table " + entityName, null);
         successAction(this.Log);
      }
      catch (Exception ex)
      {
         Notify(progressAction, "Error exporting table " + entityName, ex);
         this.synchResult = SynchResultType.Error;
         exceptionAction(ex);
      }

这是更改拦截器代码:

[ChangeInterceptor("MULTIMEDIA")]
    public void OnChangeMultimedia(MULTIMEDIA changedObject, UpdateOperations op)
    {
        switch (op)
        {
            case UpdateOperations.Add:
                if(changedObject.ID_MULTIMEDIA == null)
                    changedObject.ID_MULTIMEDIA = Guid.NewGuid().ToString();
                changedObject.STATO_INTERNO = "TRASFERITO";
                changedObject.DATA_ULTIMA_MODIFICA = changedObject.DATA_ULTIMA_MODIFICA == null
                    ? DateTime.Now.ToLocalTime() : changedObject.DATA_ULTIMA_MODIFICA;
                this.CurrentDataSource.SaveChanges();
                break;
            default: break;
        }
    }

MULTIMEDIA 更改拦截器内服务器上 changedObject 的所有属性始终为空。为什么?

【问题讨论】:

    标签: c# entity-framework wcf-data-services odata dataservice


    【解决方案1】:

    我终于得到了答案。 发送带有属性 hasStream 的实体意味着两个请求。

    1. 第一个是 POST,在此期间服务器在数据库中创建记录并将文件保存在文件系统中。
    2. 第二个是 MERGE,在此期间客户端对从服务器传递的 ID 执行更新

    这就是为什么在对服务器的第一次请求期间,对象的所有属性都为空。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 1970-01-01
      • 2011-05-09
      • 2012-12-16
      • 1970-01-01
      相关资源
      最近更新 更多