当前位置: 首页>>代码示例>>C#>>正文


C# DataServiceContext.SetSaveStream方法代码示例

本文整理汇总了C#中DataServiceContext.SetSaveStream方法的典型用法代码示例。如果您正苦于以下问题:C# DataServiceContext.SetSaveStream方法的具体用法?C# DataServiceContext.SetSaveStream怎么用?C# DataServiceContext.SetSaveStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DataServiceContext的用法示例。


在下文中一共展示了DataServiceContext.SetSaveStream方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetSaveStreamNotAllowedInBatch

        public void SetSaveStreamNotAllowedInBatch()
        {
            // Saving named stream is not supported in batch
            DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
            context.EnableAtom = true;
            Customer c;
            using (PlaybackService.OverridingPlayback.Restore())
            {
                // Populate the context with a single customer instance
                string payload = AtomParserTests.AnyEntry(
                    id: Id,
                    properties: Properties,
                    links: GetNamedStreamEditLink(MediaEditLink, contentType: MediaContentType, etag: MediaETag) + GetNamedStreamSelfLink(MediaQueryLink, MediaContentType));

                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                c = context.Execute<Customer>(new Uri("/Customers(1)", UriKind.Relative)).Single();
            }

            context.SetSaveStream(c, "Thumbnail", new MemoryStream(), true, "image/jpeg");

            TestUtil.RunCombinations((IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)), (mode) =>
            {
                try
                {
                    DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.BatchWithSingleChangeset, mode);
                    Assert.Fail("Named streams requests are not support in batch mode");
                }
                catch (NotSupportedException e)
                {
                    Assert.AreEqual(e.Message, AstoriaUnitTests.DataServicesClientResourceUtil.GetString("Context_BatchNotSupportedForNamedStreams"), "Expecting the correct error message");
                }
            });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:33,代码来源:NamedStreamTests.cs

示例2: UpdateNamedStreamOnDeletedEntity

        public void UpdateNamedStreamOnDeletedEntity()
        {
            // Calling SetSaveStream on deleted entity is not supported
            // Populate the context with a single customer instance
            string payload = AtomParserTests.AnyEntry(
                id: Id,
                properties: Properties,
                links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType));

            using (PlaybackService.OverridingPlayback.Restore())
            {
                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                context.EnableAtom = true;
                DataServiceQuery<Customer> q = (DataServiceQuery<Customer>)context.CreateQuery<Customer>("Customers").Where(c1 => c1.ID == 1);
                Customer c = ((IEnumerable<Customer>)DataServiceContextTestUtil.ExecuteQuery(context, q, QueryMode.AsyncExecute)).Single();

                context.DeleteObject(c);

                try
                {
                    context.SetSaveStream(c, "Thumbnail", new MemoryStream(), true /*closeStream*/, "image/jpeg");
                }
                catch (DataServiceClientException ex)
                {
                    Assert.AreEqual(ex.Message, DataServicesClientResourceUtil.GetString("Context_SetSaveStreamOnInvalidEntityState", EntityStates.Deleted), "Error Message not as expected");
                }
            }
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:29,代码来源:NamedStreamTests.cs

