WCF数据客户端不发送实体属性值

时间:2012-10-22 16:37:56

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

当我尝试使用

发送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的所有属性始终为null。为什么呢?

1 个答案:

答案 0 :(得分:1)

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

  1. 第一个是POST,在此期间服务器在数据库中创建记录并将文件保存在文件系统中。
  2. 第二个是MERGE,在此期间客户端对从服务器传递的ID执行更新
  3. 这就是为什么在第一次向服务器发出请求时,对象的所有属性都为空。