<commit_before>﻿using System;
using System.Diagnostics;
using ServiceStack.CacheAccess;
using ServiceStack.Common.Web;
using ServiceStack.Logging;
using ServiceStack.Messaging;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Validation;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

namespace ServiceStack.ServiceInterface
{
    /// <summary>
    /// A Useful ServiceBase for all services with support for automatically serializing
    /// Exceptions into a common ResponseError DTO so errors can be handled generically by clients. 
    /// 
    /// If an 'IRedisClientsManager' is configured in your AppHost, service errors will
    /// also be maintained into a service specific and combined rolling error log.
    /// </summary>
    /// <typeparam name="TRequest"></typeparam>
    public abstract class ServiceBase<TRequest>
        : IService<TRequest>, IRequiresRequestContext, IServiceBase, IAsyncService<TRequest>
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ServiceBase<>));

        /// <summary>
        /// Stopwatch used to record the duration of each request
        /// </summary>
        private Stopwatch requestDurationStopwatch;
        
        /// <summary>
        /// Can be overriden to supply Custom 'ServiceName' error logs
        /// </summary>
        public virtual string ServiceName
        {
            get { return typeof(TRequest).Name; }
        }

        /// <summary>
        /// Access to the Applications ServiceStack AppHost Instance
        /// </summary>
        /// 
        private IAppHost appHost; //not property to stop alt IOC's creating new instances of AppHost
        
        public IAppHost GetAppHost()
        {
            return appHost ?? EndpointHost.AppHost;
        }

        private HandleServiceExceptionDelegate serviceExceptionHandler;
        public HandleServiceExceptionDelegate ServiceExceptionHandler
        {
            get { return serviceExceptionHandler ?? appHost.ServiceExceptionHandler; }
            set { serviceExceptionHandler = value; }
        }

        public ServiceBase<TRequest> SetAppHost(IAppHost appHost) //Allow chaining
        {
            this.appHost = appHost;
            return this;
        }

        /// <summary>
        /// Endpoint-agnostic Request Context
        /// </summary>
        public IRequestContext RequestContext { get; set; }

        /// <summary>
        /// The Http Request (for HTTP/S only)
        /// </summary>
        public IHttpRequest Request
        {
            get { return RequestContext.Get<IHttpRequest>(); }
        }

        /// <summary>
        /// The HTTP Response (for HTTP/S only)
        /// </summary>
        public IHttpResponse Response
        {
            get { return RequestContext.Get<IHttpResponse>(); }
        }

        public ISessionFactory SessionFactory { get; set; }<commit_msg>fix checking of ServiceExceptionHandler in ServiceBase<commit_after>﻿using System;
using System.Diagnostics;
using ServiceStack.CacheAccess;
using ServiceStack.Common.Web;
using ServiceStack.Logging;
using ServiceStack.Messaging;
using ServiceStack.ServiceClient.Web;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Validation;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints;

namespace ServiceStack.ServiceInterface
{
    /// <summary>
    /// A Useful ServiceBase for all services with support for automatically serializing
    /// Exceptions into a common ResponseError DTO so errors can be handled generically by clients. 
    /// 
    /// If an 'IRedisClientsManager' is configured in your AppHost, service errors will
    /// also be maintained into a service specific and combined rolling error log.
    /// </summary>
    /// <typeparam name="TRequest"></typeparam>
    public abstract class ServiceBase<TRequest>
        : IService<TRequest>, IRequiresRequestContext, IServiceBase, IAsyncService<TRequest>
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ServiceBase<>));

        /// <summary>
        /// Stopwatch used to record the duration of each request
        /// </summary>
        private Stopwatch requestDurationStopwatch;
        
        /// <summary>
        /// Can be overriden to supply Custom 'ServiceName' error logs
        /// </summary>
        public virtual string ServiceName
        {
            get { return typeof(TRequest).Name; }
        }

        /// <summary>
        /// Access to the Applications ServiceStack AppHost Instance
        /// </summary>
        /// 
        private IAppHost appHost; //not property to stop alt IOC's creating new instances of AppHost
        
        public IAppHost GetAppHost()
        {
            return appHost ?? EndpointHost.AppHost;
        }

        private HandleServiceExceptionDelegate serviceExceptionHandler;
        public HandleServiceExceptionDelegate ServiceExceptionHandler
        {
            get { return serviceExceptionHandler ?? (GetAppHost() != null ? GetAppHost().ServiceExceptionHandler : null); }
            set { serviceExceptionHandler = value; }
        }

        public ServiceBase<TRequest> SetAppHost(IAppHost appHost) //Allow chaining
        {
            this.appHost = appHost;
            return this;
        }

        /// <summary>
        /// Endpoint-agnostic Request Context
        /// </summary>
        public IRequestContext RequestContext { get; set; }

        /// <summary>
        /// The Http Request (for HTTP/S only)
        /// </summary>
        public IHttpRequest Request
        {
            get { return RequestContext.Get<IHttpRequest>(); }
        }

        /// <summary>
        /// The HTTP Response (for HTTP/S only)
        /// </summary>
        public IHttpResponse Response
        {
            get { return RequestContext.Get<IHttpResponse>(); }
        }

        public ISessionFactory SessionFactory { get; set; }
{'fraction_non_alphanumeric': 0.08939292861907938, 'fraction_numerical': 0.0, 'mean_word_length': 2.255700325732899, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1881200', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1431, 'n_words': 442}