/// <summary>
 /// Extension method to perform sync/async version of DataServiceContext.GetReadStream dynamically
 /// </summary>
 /// <param name="context">The context to call get read stream on</param>
 /// <param name="continuation">The asynchronous continuation</param>
 /// <param name="async">A value indicating whether or not to use async API</param>
 /// <param name="entity">The entity to get the read stream for</param>
 /// <param name="streamName">The name of the stream or null to indicate the default stream</param>
 /// <param name="args">The args to the request</param>
 /// <param name="onCompletion">A callback for when the call completes</param>
 public static void GetReadStream(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string streamName, DataServiceRequestArgs args, Action <DataServiceStreamResponse> onCompletion)
 {
     ExceptionUtilities.CheckArgumentNotNull(context, "context");
     if (streamName == null)
     {
         AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, args), c => context.BeginGetReadStream(entity, args, c, null), r => context.EndGetReadStream(r), onCompletion);
     }
     else
     {
         AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, streamName, args), c => context.BeginGetReadStream(entity, streamName, args, c, null), r => context.EndGetReadStream(r), onCompletion);
     }
 }
        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");
                        }
                    }
                });
        }
Exemple #3
0
        /// <summary>
        /// Extension method to perform sync/async version of DataServiceContext.GetReadStream dynamically
        /// </summary>
        /// <param name="context">The context to call get read stream on</param>
        /// <param name="continuation">The asynchronous continuation</param>
        /// <param name="async">A value indicating whether or not to use async API</param>
        /// <param name="entity">The entity to get the read stream for</param>
        /// <param name="streamName">The name of the stream or null to indicate the default stream</param>
        /// <param name="args">The args to the request</param>
        /// <param name="onCompletion">A callback for when the call completes</param>
        public static void GetReadStream(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string streamName, DataServiceRequestArgs args, Action <DataServiceStreamResponse> onCompletion)
        {
            ExceptionUtilities.CheckArgumentNotNull(context, "context");
            if (streamName == null)
            {
                AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, args), c => context.BeginGetReadStream(entity, args, c, null), r => context.EndGetReadStream(r), onCompletion);
            }
            else
            {
#if WINDOWS_PHONE
                throw new TaupoNotSupportedException("Named streams are not supported on Windows Phone yet");
#else
                AsyncHelpers.InvokeSyncOrAsyncMethodCall <DataServiceStreamResponse>(continuation, async, () => context.GetReadStream(entity, streamName, args), c => context.BeginGetReadStream(entity, streamName, args, c, null), r => context.EndGetReadStream(r), onCompletion);
#endif
            }
        }
        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);
                }
            });
        }