示例3: VerifyMissingLinkSaveChangesScenario

        public void VerifyMissingLinkSaveChangesScenario()
        {
            // Make sure update scenarios fail, whenever edit link is missing
            TestUtil.RunCombinations(
                (IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)),
                UnitTestsUtil.BooleanValues,
                (mode, hasSelfLink) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        context.EnableAtom = true;

                        string links = null;
                        string selfLink = null;
                        string contentType = null;
                        if (hasSelfLink)
                        {
                            selfLink = request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail";
                            contentType = MediaContentType;
                            links = GetNamedStreamSelfLink(selfLink, contentType);
                        }

                        string payload = AtomParserTests.AnyEntry(
                                    id: Id,
                                    selfLink: request.ServiceRoot.AbsoluteUri + "/selfLink/Customers(1)",
                                    editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)",
                                    properties: Properties,
                                    links: links);

                        PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                        Customer c = context.Execute<Customer>(new Uri("/Customers(1)", UriKind.Relative)).Single();
                        PlaybackService.OverridingPlayback.Value = null;

                        EntityDescriptor ed = context.Entities.Single();
                        if (hasSelfLink)
                        {
                            StreamDescriptor streamInfo = ed.StreamDescriptors.Single();
                            Assert.AreEqual(streamInfo.StreamLink.SelfLink.AbsoluteUri, selfLink, "self links must match");
                            Assert.AreEqual(streamInfo.StreamLink.EditLink, null, "edit link must be null");
                            Assert.AreEqual(streamInfo.StreamLink.ContentType, contentType, "content type must match");
                            Assert.AreEqual(streamInfo.StreamLink.ETag, null, "no etag");
                        }
                        else
                        {
                            Assert.AreEqual(0, ed.StreamDescriptors.Count, "No named streams should be present");
                        }

                        context.SetSaveStream(c, "Thumbnail", new MemoryStream(new byte[] { 0, 1, 2, 3, 4 }), true, new DataServiceRequestArgs() { ContentType = "image/jpeg" });

                        StreamDescriptor ns = ed.StreamDescriptors.Single();
                        Assert.AreEqual(ns.StreamLink.SelfLink, selfLink, "self links must match");
                        Assert.AreEqual(ns.StreamLink.EditLink, null, "edit link must be null");
                        Assert.AreEqual(ns.StreamLink.ContentType, contentType, "content type must match");
                        Assert.AreEqual(ns.StreamLink.ETag, null, "no etag");
                        Assert.AreEqual(EntityStates.Modified, ns.State, "named stream must be in modified state");

                        try
                        {
                            DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.None, mode);
                        }
                        catch (DataServiceRequestException ex)
                        {
                            Assert.AreEqual(typeof(DataServiceClientException), ex.InnerException.GetType());
                            Assert.AreEqual(DataServicesClientResourceUtil.GetString("Context_SetSaveStreamWithoutNamedStreamEditLink", "Thumbnail"), ex.InnerException.Message);
                        }

                        Assert.AreEqual(EntityStates.Modified, ns.State, "Since save changes failed, the stream should still be in modified state");
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:71,代码来源:NamedStreamTests.cs

示例4: FailureOnInsertEntity

        public void FailureOnInsertEntity()
        {
            // Make sure if the POST fails, then also named stream request also fails, since we don't have the edit link for the named stream
            TestUtil.RunCombinations(
                (IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)), UnitTestsUtil.BooleanValues,
                (mode, continueOnError) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    using (PlaybackService.InspectRequestPayload.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        PlaybackService.OverridingPlayback.Value = null;

                        Customer c = new Customer() { ID = 1, Name = "Foo" };
                        context.AddObject("Customers", c);
                        context.SetSaveStream(c, "Thumbnail", new MemoryStream(new byte[] { 0, 1, 2, 3 }), true, new DataServiceRequestArgs() { ContentType = "image/bmp" });

                        EntityDescriptor entityDescriptor = context.Entities.Single();
                        StreamDescriptor streamInfo = entityDescriptor.StreamDescriptors.Single();

                        int i = 0;
                        PlaybackService.InspectRequestPayload.Value = (message) =>
                        {
                            if (i == 1)
                            {
                                // Verify the first request was a POST request
                                Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("POST {0}/Customers", request.ServiceRoot)), "the first request must be a POST request");
                            }

                            i++;
                        };

                        try
                        {
                            DataServiceContextTestUtil.SaveChanges(context, continueOnError ? SaveChangesOptions.ContinueOnError : SaveChangesOptions.None, mode);
                            Assert.Fail("SaveChanges should always fail");
                        }
                        catch (DataServiceRequestException ex)
                        {
                            // here the server throws instream error (bad xml), so we'll always continue on error, even if it's not set..
                            Assert.AreEqual(2, ((IEnumerable<OperationResponse>)ex.Response).Count(), "we are expected 2 response for this SaveChanges operation");
                            Assert.IsTrue(ex.Response.Where<OperationResponse>(or => or.Error == null).SingleOrDefault() == null, "all responses should have errors");
                            Assert.AreEqual(streamInfo.State, EntityStates.Modified, "streamdescriptor should still be in modified state, so that next save changes can try this");
                            Assert.AreEqual(entityDescriptor.State, EntityStates.Added, "entityDescriptor is in the added state");
                        }

                        Assert.AreEqual(i, 1, "the number of requests sent to the server will only be 1, since the named stream request fails on the client side");

                        // Retrying again will always fail, since we have closed the stream.
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:52,代码来源:NamedStreamTests.cs

