Пример #1
0
        /// <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;
        }
Пример #2
0
        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);
        }