//[TestMethod, Variation("One should not be able to get named streams via load property api")] public void NamedStreams_LoadPropertyTest() { // populate the context DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); EntityWithNamedStreams1 entity = context.CreateQuery <EntityWithNamedStreams1>("MySet1").Take(1).Single(); try { context.LoadProperty(entity, "Stream1"); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } try { context.BeginLoadProperty( entity, "Stream1", (result) => { context.EndLoadProperty(result); }, null); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } }
/// <summary> /// Extension method to perform sync/async version of DataServiceContext.LoadProperty dynamically /// </summary> /// <param name="context">The context to call call load property 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 load a property on</param> /// <param name="propertyName">The name of the property to load</param> /// <param name="onCompletion">A callback for when the call completes</param> public static void LoadProperty(this DataServiceContext context, IAsyncContinuation continuation, bool async, object entity, string propertyName, Action <QueryOperationResponse> onCompletion) { ExceptionUtilities.CheckArgumentNotNull(context, "context"); AsyncHelpers.InvokeSyncOrAsyncMethodCall <QueryOperationResponse>( continuation, async, () => context.LoadProperty(entity, propertyName), c => context.BeginLoadProperty(entity, propertyName, c, null), r => context.EndLoadProperty(r), onCompletion); }
/// <summary> /// Asynchronously loads the next page of related entities from a navigation property. /// </summary> /// <typeparam name="TResult">Entity type of the result of the execution.</typeparam> /// <param name="context">The <see cref="T:System.Data.Services.Client.DataServiceContext"/> instance on which this extension method is enabled. </param> /// <param name="entity">The parent entity.</param> /// <param name="propertyName">The name of the navigation property.</param> /// <param name="nextLinkUri">The URI that is used to request the next page of data.</param> /// <returns>A <see cref="T:System.Threading.Tasks.Task`1"/> that, when completed, returns the results of the execution.</returns> public static async Task <IEnumerable <TResult> > LoadPropertyAsync <TResult>(this DataServiceContext context, object entity, string propertyName, Uri nextLinkUri) { var queryTask = Task.Factory.FromAsync <IEnumerable <TResult> >(context.BeginLoadProperty(entity, propertyName, nextLinkUri, null, null), (loadPropertyAsyncResult) => { var results = context.EndLoadProperty(loadPropertyAsyncResult); return((IEnumerable <TResult>)results); }); return(await queryTask); }
public void QueryExpandStronglyTypedProperties() { Uri baseUri = ctx.BaseUri; northwindClient.Products product = ctx.Execute <northwindClient.Products>(new Uri(baseUri.OriginalString + "/Products?$top=1")).Single <northwindClient.Products>(); Func <object, string, QueryOperationResponse> getResponse; for (int i = 0; i < 2; i++) { if (i == 0) { getResponse = (o, p) => { return(ctx.LoadProperty(o, p)); }; } else { // Async pattern getResponse = (o, p) => { IAsyncResult async = ctx.BeginLoadProperty(o, p, null, null); if (!async.CompletedSynchronously) { Assert.IsTrue(async.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, TestConstants.MaxTestTimeout), false), "BeginLoadProperty timeout"); } Assert.IsTrue(async.IsCompleted); return(ctx.EndLoadProperty(async)); }; } int productID = ((QueryOperationResponse <int>)getResponse(product, "ProductID")).Single(); string productName = ((QueryOperationResponse <string>)getResponse(product, "ProductName")).Single(); short? reorderLevel = ((QueryOperationResponse <short?>)getResponse(product, "ReorderLevel")).Single(); northwindClient.Categories category = ((QueryOperationResponse <northwindClient.Categories>)getResponse(product, "Categories")).Single(); QueryOperationResponse <northwindClient.Order_Details> orderDetails = (QueryOperationResponse <northwindClient.Order_Details>)getResponse(product, "Order_Details"); Assert.IsTrue(orderDetails.Count() != 0, "There must be one or more order details"); } }
//[TestMethod, Variation("One should not be able to get named streams via load property api")] public void NamedStreams_LoadPropertyTest() { // populate the context DataServiceContext context = new DataServiceContext(request.ServiceRoot, ODataProtocolVersion.V4); EntityWithNamedStreams1 entity = context.CreateQuery<EntityWithNamedStreams1>("MySet1").Take(1).Single(); try { context.LoadProperty(entity, "Stream1"); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } try { context.BeginLoadProperty( entity, "Stream1", (result) => { context.EndLoadProperty(result); }, null); } catch (DataServiceClientException ex) { Assert.IsTrue(ex.Message.Contains(DataServicesResourceUtil.GetString("DataService_VersionTooLow", "1.0", "3", "0")), String.Format("The error message was not as expected: {0}", ex.Message)); } }