public void IsBatchOperationIsSetInConstructor()
 {
     var eventArgs = new ReceivingResponseEventArgs(new ResponseMessageSimulator(null), null, true);
     eventArgs.IsBatchPart.Should().BeTrue();
     eventArgs = new ReceivingResponseEventArgs(new ResponseMessageSimulator(null), null, false);
     eventArgs.IsBatchPart.Should().BeFalse();
 }
 public void ConstructorShouldGetHeadersFromParameter()
 {
     var originalHeaders = new List<KeyValuePair<string, string>> {new KeyValuePair<string, string>("first-header", "first_value"), new KeyValuePair<string, string>("second header", "second value")};
     var eventArgs = new ReceivingResponseEventArgs(new ResponseMessageSimulator(originalHeaders), null, false);
     eventArgs.ResponseMessage.Headers.Count().Should().Be(originalHeaders.Count);
     originalHeaders.ForEach((h) => eventArgs.ResponseMessage.Headers.Should().Contain(h));
 }
Beispiel #3
0
        public static void ContextReceivingResponse(object sender, Microsoft.OData.Client.ReceivingResponseEventArgs e)
        {
            var response   = e.ResponseMessage as HttpWebResponseMessage;
            var statusCode = response.StatusCode;
            var headers    = response.Headers;

            Resources context = (Resources)sender;

            int retrySeconds = 5;

            if (statusCode == 429)
            {
                if (response.GetHeader("Retry-After") != "")
                {
                    var val = response.GetHeader("Retry-After");
                    retrySeconds = int.Parse(val);
                }

                Console.WriteLine("Caught 429, waiting {0}", retrySeconds);

                Thread.Sleep(TimeSpan.FromSeconds(retrySeconds));
            }
        }
        /// <summary>
        /// Loads the metadata and converts it into an EdmModel that is then used by a dataservice context
        /// This allows the user to use the DataServiceContext directly without having to manually pass an IEdmModel in the Format
        /// </summary>
        /// <returns>A service model to be used in format tracking</returns>
        internal IEdmModel LoadServiceModelFromNetwork()
        {
            HttpWebRequestMessage    httpRequest;
            BuildingRequestEventArgs requestEventArgs = null;

            // test hook for injecting a network request to use instead of the default
            if (InjectMetadataHttpNetworkRequest != null)
            {
                httpRequest = InjectMetadataHttpNetworkRequest();
            }
            else
            {
                requestEventArgs = new BuildingRequestEventArgs(
                    "GET",
                    context.GetMetadataUri(),
                    null,
                    null,
                    context.HttpStack);

                // fire the right events if they exist to allow user to modify the request
                if (context.HasBuildingRequestEventHandlers)
                {
                    requestEventArgs = context.CreateRequestArgsAndFireBuildingRequest(
                        requestEventArgs.Method,
                        requestEventArgs.RequestUri,
                        requestEventArgs.HeaderCollection,
                        requestEventArgs.ClientHttpStack,
                        requestEventArgs.Descriptor);
                }

                DataServiceClientRequestMessageArgs args = new DataServiceClientRequestMessageArgs(
                    requestEventArgs.Method,
                    requestEventArgs.RequestUri,
                    context.UseDefaultCredentials,
                    context.UsePostTunneling,
                    requestEventArgs.Headers);

                httpRequest = new HttpWebRequestMessage(args);
            }

            Descriptor descriptor = requestEventArgs != null ? requestEventArgs.Descriptor : null;

            // fire the right events if they exist
            if (context.HasSendingRequest2EventHandlers)
            {
                SendingRequest2EventArgs eventArgs = new SendingRequest2EventArgs(
                    httpRequest,
                    descriptor,
                    false);

                context.FireSendingRequest2(eventArgs);
            }

            Task <IODataResponseMessage> asyncResponse =
                Task <IODataResponseMessage> .Factory.FromAsync(httpRequest.BeginGetResponse, httpRequest.EndGetResponse,
                                                                httpRequest);

            IODataResponseMessage response = asyncResponse.GetAwaiter().GetResult();

            ReceivingResponseEventArgs responseEvent = new ReceivingResponseEventArgs(response, descriptor);

            context.FireReceivingResponseEvent(responseEvent);

            using (StreamReader streamReader = new StreamReader(response.GetStream()))
                using (XmlReader xmlReader = XmlReader.Create(streamReader))
                {
                    return(CsdlReader.Parse(xmlReader));
                }
        }
 public void DescriptorIsFromConstructor()
 {
     var descriptor = new FunctionDescriptor();
     var eventArgs = new ReceivingResponseEventArgs(new ResponseMessageSimulator(null), descriptor);
     eventArgs.Descriptor.Should().BeSameAs(descriptor);
 }