Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the ODataService class.
        /// </summary>
        public ODataService()
        {
            Debug.Assert(WebOperationContext.Current != null, "WebOperationContext.Current != null");

            var uri = OperationContext.Current.IncomingMessageProperties.ContainsKey("MicrosoftDataServicesRequestUri")
                ? OperationContext.Current.IncomingMessageProperties["MicrosoftDataServicesRequestUri"] as Uri : HttpContext.Current.Request.Url;

            var rootUrl = OperationContext.Current.IncomingMessageProperties.ContainsKey("MicrosoftDataServicesRootUri")
                ? OperationContext.Current.IncomingMessageProperties["MicrosoftDataServicesRootUri"] as Uri : null;

            var urlSettings  = UrlSettingsAttribute.GetServiceUrlSettings(GetType());
            var urlSanitizer = new UrlSanitizer(urlSettings);

            Debug.Assert(uri != null, "uri != null");
            var sanitizedUrl = urlSanitizer.Sanitize(uri.ToString());

            // FIX: accessing the root causes a null reference error
            // http://wcfdstoolkit.codeplex.com/workitem/28
            if (rootUrl == null && WebOperationContext.Current.IncomingRequest.UriTemplateMatch == null)
            {
                rootUrl = uri;
            }

            var rootUri           = rootUrl == null ? WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri : new Uri(rootUrl.ToString());
            var rootUriBuilder    = new UriBuilder(rootUri);
            var requestUriBuilder = new UriBuilder(sanitizedUrl.ToString());

            //// If we're not debugging, then attempt to refactor
            //// the request host and port based on the URL settings.
            if (!HttpContext.Current.Request.IsLocal)
            {
                if (!string.IsNullOrWhiteSpace(urlSettings.Host))
                {
                    rootUriBuilder.Host    = urlSettings.Host;
                    requestUriBuilder.Host = urlSettings.Host;
                }

                if (urlSettings.ClearPorts)
                {
                    rootUriBuilder.Port    = -1;
                    requestUriBuilder.Port = -1;
                }
            }

            if (!rootUriBuilder.Path.EndsWith("/"))
            {
                rootUriBuilder.Path = rootUriBuilder.Path += "/";
            }

            if (!requestUriBuilder.Path.EndsWith("/"))
            {
                requestUriBuilder.Path = requestUriBuilder.Path += "/";
            }

            OperationContext.Current.IncomingMessageProperties["MicrosoftDataServicesRootUri"]    = rootUriBuilder.Uri;
            OperationContext.Current.IncomingMessageProperties["MicrosoftDataServicesRequestUri"] = requestUriBuilder.Uri;
        }
 /// <summary>
 /// Initializes a new instance of the UrlSanitizer class.
 /// </summary>
 /// <param name="urlSettings">The url settings for the sanitization process.</param>
 public UrlSanitizer(UrlSettingsAttribute urlSettings)
 {
     this.urlSettings = urlSettings;
 }
 /// <summary>
 /// Initializes a new instance of the UrlSanitizer class.
 /// </summary>
 /// <param name="urlSettings">The url settings for the sanitization process.</param>
 public UrlSanitizer(UrlSettingsAttribute urlSettings)
 {
     this.urlSettings = urlSettings;
 }