示例5: FailureOnModifyEntity

        public void FailureOnModifyEntity()
        {
            // Make sure that if the update fails, the state of the stream still remains modified and then next savechanges succeeds
            TestUtil.RunCombinations(
                (IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)),
                (mode) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    using (PlaybackService.InspectRequestPayload.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        context.EnableAtom = true;

                        // Populate the context with an entity
                        string payload = AtomParserTests.AnyEntry(
                                        id: Id,
                                        editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)",
                                        properties: Properties,
                                        links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                        GetNamedStreamEditLink(request.ServiceRoot.AbsoluteUri + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: MediaETag));
                        PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                        Customer c = context.Execute<Customer>(new Uri("/Customers(1)", UriKind.Relative)).Single();

                        PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, null, statusCode: System.Net.HttpStatusCode.BadRequest);
                        context.SetSaveStream(c, "Thumbnail", new MemoryStream(new byte[] { 0, 1, 2, 3 }), true, new DataServiceRequestArgs() { ContentType = "image/bmp" });

                        EntityDescriptor entityDescriptor = context.Entities.Single();
                        StreamDescriptor streamInfo = entityDescriptor.StreamDescriptors.Single();

                        try
                        {
                            DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.None, mode);
                            Assert.Fail("SaveChanges should always fail");
                        }
                        catch (DataServiceRequestException ex)
                        {
                            Assert.AreEqual(1, ((IEnumerable<OperationResponse>)ex.Response).Count(), "we are expected 2 response for this SaveChanges operation");
                            Assert.IsTrue(ex.Response.Where<OperationResponse>(or => or.Error == null).SingleOrDefault() == null, "all responses should have errors");
                            Assert.AreEqual(streamInfo.State, EntityStates.Modified, "streamdescriptor should still be in modified state, so that next save changes can try this");
                        }

                        // Retrying will always fail, since the client closes the underlying stream.
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:45,代码来源:NamedStreamTests.cs

示例6: SetSaveStreamAndUpdateEntity

        public void SetSaveStreamAndUpdateEntity()
        {
            // Making sure that setting the save stream followed by updating an entity works well
            TestUtil.RunCombinations((IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)), (mode) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    using (PlaybackService.InspectRequestPayload.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        context.EnableAtom = true;
                        context.Format.UseAtom();

                        string payload = AtomParserTests.AnyEntry(
                                        id: Id,
                                        editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)",
                                        properties: Properties,
                                        links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                        GetNamedStreamEditLink(request.ServiceRoot.AbsoluteUri + "/Customers(1)/EditLink/Thumbnail", MediaContentType));

                        PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                        Customer c = context.Execute<Customer>(new Uri("/Customers(1)", UriKind.Relative)).Single();
                        PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, null);

                        context.SetSaveStream(c, "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs() { ContentType = "image/bmp" });
                        context.UpdateObject(c);

                        int i = 0;
                        PlaybackService.InspectRequestPayload.Value = (message) =>
                        {
                            if (i == 1)
                            {
                                // Verify that the second request was a PUT request
                                Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PUT {0}/Customers(1)/EditLink/Thumbnail", request.ServiceRoot)), "the second request must be a PUT request to named stream");
                                Assert.IsTrue(!PlaybackService.LastPlayback.Contains(String.Format("If-Match", "no if-match header must be sent if the named stream does not have etag")));

                                // Verify the payload of the first request - it must have an entry element.
                                XDocument document = XDocument.Load(message);
                                Assert.IsTrue(document.Element(AstoriaUnitTests.Tests.UnitTestsUtil.AtomNamespace + "entry") != null, "must contain an entry element");
                            }

                            i++;
                        };

                        DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.ContinueOnError, mode);
                        Assert.AreEqual(i, 2, "Only 2 request should have been made");

                        // Verify the first request was a PUT request to the entity
                        Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PATCH {0}/editLink/Customers(1)", request.ServiceRoot)), "the first request must be a PUT request to the entity");
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:51,代码来源:NamedStreamTests.cs

