예제 #1
0
파일: Serializer.cs 프로젝트: tapika/swupd
        /// <summary>
        /// Creates an ODataEntry for the given EntityDescriptor and fills in its ODataLib metadata.
        /// </summary>
        /// <param name="entityDescriptor">The entity descriptor.</param>
        /// <param name="serverTypeName">Name of the server type.</param>
        /// <param name="entityType">The client-side entity type.</param>
        /// <param name="clientFormat">The current client format.</param>
        /// <returns>An odata entry with its metadata filled in.</returns>
        internal static ODataEntry CreateODataEntry(EntityDescriptor entityDescriptor, string serverTypeName, ClientTypeAnnotation entityType, DataServiceClientFormat clientFormat)
        {
            ODataEntry entry = new ODataEntry();

            // If the client type name is different from the server type name, then add SerializationTypeNameAnnotation
            // which tells ODataLib to write the type name in the annotation in the payload.
            if (entityType.ElementTypeName != serverTypeName)
            {
                entry.SetAnnotation(new SerializationTypeNameAnnotation {
                    TypeName = serverTypeName
                });
            }

            // We always need to write the client type name, since this is the type name used by ODataLib
            // to resolve the entity type using EdmModel.FindSchemaElement.
            entry.TypeName = entityType.ElementTypeName;

            // Continue to send the entry's ID in update payloads in Atom for compatibility with V1-V3,
            // but for JSON-Light we do not want the extra information on the wire.
            if (clientFormat.UsingAtom && EntityStates.Modified == entityDescriptor.State)
            {
                // <id>http://host/service/entityset(key)</id>
                entry.Id = entityDescriptor.GetLatestIdentity();
            }

            if (entityDescriptor.IsMediaLinkEntry || entityType.IsMediaLinkEntry)
            {
                // Since we are already enabled EnableWcfDataServicesClientBehavior in the writer settings,
                // setting the MediaResource value will tell ODataLib to write MLE payload, irrespective of
                // what the metadata says.
                entry.MediaResource = new ODataStreamReferenceValue();
            }

            return(entry);
        }
예제 #2
0
파일: Serializer.cs 프로젝트: tapika/swupd
        /// <summary>
        /// Writes an entity reference link.
        /// </summary>
        /// <param name="binding">The link descriptor.</param>
        /// <param name="requestMessage">The request message used for writing the payload.</param>
        internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage)
#endif
        {
            using (ODataMessageWriter messageWriter = Serializer.CreateMessageWriter(requestMessage, this.requestInfo, false /*isParameterPayload*/))
            {
                EntityDescriptor targetResource = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target);

                Uri targetResourceEditLink;
                if (null != targetResource.GetLatestIdentity())
                {
                    // When we write the uri in the payload, we need to make sure that we write the edit
                    // link in the payload, since the request uri is the edit link of the parent entity.
                    // Think of a read/write service - since the uri is the target link to the parent entity
                    // its better that we write the edit link of the child entity in the payload.
                    targetResourceEditLink = targetResource.GetResourceUri(this.requestInfo.BaseUriResolver, false /*queryLink*/);
                }
                else
                {
#if DEBUG
                    Debug.Assert(isBatch, "we should be cross-referencing entities only in batch scenarios");
#endif
                    targetResourceEditLink = UriUtil.CreateUri("$" + targetResource.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative);
                }

                ODataEntityReferenceLink referenceLink = new ODataEntityReferenceLink();
                referenceLink.Url = targetResourceEditLink;
                messageWriter.WriteEntityReferenceLink(referenceLink);
            }
        }
예제 #3
0
        protected ODataRequestMessageWrapper CreateRequest(LinkDescriptor binding)
        {
            if (binding.ContentGeneratedForSave)
            {
                return(null);
            }
            EntityDescriptor entityDescriptor = this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Source);
            EntityDescriptor descriptor2      = (binding.Target != null) ? this.RequestInfo.EntityTracker.GetEntityDescriptor(binding.Target) : null;
            Uri requestUri = null;

            if (entityDescriptor.GetLatestIdentity() == null)
            {
                if (!this.IsBatch)
                {
                    binding.ContentGeneratedForSave = true;
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_LinkResourceInsertFailure, entityDescriptor.SaveError);
                }
                Uri uri = this.CreateRequestRelativeUri(binding);
                requestUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture) + "/" + CommonUtil.UriToString(uri), UriKind.Relative);
            }
            else
            {
                if ((!this.IsBatch && (descriptor2 != null)) && (descriptor2.GetLatestIdentity() == null))
                {
                    binding.ContentGeneratedForSave = true;
                    throw System.Data.Services.Client.Error.InvalidOperation(System.Data.Services.Client.Strings.Context_LinkResourceInsertFailure, descriptor2.SaveError);
                }
                requestUri = this.CreateRequestUri(entityDescriptor, binding);
            }
            ODataRequestMessageWrapper requestMessage = this.CreateRequestMessage(requestUri, GetLinkHttpMethod(binding));

            if (this.IsBatch)
            {
                requestMessage.SetHeader("Content-ID", binding.ChangeOrder.ToString(CultureInfo.InvariantCulture));
            }
            if ((EntityStates.Added == binding.State) || ((EntityStates.Modified == binding.State) && (binding.Target != null)))
            {
                requestMessage.SetHeader("Content-Type", "application/xml");
            }
            WebUtil.SetOperationVersionHeaders(requestMessage, Util.DataServiceVersion1, this.RequestInfo.MaxProtocolVersionAsVersion);
            return(requestMessage);
        }
