public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        Uri originalURI = null;

        if (request.Properties.ContainsKey("OriginalHttpRequestUri"))
        {
            originalURI = request.Properties["OriginalHttpRequestUri"] as Uri;
        }
        else
        {
            //When running in WCF ServiceHost mode , the OriginalHttpRequestUri is inaccissible as the host does not support it
            originalURI = request.Properties.Via;
        }

        #region Log only interesting operations
        string lastSegment = originalURI.Segments.Last();
        if (ignorePoints.Any(fo => lastSegment.Contains(fo)))
        {
            return(null);
        }
        #endregion

        HttpRequestMessageProperty httpmsg = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        if (httpmsg != null && httpmsg.Headers != null && httpmsg.Headers.AllKeys.Contains(LogIndexHeader))
        {
            PTEntities ctx = new PTEntities();
            int        LogIndex;
            if (!Int32.TryParse(httpmsg.Headers[LogIndexHeader], out LogIndex))
            {
                return(null);
            }

            TestOperation t = ctx.TestOperations.FirstOrDefault(to => (to.Id == LogIndex));
            if (t == null)
            {
                return(null);
            }

            LogEntry le = new LogEntry {
                TestOperation = t, URI = originalURI.ToString(), Verb = httpmsg.Method
            };
            ctx.AddObject("LogEntries", le);

            // skip loging of the logIndex header
            foreach (var key in httpmsg.Headers.AllKeys.Where(k => !k.Equals(LogIndexHeader, StringComparison.OrdinalIgnoreCase)))
            {
                ctx.AddObject("LogEntryHeaders", new LogEntryHeader {
                    LogEntry = le, Header = key, Value = httpmsg.Headers[key]
                });
            }
            ctx.SaveChanges();
            return(new MyCorrelation {
                Uri = originalURI, LogIndex = httpmsg.Headers[LogIndexHeader]
            });
        }
        return(null);
    }
    public PictureStreamProvider(PTEntities entityContext)
    {
        this.entityContext = entityContext;

        string assemblyPath = System.Data.Test.Astoria.FullTrust.TrustedMethods.GetAssemblyLocation(Assembly.GetExecutingAssembly());

        assemblyPath = Path.GetDirectoryName(assemblyPath);
        string appName = assemblyPath.Split('/', '\\').Last();

        DataDirName = Path.Combine(System.Data.Test.Astoria.FullTrust.TrustedMethods.GetTempPath(), Path.Combine("BlobClientTestFiles", appName));

        // copy the static data files into temp directory
        string sourceDir = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/BlobClientTestFiles/") ??
                           Path.Combine(assemblyPath, @"App_Data\BlobClientTestFiles");

        System.Data.Test.Astoria.FullTrust.TrustedMethods.EnsureDirectoryExists(DataDirName);
        System.Data.Test.Astoria.FullTrust.TrustedMethods.CopyDirectoryRecursively(sourceDir, DataDirName);
    }
    public XElement GetLog()
    {
        int           id  = GetIdFromHeaders();
        PTEntities    ctx = this.CurrentDataSource;
        TestOperation t   = ctx.TestOperations.Include("LogEntries.LogEntryHeaders").FirstOrDefault(to => (to.Id == id));

        if (t == null)
        {
            return(null);
        }
        XElement retVal = new XElement("TestOperation", new XAttribute("id", id), new XAttribute("desc", t.Name));

        foreach (var le in t.LogEntries)
        {
            XElement xle = new XElement(le.Verb, new XAttribute("Uri", le.URI));
            retVal.Add(xle);
            foreach (var leh in le.LogEntryHeaders)
            {
                xle.Add(new XElement("Header", new XAttribute("key", leh.Header), new XAttribute("value", leh.Value)));
            }
        }
        return(retVal);
    }
    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        if (correlationState != null && correlationState is MyCorrelation)
        {
            MyCorrelation myCor = correlationState as MyCorrelation;
            HttpResponseMessageProperty httpmsg = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
            if (httpmsg != null && httpmsg.Headers != null)
            {
                PTEntities ctx = new PTEntities();
                int        LogIndex;
                if (!Int32.TryParse(myCor.LogIndex, out LogIndex))
                {
                    return;
                }

                TestOperation t = ctx.TestOperations.FirstOrDefault(to => (to.Id == LogIndex));
                if (t == null)
                {
                    return;
                }

                LogEntry le = new LogEntry {
                    TestOperation = t, URI = myCor.Uri.ToString(), Verb = "RESPONSE"
                };
                ctx.AddObject("LogEntries", le);

                // skip loging of the logIndex header
                foreach (var key in httpmsg.Headers.AllKeys.Where(k => !k.Equals(LogIndexHeader, StringComparison.OrdinalIgnoreCase)))
                {
                    ctx.AddObject("LogEntryHeaders", new LogEntryHeader {
                        LogEntry = le, Header = key, Value = httpmsg.Headers[key]
                    });
                }
                ctx.SaveChanges();
            }
        }
    }
    public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState) {
        if (correlationState != null && correlationState is MyCorrelation) {
            MyCorrelation myCor = correlationState as MyCorrelation;
            HttpResponseMessageProperty httpmsg = (HttpResponseMessageProperty)reply.Properties[HttpResponseMessageProperty.Name];
            if (httpmsg != null && httpmsg.Headers != null) {
                PTEntities ctx = new PTEntities();
                int LogIndex;
                if (!Int32.TryParse(myCor.LogIndex, out LogIndex)) return;

                TestOperation t = ctx.TestOperations.FirstOrDefault(to => (to.Id == LogIndex));
                if (t == null) return;

                LogEntry le = new LogEntry { TestOperation = t, URI = myCor.Uri.ToString(), Verb = "RESPONSE" };
                ctx.AddObject("LogEntries", le);

                // skip loging of the logIndex header
                foreach (var key in httpmsg.Headers.AllKeys.Where(k => !k.Equals(LogIndexHeader, StringComparison.OrdinalIgnoreCase))) {
                    ctx.AddObject("LogEntryHeaders", new LogEntryHeader { LogEntry = le, Header = key, Value = httpmsg.Headers[key] });
                }
                ctx.SaveChanges();
            }
        }
    }
    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext) {
        Uri originalURI = null;
        if (request.Properties.ContainsKey("OriginalHttpRequestUri"))
        {
            originalURI = request.Properties["OriginalHttpRequestUri"] as Uri;
        }
        else
        {
            //When running in WCF ServiceHost mode , the OriginalHttpRequestUri is inaccissible as the host does not support it 
            originalURI = request.Properties.Via;
        }

        #region Log only interesting operations
        string lastSegment = originalURI.Segments.Last();
        if (ignorePoints.Any(fo => lastSegment.Contains(fo))) return null;
        #endregion

        HttpRequestMessageProperty httpmsg = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        if (httpmsg != null && httpmsg.Headers != null && httpmsg.Headers.AllKeys.Contains(LogIndexHeader)) {


            PTEntities ctx = new PTEntities();
            int LogIndex;
            if (!Int32.TryParse(httpmsg.Headers[LogIndexHeader], out LogIndex)) return null;

            TestOperation t = ctx.TestOperations.FirstOrDefault(to => (to.Id == LogIndex));
            if (t == null) return null;

            LogEntry le = new LogEntry { TestOperation = t, URI = originalURI.ToString(), Verb = httpmsg.Method };
            ctx.AddObject("LogEntries", le);

            // skip loging of the logIndex header
            foreach (var key in httpmsg.Headers.AllKeys.Where(k => !k.Equals(LogIndexHeader, StringComparison.OrdinalIgnoreCase))) {
                ctx.AddObject("LogEntryHeaders", new LogEntryHeader { LogEntry = le, Header = key, Value = httpmsg.Headers[key] });
            }
            ctx.SaveChanges();
            return new MyCorrelation { Uri = originalURI, LogIndex = httpmsg.Headers[LogIndexHeader] };
        }
        return null;
    }
    public PictureStreamProvider(PTEntities entityContext) 
    {
        this.entityContext = entityContext;

        string assemblyPath = System.Data.Test.Astoria.FullTrust.TrustedMethods.GetAssemblyLocation(Assembly.GetExecutingAssembly());
        assemblyPath = Path.GetDirectoryName(assemblyPath);
        string appName = assemblyPath.Split('/', '\\').Last();
        DataDirName = Path.Combine(System.Data.Test.Astoria.FullTrust.TrustedMethods.GetTempPath(), Path.Combine("BlobClientTestFiles", appName));

        // copy the static data files into temp directory 
        string sourceDir = System.Web.Hosting.HostingEnvironment.MapPath(@"~/App_Data/BlobClientTestFiles/") ?? 
            Path.Combine(assemblyPath, @"App_Data\BlobClientTestFiles");

        System.Data.Test.Astoria.FullTrust.TrustedMethods.EnsureDirectoryExists(DataDirName);
        System.Data.Test.Astoria.FullTrust.TrustedMethods.CopyDirectoryRecursively(sourceDir, DataDirName);
    }