示例7: UpdateEntityAndSetSaveStream

        public void UpdateEntityAndSetSaveStream()
        {
            // Making sure that updating an entity followed by SetSaveStream works well
            TestUtil.RunCombinations(
                 UnitTestsUtil.BooleanValues,
                 (IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)),
                 UnitTestsUtil.BooleanValues,
                 (sendResponse, mode, closeStream) =>
                 {
                     using (PlaybackService.OverridingPlayback.Restore())
                     using (PlaybackService.InspectRequestPayload.Restore())
                     {
                         DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                         context.EnableAtom = true;
                         context.Format.UseAtom();

                         string payload = AtomParserTests.AnyEntry(
                                         id: Id,
                                         editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)",
                                         serverTypeName: typeof(Customer).FullName,
                                         properties: Properties,
                                         links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                         GetNamedStreamEditLink(request.ServiceRoot.AbsoluteUri + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: MediaETag));

                         PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                         context.ResolveType = name => typeof(Customer);
                         Customer c = (Customer)context.Execute<object>(new Uri("/Customers(1)", UriKind.Relative)).Single();
                         context.UpdateObject(c);
                         MemoryStream thumbnailStream = new MemoryStream();
                         context.SetSaveStream(c, "Thumbnail", thumbnailStream, closeStream, new DataServiceRequestArgs() { ContentType = "image/bmp" });

                         string namedStreamETagInEntityResponse = null;
                         string namedStreamETagInResponseHeader = "ETagInResponseHeader";
                         int i = 0;
                         PlaybackService.InspectRequestPayload.Value = (message) =>
                         {
                             if (i == 1)
                             {
                                 // Verify the first request was a PUT request to the entity
                                 Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PATCH {0}/editLink/Customers(1)", request.ServiceRoot)), "the first request must be a PUT request to the entity");

                                 var headers = new KeyValuePair<string, string>[] { new KeyValuePair<string, string>("ETag", namedStreamETagInResponseHeader) };
                                 PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(headers, null);
                             }
                             else if (i == 0)
                             {
                                 string updatePayload = null;
                                 if (sendResponse)
                                 {
                                     namedStreamETagInEntityResponse = "ETagInResponsePayload";
                                     updatePayload = AtomParserTests.AnyEntry(
                                        id: Id,
                                        editLink: request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)",
                                        properties: Properties,
                                        links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                        GetNamedStreamEditLink(request.ServiceRoot.AbsoluteUri + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: namedStreamETagInEntityResponse));
                                 }
                                 PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, updatePayload);

                                 // Verify the payload of the first request - it must have an entry element.
                                 XDocument document = XDocument.Load(message);
                                 Assert.IsTrue(document.Element(AstoriaUnitTests.Tests.UnitTestsUtil.AtomNamespace + "entry") != null, "must contain an entry element");
                             }

                             i++;
                             Assert.IsTrue(thumbnailStream.CanWrite, "The thumbnail stream hasn't been closed yet");
                         };

                         DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.None, mode);
                         Assert.AreEqual(i, 2, "Only 2 request should have been made");

                         // Verify that the second request was a PUT request
                         Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PUT {0}/Customers(1)/EditLink/Thumbnail", request.ServiceRoot)), "the second request must be a PUT request to named stream");

                         // If the first update sent a response, then the new etag must be used
                         Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("If-Match: {0}", namedStreamETagInEntityResponse ?? MediaETag)), "etag must be sent in the PUT request to the named stream");

                         Assert.AreEqual(!thumbnailStream.CanWrite, closeStream, "The thumbnail stream must be desired state after SaveChanges returns");

                         StreamDescriptor sd = context.Entities[0].StreamDescriptors[0];

                         // Verify that the etag from the response overrides the etag from the header when the response is present
                         Assert.AreEqual(sd.StreamLink.ETag, namedStreamETagInResponseHeader, "make sure the etag was updated to the latest etag");
                     }
                 });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:86,代码来源:NamedStreamTests.cs

