/// <summary>
        /// Called before a service method is executed.
        /// </summary>
        /// <param name="serviceContext">The service context.</param>
        /// <param name="behaviorContext">The "method executing" behavior context.</param>
        /// <returns>A service method action.</returns>
        public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
        {
            serviceContext.Request.ResourceBag.LoggingEnabled = true;
            serviceContext.Response.Output.WriteFormat("Action '{0}' executing", behaviorContext.GetMethodName()).WriteLine(2);

            return BehaviorMethodAction.Execute;
        }
        /// <summary>
        /// Called before a service method is executed.
        /// </summary>
        /// <param name="serviceContext">The service context.</param>
        /// <param name="behaviorContext">The "method executing" behavior context.</param>
        /// <returns>A service method action.</returns>
        public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
        {
            if (serviceContext == null)
            {
                throw new ArgumentNullException("serviceContext");
            }

            if (behaviorContext == null)
            {
                throw new ArgumentNullException("behaviorContext");
            }

            if (behaviorContext.Resource == null || m_validator == null)
            {
                return BehaviorMethodAction.Execute;
            }

            IReadOnlyCollection<ValidationError> validationErrors;

            if (!m_validator.IsValid(behaviorContext.Resource, out validationErrors))
            {
                serviceContext.GetHttpContext().Items[ResourceValidator.ValidationErrorKey] = new ResourceState(validationErrors);
            }

            return BehaviorMethodAction.Execute;
        }
        /// <summary>
        /// Called before a service method is executed.
        /// </summary>
        /// <param name="serviceContext">The service context.</param>
        /// <param name="behaviorContext">The "method executing" behavior context.</param>
        /// <returns>A service method action.</returns>
        public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
        {
            if (serviceContext == null)
            {
                throw new ArgumentNullException("serviceContext");
            }

            serviceContext.GetHttpContext().Items[ServiceCallConstants.MaxQueryResults] = AllowOverride ? (MaxResults * -1) : MaxResults;

            return BehaviorMethodAction.Execute;
        }
        /// <summary>
        /// Called before a service method is executed.
        /// </summary>
        /// <param name="serviceContext">The service context.</param>
        /// <param name="behaviorContext">The "method executing" behavior context.</param>
        /// <returns>A service method action.</returns>
        public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
        {
            timer = Stopwatch.StartNew();

            serviceContext.Response.Output.WriteFormat("Contract: {0}", behaviorContext.GetServiceContractType().Name).WriteLine();
            serviceContext.Response.Output.WriteFormat("Service: {0}", behaviorContext.GetServiceType().Name).WriteLine();
            serviceContext.Response.Output.WriteFormat("Method: {0}", behaviorContext.GetMethodName()).WriteLine();
            serviceContext.Response.Output.WriteFormat("Full URL: {0}", serviceContext.Request.Url.ToString()).WriteLine();
            serviceContext.Response.Output.WriteFormat("Relative URL: {0}", serviceContext.Request.Url.LocalPath).WriteLine();
            serviceContext.Response.Output.WriteFormat("Service absolute URL: {0}", serviceContext.Request.Url.OperationUrl.ToString()).WriteLine();
            serviceContext.Response.Output.WriteFormat("Service relative URL: {0}", serviceContext.Request.Url.OperationUrl.LocalPath).WriteLine();
            serviceContext.Response.Output.WriteFormat("Is Local: {0}", serviceContext.Request.IsLocal).WriteLine();
            serviceContext.Response.Output.WriteFormat("Is Secure: {0}", serviceContext.Request.IsSecure).WriteLine();
            serviceContext.Response.Output.WriteFormat("Is AJAX: {0}", serviceContext.Request.IsAjax).WriteLine();

            if (serviceContext.IsAuthenticated)
            {
                serviceContext.Response.Output.WriteFormat("Authenticated as: \"{0}\" ({1})", serviceContext.User.Identity.Name, serviceContext.User.Identity.AuthenticationType).WriteLine();

                if (serviceContext.User.IsInRole("Administrators"))
                {
                    serviceContext.Response.Output.WriteLine("-- ADMIN ACCESS --");
                }
            }
            else
            {
                serviceContext.Response.Output.WriteLine("Not Authenticated");
            }

            serviceContext.Response.Output.WriteLine().WriteLine("--- Route Values ---");

            foreach (string key in serviceContext.Request.RouteValues.Keys)
            {
                serviceContext.Response.Output.WriteFormat("{0} : {1}", key, serviceContext.Request.RouteValues.TryGet(key)).WriteLine();
            }

            serviceContext.Response.Output.WriteLine().WriteLine("--- Query Values ---");

            foreach (string key in serviceContext.Request.QueryString.Keys)
            {
                serviceContext.Response.Output.WriteFormat("{0} : {1}", key, serviceContext.Request.QueryString.TryGet(key)).WriteLine();
            }

            serviceContext.Response.Output.WriteLine();

            return BehaviorMethodAction.Execute;
        }
        /// <summary>
        /// Called before a service method is executed.
        /// </summary>
        /// <param name="serviceContext">The service context.</param>
        /// <param name="behaviorContext">The "method executing" behavior context.</param>
        /// <returns>A service method action.</returns>
        public override BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
        {
            if (serviceContext == null)
            {
                throw new ArgumentNullException("serviceContext");
            }

            if (serviceContext.Request.Method != HttpMethod.Post && serviceContext.Request.Method != HttpMethod.Put && serviceContext.Request.Method != HttpMethod.Patch)
            {
                return base.OnMethodExecuting(serviceContext, behaviorContext);
            }

            if (ThrowOnNullResource && ReferenceEquals(null, serviceContext.Request.ResourceBag.Resource))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest, Resources.Global.NoResourceProvided);
            }

            if (!serviceContext.Request.ResourceState.IsValid)
            {
                throw new HttpResourceFaultException();
            }

            return base.OnMethodExecuting(serviceContext, behaviorContext);
        }
 /// <summary>
 /// Called before a service method is executed.
 /// </summary>
 /// <param name="serviceContext">The service context.</param>
 /// <param name="behaviorContext">The "method executing" behavior context.</param>
 /// <returns>A service method action.</returns>
 public virtual BehaviorMethodAction OnMethodExecuting(IServiceContext serviceContext, MethodExecutingContext behaviorContext)
 {
     return BehaviorMethodAction.Execute;
 }
        public void MethodExecutingContextShouldBePopulatedForSelfContainedService()
        {
            var service = new TestSelfContainedService();

            MethodInfo method = service.GetType().GetMethod("GetOK");
            Assert.That(method, Is.Not.Null);

            var behaviorContext = new MethodExecutingContext(service, method, null);
            Assert.That(behaviorContext.Service, Is.SameAs(service));
            Assert.That(behaviorContext.Method, Is.SameAs(method));
            Assert.That(behaviorContext.Resource, Is.Null);
            Assert.That(behaviorContext.GetServiceContractType(), Is.EqualTo(typeof(TestSelfContainedService)));
            Assert.That(behaviorContext.GetServiceType(), Is.EqualTo(typeof(TestSelfContainedService)));
            Assert.That(behaviorContext.GetMethodName(), Is.EqualTo(method.Name));

            var httpMethods = behaviorContext.GetSupportedHttpMethods().ToList();
            Assert.That(httpMethods.Contains(HttpMethod.Get), Is.True);
            Assert.That(httpMethods.Contains(HttpMethod.Head), Is.False);
            Assert.That(httpMethods.Contains(HttpMethod.Post), Is.False);
            Assert.That(httpMethods.Contains(HttpMethod.Put), Is.False);
            Assert.That(httpMethods.Contains(HttpMethod.Patch), Is.False);
            Assert.That(httpMethods.Contains(HttpMethod.Delete), Is.False);
        }