public CustomizedRequestMessage(DataServiceClientRequestMessageArgs args, ConnectionProperties properties) : base(args)
        {
            HttpWebRequest.Proxy = properties.GetWebProxy();
            HttpWebRequest.Headers.Add(properties.GetCustomHeaders());

            var cert = properties.GetClientCertificate();

            if (cert != null)
            {
                HttpWebRequest.ClientCertificates.Add(cert);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the metadata stream from a file path or a http/https path.
        /// </summary>
        /// <param name="metadataUri">The Uri to the stream. The supported scheme are File, http and https.</param>
        /// <param name="properties"></param>
        private static Stream GetEdmxStreamFromUri(Uri metadataUri, ConnectionProperties properties)
        {
            Debug.Assert(metadataUri != null, "metadataUri != null");
            Stream metadataStream;

            if (metadataUri.Scheme == "file")
            {
                metadataStream = new FileStream(Uri.UnescapeDataString(metadataUri.AbsolutePath), FileMode.Open, FileAccess.Read);
            }
            else if (metadataUri.Scheme == "http" || metadataUri.Scheme == "https")
            {
                try
                {
                    var webRequest = (HttpWebRequest)WebRequest.Create(metadataUri);
                    webRequest.Credentials = properties.GetCredentials();
                    webRequest.Proxy       = properties.GetWebProxy();
                    webRequest.Headers.Add(properties.GetCustomHeaders());

                    var cert = properties.GetClientCertificate();
                    if (cert != null)
                    {
                        webRequest.ClientCertificates.Add(cert);
                    }

                    ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => properties.AcceptInvalidCertificate;

                    var webResponse = webRequest.GetResponse();
                    metadataStream = webResponse.GetResponseStream();
                }
                catch (WebException e)
                {
                    var webResponse = e.Response as HttpWebResponse;
                    if (webResponse != null && webResponse.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        throw new WebException("Failed to access the metadata document. The OData service requires authentication for accessing it. Please download the metadata, store it into a local file, and set the value of “MetadataDocumentUri” in the .odata.config file to the file path. After that, run custom tool again to generate the OData Client code.");
                    }
                    throw;
                }
            }
            else
            {
                throw new ArgumentException("Only file, http, https schemes are supported for paths to metadata source locations.");
            }

            return(metadataStream);
        }