示例8: InsertMLEAndSetSaveStream

        public void InsertMLEAndSetSaveStream()
        {
            // Making sure that inserting an MLE/MR followed by SetSaveStream works well
            TestUtil.RunCombinations(
                (IEnumerable<SaveChangesMode>)Enum.GetValues(typeof(SaveChangesMode)),
                UnitTestsUtil.BooleanValues,
                (mode, closeStream) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    using (PlaybackService.InspectRequestPayload.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        context.EnableAtom = true;
                        context.Format.UseAtom();

                        ClientCSharpRegressionTests.CustomerWithStream c = new ClientCSharpRegressionTests.CustomerWithStream() { ID = 1, Name = "Foo" };
                        context.AddObject("Customers", c);
                        MemoryStream defaultStream = new MemoryStream(new byte[] { 0, 1, 2, 3 });
                        MemoryStream thumbnailStream = new MemoryStream(new byte[] { 0, 1, 2, 3, 4 });
                        context.SetSaveStream(c, defaultStream, closeStream, new DataServiceRequestArgs() { ContentType = "image/bmp" });
                        context.SetSaveStream(c, "Thumbnail", thumbnailStream, closeStream, new DataServiceRequestArgs() { ContentType = "image/bmp" });

                        // verify the entity descriptor state
                        EntityDescriptor entityDescriptor = context.Entities.Single();
                        StreamDescriptor streamInfo = entityDescriptor.StreamDescriptors.Single();
                        Assert.IsTrue(entityDescriptor.State == EntityStates.Added, "entity must be in added state");
                        Assert.AreEqual(streamInfo.StreamLink.Name, "Thumbnail");
                        Assert.IsTrue(streamInfo.State == EntityStates.Modified, "named stream must be in modified state");

                        string editLink = request.ServiceRoot.AbsoluteUri + "/editLink/Customers(1)";
                        int i = 0;
                        PlaybackService.InspectRequestPayload.Value = (message) =>
                        {
                            if (i == 2)
                            {
                                // Verify that the second request was a PUT request
                                Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PATCH {0}", editLink)), "the second request must be a PATCH request with the edit link");
                                Assert.AreEqual(!defaultStream.CanWrite, closeStream, "The default stream must have been in the desired state");
                            }
                            else if (i == 1)
                            {
                                // Verify the first request was a POST request
                                Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("POST {0}/Customers", request.ServiceRoot)), "the first request must be a POST request");
                                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, null);
                                XDocument document = XDocument.Load(message);
                                Assert.IsTrue(document.Element(AstoriaUnitTests.Tests.UnitTestsUtil.AtomNamespace + "entry") != null, "must contain an entry element");
                                Assert.AreEqual(!defaultStream.CanWrite, closeStream, "The default stream must have been in the desired state immd after the request");
                            }
                            else if (i == 0)
                            {
                                // Populate the context with a single customer instance
                                string payload = AtomParserTests.MediaEntry(
                                    id: Id,
                                    editLink: editLink,
                                    properties: Properties,
                                    readStreamUrl: request.ServiceRoot + "/Customers(1)/readStreamUrl/$value",
                                    links: GetNamedStreamSelfLink(request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                           GetNamedStreamEditLink(request.ServiceRoot.AbsoluteUri + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: MediaETag));

                                var headers = new List<KeyValuePair<string, string>>() {
                                new KeyValuePair<string, string>("Location", "http://locationservice/locationheader") };
                                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(headers, payload);

                                // entity state should not be modified until all the responses have been changed
                                Assert.IsTrue(entityDescriptor.State == EntityStates.Modified, "entity must be in added state");
                                Assert.IsTrue(defaultStream.CanWrite, "The default stream hasn't been closed yet");
                            }

                            Assert.AreEqual(streamInfo.StreamLink.Name, "Thumbnail");
                            Assert.IsTrue(streamInfo.State == EntityStates.Modified, "named stream must be in modified state");

                            // Also the stream info links should not be modified
                            Assert.IsTrue(streamInfo.StreamLink.SelfLink == null, "descriptor should not have been modified yet - self link must be null");
                            Assert.IsTrue(streamInfo.StreamLink.EditLink == null, "descriptor should not have been modified yet - edit link must be null");
                            Assert.IsTrue(String.IsNullOrEmpty(streamInfo.StreamLink.ContentType), "descriptor should not have been modified yet - content type must be null");
                            Assert.IsTrue(String.IsNullOrEmpty(streamInfo.StreamLink.ETag), "descriptor should not have been modified yet - etag must be null");
                            Assert.IsTrue(thumbnailStream.CanWrite, "The thumbnail stream hasn't been closed yet");
                            i++;

                        };

                        DataServiceContextTestUtil.SaveChanges(context, SaveChangesOptions.None, mode);
                        Assert.AreEqual(i, 3, "Only 2 request should have been made");

                        // Verify that the second request was a PUT request
                        Assert.IsTrue(PlaybackService.LastPlayback.Contains(String.Format("PUT {0}", streamInfo.StreamLink.EditLink)), "the second request must be a PUT request with the edit link");
                        Assert.AreEqual(streamInfo.StreamLink.SelfLink.AbsoluteUri, request.ServiceRoot + "/Customers(1)/SelfLink/Thumbnail", "self link must be null, since the payload did not have self link");
                        Assert.AreEqual(streamInfo.StreamLink.EditLink.AbsoluteUri, request.ServiceRoot + "/Customers(1)/EditLink/Thumbnail", "edit link should have been populated");
                        Assert.AreEqual(!thumbnailStream.CanWrite, closeStream, "The stream must be in the desired state");
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:92,代码来源:NamedStreamTests.cs

示例9: NamedStreams_TestPublicAPIParameters

        public void NamedStreams_TestPublicAPIParameters()
        {
            // Test public api parameters
            DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);

            #region GetReadStreamUri API
            DataServiceContextTestUtil.CheckArgumentNull("entity", () =>
            {
                context.GetReadStreamUri(null, "Thumbnail");
            });

            DataServiceContextTestUtil.CheckArgumentNull("name", () =>
            {
                context.GetReadStreamUri(new Customer(), null);
            });

            DataServiceContextTestUtil.CheckArgumentEmpty("name", () =>
            {
                context.GetReadStreamUri(new Customer(), "");
            });

            DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () =>
            {
                context.GetReadStreamUri(new Customer(), "Thumbnail");
            });
            #endregion GetReadStreamUri API

            #region BeginGetReadStream
            DataServiceContextTestUtil.CheckArgumentNull("entity", () =>
            {
                context.BeginGetReadStream(null, "Thumbnail", new DataServiceRequestArgs(), null, null);
            });

            DataServiceContextTestUtil.CheckArgumentNull("name", () =>
            {
                context.BeginGetReadStream(new Customer(), null, new DataServiceRequestArgs(), null, null);
            });

            DataServiceContextTestUtil.CheckArgumentEmpty("name", () =>
            {
                context.BeginGetReadStream(new Customer(), "", new DataServiceRequestArgs(), null, null);
            });

            DataServiceContextTestUtil.CheckArgumentNull("args", () =>
            {
                context.BeginGetReadStream(new Customer(), "Thumbnail", null, null, null);
            });

            DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () =>
            {
                context.BeginGetReadStream(new Customer(), "Thumbnail", new DataServiceRequestArgs(), null, null);
            });
            #endregion BeginGetReadStream

            #region GetReadStream
            DataServiceContextTestUtil.CheckArgumentNull("entity", () =>
            {
                context.GetReadStream(null, "Thumbnail", new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentNull("name", () =>
            {
                context.GetReadStream(new Customer(), null, new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentEmpty("name", () =>
            {
                context.GetReadStream(new Customer(), "", new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentNull("args", () =>
            {
                context.GetReadStream(new Customer(), "Thumbnail", null);
            });

            DataServiceContextTestUtil.VerifyInvalidRequest(typeof(InvalidOperationException), "Context_EntityNotContained", () =>
            {
                context.GetReadStream(new Customer(), "Thumbnail", new DataServiceRequestArgs());
            });
            #endregion GetReadStream

            #region SetSaveStream
            DataServiceContextTestUtil.CheckArgumentNull("entity", () =>
            {
                context.SetSaveStream(null, "Thumbnail", new MemoryStream(), true, new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentNull("name", () =>
            {
                context.SetSaveStream(new Customer(), null, new MemoryStream(), true, new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentEmpty("name", () =>
            {
                context.SetSaveStream(new Customer(), "", new MemoryStream(), true, new DataServiceRequestArgs());
            });

            DataServiceContextTestUtil.CheckArgumentNull("stream", () =>
            {
                context.SetSaveStream(new Customer(), "Thumbnail", null, true, new DataServiceRequestArgs());
//.........这里部分代码省略.........
开发者ID:larsenjo,项目名称:odata.net,代码行数:101,代码来源:NamedStreamTests.cs

示例10: Collection_Blobs

            public void Collection_Blobs()
            {
                DSPMetadata metadata = CreateMetadataForXFeatureEntity(true);

                DSPServiceDefinition service = new DSPServiceDefinition() { 
                    Metadata = metadata, 
                    Writable = true, 
                    SupportMediaResource = true,
                    MediaResourceStorage = new DSPMediaResourceStorage()
                };

                byte[] clientBlob = new byte[] { 0xcc, 0x10, 0x00, 0xff };

                DSPContext data = new DSPContext();
                service.CreateDataSource = (m) => { return data; };

                using (TestWebRequest request = service.CreateForInProcessWcf())
                using (DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Restore())
                {
                    DSPResourceWithCollectionProperty.CollectionPropertiesResettable.Value = true;
                    request.StartService();

                    XFeatureTestsMLE clientMle = new XFeatureTestsMLE() {
                        ID = 1,
                        Description = "Entity 1",
                        Strings = new List<string>(new string[] { "string 1", "string 2", string.Empty }),
                        Structs = new List<XFeatureTestsComplexType>(new XFeatureTestsComplexType[] {
                                    new XFeatureTestsComplexType() { Text = "text 1" },
                                    new XFeatureTestsComplexType() { Text = "text 2" }}) };


                    DataServiceContext ctx = new DataServiceContext(new Uri(request.BaseUri), ODataProtocolVersion.V4);
                    ctx.EnableAtom = true;
                    ctx.Format.UseAtom();

                    ctx.AddObject("Entities", clientMle);
                    ctx.SetSaveStream(clientMle, new MemoryStream(clientBlob), true, "application/octet-stream", clientMle.ID.ToString());
                    ctx.SaveChanges();
                    VerifyMLEs(service, clientMle, clientBlob);

                    // Read stream and verify stream contents
                    using (Stream serverStream = ctx.GetReadStream(clientMle).Stream)
                    {
                        VerifyStream(clientBlob, serverStream);
                    }

                    // modify MLE and the corresponding stream 
                    clientMle.Structs.Add(new XFeatureTestsComplexType() { Text = "text 3" });
                    clientMle.Strings.RemoveAt(0);
                    clientBlob[0] ^= 0xff;
                    ctx.UpdateObject(clientMle);
                    ctx.SetSaveStream(clientMle, new MemoryStream(clientBlob), true, "application/octet-stream", clientMle.ID.ToString());
                    ctx.SaveChanges();
                    VerifyMLEs(service, clientMle, clientBlob);

                    // delete MLE
                    ctx.DeleteObject(clientMle);
                    ctx.SaveChanges();

                    Assert.IsNull((DSPResource)service.CurrentDataSource.GetResourceSetEntities("Entities").
                            FirstOrDefault(e => (int)(((DSPResource)e).GetValue("ID")) == (int)clientMle.GetType().GetProperty("ID").GetValue(clientMle, null)),
                            "MLE has not been deleted.");

                    Assert.AreEqual(0, service.MediaResourceStorage.Content.Count(), "The stream on the server has not been deleted.");
                };
            }
开发者ID:AlineGuan,项目名称:odata.net,代码行数:66,代码来源:CollectionCrossFeature.cs


注:本文中的DataServiceContext.SetSaveStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。