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


C# DataServiceContext.ExecuteBatch方法代码示例

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


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

示例1: NamedStreams_Projections_MergeInfoOptions

        public void NamedStreams_Projections_MergeInfoOptions()
        {
            // Make sure that based on the MergeOption, the value of the DataServiceStreamLink is updated
            TestUtil.RunCombinations(
                    new EntityStates[] { EntityStates.Added, EntityStates.Deleted, EntityStates.Modified, EntityStates.Unchanged },
                    new MergeOption[] { MergeOption.AppendOnly, MergeOption.OverwriteChanges, MergeOption.PreserveChanges },
                    new int[] { -1, 0, 1 }, // -1 indicates less links, 0 means exact same number of links, 1 means some extra links
                    UnitTestsUtil.BooleanValues,
                    (entityState, mergeOption, extraLinks, useBatchMode) =>
                    {
                        using (PlaybackService.OverridingPlayback.Restore())
                        {
                            DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                            context.EnableAtom = true;
                            context.Format.UseAtom();
                            context.MergeOption = mergeOption;

                            // Populate the context with a single customer instance with 2 named streams
                            string originalServiceRoot = "http://randomservice/Foo.svc";
                            string newServiceRoot = "http://randomservice1/Foo1.svc";

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

                            PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                            DataServiceQuery<StreamType2> query = (DataServiceQuery<StreamType2>)context.CreateQuery<StreamType2>("Customers");

                            StreamType2 c = DataServiceContextTestUtil.CreateEntity<StreamType2>(context, "Customers", entityState, query);
                            PlaybackService.OverridingPlayback.Value = null;

                            string linksPayload = null;

                            if (extraLinks == -1)
                            {
                                // send no links
                            }
                            else if (extraLinks == 0)
                            {
                                linksPayload = NamedStreamTests.GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Thumbnail");
                            }
                            else
                            {
                                linksPayload = NamedStreamTests.GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Thumbnail") +
                                               NamedStreamTests.GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Photo", name: "Photo");
                            }

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

                            PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);

                            if (useBatchMode)
                            {
                                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToBatchQueryResponsePayload(PlaybackService.OverridingPlayback.Value);
                                QueryOperationResponse<StreamType2> resp = (QueryOperationResponse<StreamType2>)context.ExecuteBatch((DataServiceRequest)query).Single();
                                c = resp.First();
                            }
                            else
                            {
                                switch (extraLinks)
                                {
                                    case -1:
                                        c = query.Select(s => new StreamType2()
                                        {
                                            ID = s.ID,
                                            Name = s.Name
                                        }).Single();
                                        break;
                                    case 0:
                                        c = query.Select(s => new StreamType2()
                                        {
                                            ID = s.ID,
                                            Name = s.Name,
                                            Thumbnail = s.Thumbnail
                                        }).Single();
                                        break;
                                    default:
                                        c = query.Select(s => new StreamType2()
                                        {
                                            ID = s.ID,
                                            Name = s.Name,
                                            Thumbnail = s.Thumbnail,
                                            Photo = s.Photo
                                        }).Single();
                                        break;
                                }
                            }

                            EntityDescriptor entityDescriptor = context.Entities.Where(ed => Object.ReferenceEquals(c, ed.Entity)).Single();
                            StreamDescriptor thumbnail = entityDescriptor.StreamDescriptors.Where(ns => ns.StreamLink.Name == "Thumbnail").SingleOrDefault();
                            //Assert.IsTrue(thumbnail == null || object.ReferenceEquals(thumbnail.EntityDescriptor, entityDescriptor), "StreamDescriptor.EntityDescriptor should point to the same instance of entity descriptor");
                            StreamDescriptor photo = entityDescriptor.StreamDescriptors.Where(ns => ns.StreamLink.Name == "Photo").SingleOrDefault();
                            //Assert.IsTrue(photo == null || object.ReferenceEquals(photo.EntityDescriptor, entityDescriptor), "StreamDescriptor.EntityDescriptor should point to the same instance of entity descriptor for photo");

//.........这里部分代码省略.........
开发者ID:larsenjo,项目名称:odata.net,代码行数:101,代码来源:NamedStreamTests.cs

示例2: NamedStreams_PropertyOnClientNotDataServiceStreamLink

        public void NamedStreams_PropertyOnClientNotDataServiceStreamLink()
        {
            // [Client-ODataLib-Integration] Astoria client does not fail if the client and server stream property does not match
            TestUtil.RunCombinations(
                    new MergeOption[] { MergeOption.AppendOnly, MergeOption.OverwriteChanges, MergeOption.PreserveChanges },
                    UnitTestsUtil.BooleanValues,
                    (mergeOption, useBatchMode) =>
                    {
                        using (PlaybackService.OverridingPlayback.Restore())
                        {
                            DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                            context.EnableAtom = true;
                            context.MergeOption = mergeOption;

                            // Populate the context with a single customer instance with 2 named streams
                            string originalServiceRoot = "http://randomservice/Foo.svc";

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

                            PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                            DataServiceQuery<StreamTypeWithMismatchedStreamProperty> query = (DataServiceQuery<StreamTypeWithMismatchedStreamProperty>)context.CreateQuery<StreamTypeWithMismatchedStreamProperty>("Customers");

                            Exception exception = null;
                            if (useBatchMode)
                            {
                                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToBatchQueryResponsePayload(PlaybackService.OverridingPlayback.Value);
                                var batchResponse = context.ExecuteBatch(query);
                                foreach (var queryResponse in batchResponse.OfType<QueryOperationResponse<StreamTypeWithMismatchedStreamProperty>>())
                                {
                                    exception = TestUtil.RunCatching<InvalidOperationException>(() =>
                                    {
                                        foreach (var entity in queryResponse)
                                        {
                                            Assert.Fail("Should not reach here");
                                        }
                                    });
                                }
                            }
                            else
                            {
                                exception = TestUtil.RunCatching<InvalidOperationException>(() => query.ToList());
                            }

                            Assert.IsNotNull(exception, "Expected error on mismatched client named stream link property type");
                            Assert.IsInstanceOfType(exception, typeof(InvalidOperationException), "Expected InvalidOperationException on mismatched client named stream link property type");
                            string expectedErrorMessage = ODataLibResourceUtil.GetString("ValidationUtils_MismatchPropertyKindForStreamProperty", "Thumbnail");
                            Assert.AreEqual(expectedErrorMessage, exception.Message);
                        }
                    });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:54,代码来源:NamedStreamTests.cs

示例3: VerifyEntityDescriptorMergeFunctionality

        public void VerifyEntityDescriptorMergeFunctionality()
        {
            // Make sure that based on the MergeOption, the right links are exposed in the entity descriptor
            TestUtil.RunCombinations(
                    new EntityStates[] { EntityStates.Added, EntityStates.Deleted, EntityStates.Modified, EntityStates.Unchanged },
                    new MergeOption[] { MergeOption.AppendOnly, MergeOption.OverwriteChanges, MergeOption.PreserveChanges },
                    new int[] { -1, 0, 1 }, // -1 indicates less links, 0 means exact same number of links, 1 means some extra links
                    UnitTestsUtil.BooleanValues,
                    UnitTestsUtil.BooleanValues,
                    (entityState, mergeOption, extraLinks, useBatchMode, returnETag) =>
                    {
                        using (PlaybackService.OverridingPlayback.Restore())
                        {
                            DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                            context.EnableAtom = true;
                            context.MergeOption = mergeOption;

                            // Populate the context with a single customer instance with 2 named streams
                            string originalServiceRoot = "http://randomservice/Foo.svc";
                            string newServiceRoot = "http://randomservice1/Foo1.svc";

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

                            PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);
                            Customer c = DataServiceContextTestUtil.CreateEntity<Customer>(context, "Customers", entityState);
                            PlaybackService.OverridingPlayback.Value = null;

                            string linksPayload = null;
                            string newETag = returnETag ? MediaETag + 1 : null;

                            if (extraLinks == -1)
                            {
                                linksPayload = GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: newETag);
                            }
                            else if (extraLinks == 0)
                            {
                                linksPayload = GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: newETag) +
                                               GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/Photo", contentType: MediaContentType, name: "Photo") +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Photo", contentType: MediaContentType, name: "Photo", etag: newETag);
                            }
                            else
                            {
                                linksPayload = GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/Thumbnail", contentType: MediaContentType) +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Thumbnail", contentType: MediaContentType, etag: newETag) +
                                               GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/Photo", contentType: MediaContentType, name: "Photo") +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/Photo", contentType: MediaContentType, etag: newETag, name: "Photo") +
                                               GetNamedStreamSelfLink(newServiceRoot + "/Customers(1)/SelfLink/HighResolutionPhoto", contentType: MediaContentType, name: "HighResolutionPhoto") +
                                               GetNamedStreamEditLink(newServiceRoot + "/Customers(1)/EditLink/HighResolutionPhoto", contentType: MediaContentType, name: "HighResolutionPhoto", etag: newETag);
                            }

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

                            PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToPlaybackServicePayload(null, payload);

                            if (useBatchMode)
                            {
                                PlaybackService.OverridingPlayback.Value = PlaybackService.ConvertToBatchQueryResponsePayload(PlaybackService.OverridingPlayback.Value);
                                QueryOperationResponse<Customer> resp = (QueryOperationResponse<Customer>)context.ExecuteBatch(context.CreateQuery<Customer>("/Customers(123)")).Single();
                                c = resp.First();
                            }
                            else
                            {
                                c = context.CreateQuery<Customer>("/Customers(123)").Execute().First();
                            }

                            EntityDescriptor entityDescriptor = context.Entities.Where(ed => Object.ReferenceEquals(c, ed.Entity)).Single();
                            StreamDescriptor thumbnail = entityDescriptor.StreamDescriptors.Where(ns => ns.StreamLink.Name == "Thumbnail").SingleOrDefault();
                            StreamDescriptor photo = entityDescriptor.StreamDescriptors.Where(ns => ns.StreamLink.Name == "Photo").SingleOrDefault();
                            StreamDescriptor highResPhoto = entityDescriptor.StreamDescriptors.Where(ns => ns.StreamLink.Name == "HighResolutionPhoto").SingleOrDefault();

                            string newSelfLink = newServiceRoot + "/Customers(1)/SelfLink/{0}";
                            string newEditLink = newServiceRoot + "/Customers(1)/EditLink/{0}";
                            string existingSelfLink = originalServiceRoot + "/Customers(1)/SelfLink/{0}";
                            string existingEditLink = originalServiceRoot + "/Customers(1)/EditLink/{0}";

                            if (entityState == EntityStates.Added)
                            {
                                Assert.AreEqual(2, context.Entities.Count, "since its in added state, we will get a new entity descriptor");
                                Assert.AreEqual(2 + extraLinks, entityDescriptor.StreamDescriptors.Count, "number of named streams was not as expected");

                                VerifyNamedStreamInfo(thumbnail, newSelfLink, newEditLink, newETag, contentType: MediaContentType);
                                if (extraLinks == -1)
                                {
                                    Assert.AreEqual(photo, null, "photo must be null when extra links = -1 and state = added");
                                    Assert.AreEqual(highResPhoto, null, "highResPhoto must be null when extra links = -1 and state = added");
                                }
                                else if (extraLinks == 0)
//.........这里部分代码省略.........
开发者ID:larsenjo,项目名称:odata.net,代码行数:101,代码来源:NamedStreamTests.cs

示例4: BatchContentTypeTest


//.........这里部分代码省略.........
                    // Valid simple boundary
                    new BatchContentTypeTestCase
                    {
                        ContentType = "multipart/mixed;boundary=batchboundary",
                        PayloadBatchBoundary = "batchboundary"
                    },
                    // Valid simple boundary - mimetype using different casing
                    new BatchContentTypeTestCase
                    {
                        ContentType = "MultiPart/mIxed;boundary=batchboundary",
                        PayloadBatchBoundary = "batchboundary"
                    },
                    // Valid simple boundary - boundary parameter name different casing
                    new BatchContentTypeTestCase
                    {
                        ContentType = "multipart/mixed;BounDary=batchboundary",
                        PayloadBatchBoundary = "batchboundary"
                    },
                };

                OpenWebDataServiceDefinition serverService = new OpenWebDataServiceDefinition()
                {
                    DataServiceType = typeof(CustomDataContext)
                };

                PlaybackServiceDefinition clientService = new PlaybackServiceDefinition();

                TestUtil.RunCombinations(
                    testCases,
                    (testCase) =>
                    {
                        using (TestWebRequest request = serverService.CreateForInProcess())
                        {
                            request.RequestContentType = testCase.ContentType;
                            request.SetRequestStreamAsText(string.Format(
                                "--{0}\r\n" +
                                "Content-Type: multipart/mixed; boundary=changesetresponse_00000001-0000-0000-0000-000000000000\r\n\r\n" +
                                "--changesetresponse_00000001-0000-0000-0000-000000000000\r\n" +
                                "--changesetresponse_00000001-0000-0000-0000-000000000000--\r\n" +
                                "--{0}--\r\n", testCase.PayloadBatchBoundary));
                            request.RequestUriString = "/$batch";
                            request.HttpMethod = "POST";

                            Exception exception = TestUtil.RunCatching(request.SendRequest);

                            int actualStatusCode = 0;
                            if (exception != null)
                            {
                                actualStatusCode = request.ResponseStatusCode;
                            }
                            else
                            {
                                Assert.AreEqual(202, request.ResponseStatusCode, "Wrong response code for no-exception request.");
                                BatchWebRequest batchResponse = BatchWebRequest.FromResponse(InMemoryWebRequest.FromResponse(request));
                                if (batchResponse.Parts.Count > 0)
                                {
                                    actualStatusCode = batchResponse.Parts[0].ResponseStatusCode;
                                    if (actualStatusCode == 200) actualStatusCode = 0;
                                }
                            }

                            Assert.AreEqual(testCase.ExpectedErrorStatusCode, actualStatusCode, "Wrong status code.");
                        }

                        using (TestWebRequest request = clientService.CreateForInProcessWcf())
                        {
                            request.StartService();

                            clientService.ProcessRequestOverride = clientRequest =>
                            {
                                var clientResponse = new InMemoryWebRequest();
                                clientResponse.SetResponseStatusCode(202);
                                clientResponse.ResponseHeaders["Content-Type"] = testCase.ContentType;
                                clientResponse.SetResponseStreamAsText(string.Format(
                                    "--{0}\r\n" +
                                    "Content-Type: application/http\r\n" +
                                    "Content-Transfer-Encoding: binary\r\n" +
                                    "\r\n" +
                                    "200 OK\r\n" +
                                    "<feed xmlns='http://www.w3.org/2005/Atom'/>\r\n" +
                                    "--{0}--\r\n",
                                    testCase.PayloadBatchBoundary));
                                return clientResponse;
                            };

                            DataServiceContext ctx = new DataServiceContext(request.ServiceRoot);
                            Exception exception = TestUtil.RunCatching(() => ctx.ExecuteBatch(ctx.CreateQuery<Customer>("/Customers")));

                            if (exception != null)
                            {
                                exception = ((DataServiceRequestException)exception).InnerException;
                                Assert.AreEqual(testCase.ExpectedClientErrorMessage, exception.Message, "Unexpected error message.");
                            }
                            else
                            {
                                Assert.IsNull(testCase.ExpectedClientErrorMessage, "Expected exception, but none was thrown.");
                            }
                        }
                    });
            }
开发者ID:AlineGuan,项目名称:odata.net,代码行数:101,代码来源:BatchTests.cs


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