﻿using System;
using System.Reflection;
using System.ServiceModel;
using DotAspect;
using Hardcore.Web.Wcf;

namespace Hardcore.Web.TestCommon.Wcf
{
    public static class DumpRequestOnErrorTest
    {
        public const string ExceptionText = "ExceptionText";

        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            void ThrowSimpleException();

            [OperationContract]
            string GetLastRequestDump();
        }

        public interface IServiceObject
        {
            string LastRequestDump { get; set; }
        }

        public class CatchDumpAttribute : AspectAttribute
        {
            public override AspectLogic CreateAspectLogic(MethodBase targetMethod)
            {
                return new Logic();
            }

            class Logic : ErrorHandlerAspectLogic
            {
                protected override void HandleError(object targetObject, MethodBase method, Exception exception)
                {
                    ((IServiceObject)targetObject).LastRequestDump = LogDumpOnErrorLogic.GetDumpFromCurrentOperationContext();
                }
            }
        }
    }
}
