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


C# DataServiceContext.GetReadStream方法代码示例

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


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

示例1: VerifyMissingLinkQueryScenario

        public void VerifyMissingLinkQueryScenario()
        {
            // Make sure the query scenarios fail, when self/edit links for named stream is missing
            TestUtil.RunCombinations(
                UnitTestsUtil.BooleanValues,
                UnitTestsUtil.BooleanValues,
                UnitTestsUtil.BooleanValues,
                (syncRead, hasSelfLink, hasEditLink) =>
                {
                    using (PlaybackService.OverridingPlayback.Restore())
                    {
                        DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4);
                        context.EnableAtom = true;

                        string links = null;
                        string contentType = null;
                        string selfLink = null;
                        string editLink = null;

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

                        if (hasEditLink)
                        {
                            editLink = request.ServiceRoot + "/Customers(1)/EditLink/Thumbnail";
                            contentType = MediaContentType;
                            links += GetNamedStreamEditLink(editLink, 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();
                        StreamDescriptor ns = null;
                        bool expectException = false;

                        if (!hasEditLink && !hasSelfLink)
                        {
                            expectException = true;
                            Assert.AreEqual(0, ed.StreamDescriptors.Count, "No named streams should be present");
                        }
                        else
                        {
                            ns = ed.StreamDescriptors.Single();
                            Assert.AreEqual(ns.StreamLink.SelfLink, selfLink, "self link must match");
                            Assert.AreEqual(ns.StreamLink.EditLink, editLink, "edit link must match");
                            Assert.AreEqual(ns.StreamLink.ContentType, contentType, "content type must match");
                            Assert.AreEqual(context.GetReadStreamUri(c, ns.StreamLink.Name).AbsoluteUri, ns.StreamLink.SelfLink != null ? ns.StreamLink.SelfLink.AbsoluteUri : ns.StreamLink.EditLink.AbsoluteUri,
                                "Make sure that context.GetReadStreamUri returns the self link if present, otherwise returns edit link");
                        }

                        try
                        {
                            if (syncRead)
                            {
                                context.GetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" });
                            }
                            else
                            {
                                IAsyncResult result = context.BeginGetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" }, (r) =>
                                    {
                                        context.EndGetReadStream(r);
                                    },
                                    null);

                                if (!result.CompletedSynchronously)
                                {
                                    Assert.IsTrue(result.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, AstoriaUnitTests.TestConstants.MaxTestTimeout), false), "BeginExecute timeout");
                                }
                            }

                            Assert.IsTrue(!expectException, "should reach here when no exception is expected");
                            Assert.IsTrue(PlaybackService.LastPlayback.Contains("GET " + selfLink ?? editLink), "should use self link if present, otherwise editlink");
                        }
                        catch (ArgumentException ex)
                        {
                            Assert.IsTrue(expectException, "should get exception when expected");
                            ArgumentException expectedException = new ArgumentException(DataServicesClientResourceUtil.GetString("Context_EntityDoesNotContainNamedStream", "Thumbnail"), "name");
                            Assert.AreEqual(expectedException.Message, ex.Message, "Error message did not match");
                            Assert.AreEqual(0, ed.StreamDescriptors.Count, "No named streams should be present");
                        }
                    }
                });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:95,代码来源:NamedStreamTests.cs

示例2: 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

示例3: NamedSteams_VerifyGetReadStreamVersion

        public void NamedSteams_VerifyGetReadStreamVersion()
        {
            // Verify that GetReadStream for a named stream sends version 3 headers
            // Populate the context with a single customer instance
            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));

            TestUtil.RunCombinations(UnitTestsUtil.BooleanValues, (syncRead) =>
            {
                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();

                    if (syncRead)
                    {
                        context.GetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "img/jpeg" });
                    }
                    else
                    {
                        IAsyncResult result = context.BeginGetReadStream(c, "Thumbnail", new DataServiceRequestArgs() { ContentType = "image/jpeg" }, (r) =>
                            {
                                context.EndGetReadStream(r);
                            },
                            null);

                        if (!result.CompletedSynchronously)
                        {
                            Assert.IsTrue(result.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, AstoriaUnitTests.TestConstants.MaxTestTimeout), false), "BeginExecute timeout");
                        }
                    }

                    VerifyRequestWasVersion3(PlaybackService.LastPlayback);
                }
            });
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:42,代码来源:NamedStreamTests.cs

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