예제 #4
0
 internal void WriteEntityReferenceLink(LinkDescriptor binding, ODataRequestMessageWrapper requestMessage)
 {
     using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
     {
         Uri resourceUri;
         EntityDescriptor entityDescriptor = this.requestInfo.EntityTracker.GetEntityDescriptor(binding.Target);
         if (entityDescriptor.GetLatestIdentity() != null)
         {
             resourceUri = entityDescriptor.GetResourceUri(this.requestInfo.BaseUriResolver, false);
         }
         else
         {
             resourceUri = Util.CreateUri("$" + entityDescriptor.ChangeOrder.ToString(CultureInfo.InvariantCulture), UriKind.Relative);
         }
         ODataEntityReferenceLink link = new ODataEntityReferenceLink {
             Url = resourceUri
         };
         writer.WriteEntityReferenceLink(link);
     }
 }
예제 #5
0
        internal void WriteEntry(EntityDescriptor entityDescriptor, IEnumerable <LinkDescriptor> relatedLinks, ODataRequestMessageWrapper requestMessage)
        {
            ClientEdmModel       model = ClientEdmModel.GetModel(this.requestInfo.MaxProtocolVersion);
            ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType()));

            using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
            {
                ODataWriter odataWriter = writer.CreateODataEntryWriter();
                ODataEntry  entry       = new ODataEntry();
                if (this.requestInfo.HasWritingEventHandlers)
                {
                    entry.SetAnnotation <WritingEntityInfo>(new WritingEntityInfo(entityDescriptor.Entity, this.requestInfo));
                }
                string serverTypeName = this.requestInfo.GetServerTypeName(entityDescriptor);
                if (clientTypeAnnotation.ElementTypeName != serverTypeName)
                {
                    SerializationTypeNameAnnotation annotation = new SerializationTypeNameAnnotation {
                        TypeName = serverTypeName
                    };
                    entry.SetAnnotation <SerializationTypeNameAnnotation>(annotation);
                }
                entry.TypeName = clientTypeAnnotation.ElementTypeName;
                if (EntityStates.Modified == entityDescriptor.State)
                {
                    entry.Id = entityDescriptor.GetLatestIdentity();
                }
                if (entityDescriptor.IsMediaLinkEntry || clientTypeAnnotation.IsMediaLinkEntry)
                {
                    entry.MediaResource = new ODataStreamReferenceValue();
                }
                odataWriter.WriteStart(entry);
                if (EntityStates.Added == entityDescriptor.State)
                {
                    this.WriteNavigationLink(entityDescriptor, relatedLinks, odataWriter);
                }
                entry.Properties = this.PopulateProperties(clientTypeAnnotation, entityDescriptor.Entity, null);
                odataWriter.WriteEnd();
            }
        }
예제 #6
0
파일: Serializer.cs 프로젝트: nickchal/pash
 internal void WriteEntry(EntityDescriptor entityDescriptor, IEnumerable<LinkDescriptor> relatedLinks, ODataRequestMessageWrapper requestMessage)
 {
     ClientEdmModel model = ClientEdmModel.GetModel(this.requestInfo.MaxProtocolVersion);
     ClientTypeAnnotation clientTypeAnnotation = model.GetClientTypeAnnotation(model.GetOrCreateEdmType(entityDescriptor.Entity.GetType()));
     using (ODataMessageWriter writer = CreateMessageWriter(requestMessage, this.requestInfo))
     {
         ODataWriter odataWriter = writer.CreateODataEntryWriter();
         ODataEntry entry = new ODataEntry();
         if (this.requestInfo.HasWritingEventHandlers)
         {
             entry.SetAnnotation<WritingEntityInfo>(new WritingEntityInfo(entityDescriptor.Entity, this.requestInfo));
         }
         string serverTypeName = this.requestInfo.GetServerTypeName(entityDescriptor);
         if (clientTypeAnnotation.ElementTypeName != serverTypeName)
         {
             SerializationTypeNameAnnotation annotation = new SerializationTypeNameAnnotation {
                 TypeName = serverTypeName
             };
             entry.SetAnnotation<SerializationTypeNameAnnotation>(annotation);
         }
         entry.TypeName = clientTypeAnnotation.ElementTypeName;
         if (EntityStates.Modified == entityDescriptor.State)
         {
             entry.Id = entityDescriptor.GetLatestIdentity();
         }
         if (entityDescriptor.IsMediaLinkEntry || clientTypeAnnotation.IsMediaLinkEntry)
         {
             entry.MediaResource = new ODataStreamReferenceValue();
         }
         odataWriter.WriteStart(entry);
         if (EntityStates.Added == entityDescriptor.State)
         {
             this.WriteNavigationLink(entityDescriptor, relatedLinks, odataWriter);
         }
         entry.Properties = this.PopulateProperties(clientTypeAnnotation, entityDescriptor.Entity, null);
         odataWriter.WriteEnd();
     }
 }