Thursday, April 16, 2020

IEnumerable Batch Extension

Batches the given source into multiple provided size batches.
As we are dealing with IEnumerable and not IList therefore, we need to set the max batches that needs to be created.


 public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, uint size, uint max = byte.MaxValue)
        {
            var enumerator = source?.GetEnumerator();

            if (enumerator == null)
                yield break;

            IEnumerable CreateBatch()
            {
                var batch = size;
                while (batch-- > 0 && enumerator.MoveNext())
                {
                    yield return enumerator.Current;
                }
            }

            while (max-- > 0)
            {
                yield return CreateBatch();
            }
        }


Usage


var batches = GetIEnumerable().Batch(500, 50);


Saturday, August 11, 2018

EPiServer Find Custom Indexing Job For Number of Sites

In one of our episerver projects we had 20 sites with one database. We are using epi find and epi find indexing job would never finish completely. During the indexing job after indexing couple of sites Epi Find indexing job would crash and stop the whole job.

It was becoming difficult to run the whole indexing job in one go. Therefore, I came up with other scheduling job to start from the site at which the job was failed instead of restarting from start.

This is the custom job to start epi find indexing where it left from.


    [ScheduledPlugIn(GUID = JobId, DisplayName = "Epi Find Incremental Indexing Job", DefaultEnabled = true)]
    public class EpiFindIncrementalIndexingJob : ScheduledJobBase
    {
        private const string LineBreak = "
";

        private const string JobId = "7107017B-F443-4E41-893D-62A9FAE69153";

        private readonly ILogger logger = LogManager.GetLogger(typeof(ReIndexNewsJob));

        private bool stopSignaled;

        private IScheduledJobLogRepository scheduledJobLogRepository;

        private ISiteDefinitionRepository siteDefinitionRepository;

        public EpiFindIncrementalIndexingJob()
        {
            IsStoppable = true;
            siteDefinitionRepository = ServiceLocator.Current.GetInstance();
            scheduledJobLogRepository = ServiceLocator.Current.GetInstance();
        }

        public override string Execute()
        {
            var executeDateTime = DateTime.UtcNow;
            using (var locker = DatabaseLock.Lock(Constants.Connections.EPiServerDB, nameof(EpiFindIncrementalIndexingJob), 0))
            {
                if (!locker.HasLock)
                    return "Error: failed to start. Job is already running.";

                // Call OnStatusChanged to periodically notify progress of job for manually started jobs
                OnStatusChanged($"Starting execution of {GetType()}");

                var message = new StringBuilder();
                var current = SiteDefinition.Current;
                var siteDefinitions = siteDefinitionRepository.List().Concat(new SiteDefinition[1] { SiteDefinition.Empty });

                try
                {
                    string getNameOfDefinition(SiteDefinition site) => site == SiteDefinition.Empty ? "Global assets and other data" : site.Name;

                    // Load the cached list of remaining sites to be indexed
                    var keyValue = ApplicationCache.Get(nameof(EpiFindIncrementalIndexingJob));
                    if (keyValue == null)
                    {
                        keyValue = new KeyValueItem()
                        {
                            Key = nameof(EpiFindIncrementalIndexingJob),
                            Updated = DateTime.Now,
                            Type = nameof(EpiFindIncrementalIndexingJob),
                            Value = string.Empty
                        };
                    }

                    var remaining = new HashSet();
                    foreach (var value in keyValue.Value.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                        remaining.Add(value);

                    if (remaining.IsNullOrEmpty())
                        siteDefinitions.ToList().ForEach(s => remaining.Add(s.Id.ToString()));
                    else
                        siteDefinitions = siteDefinitions.Where(s => remaining.Contains(s.Id.ToString()));

                    stopSignaled = false;
                    foreach (SiteDefinition siteDefinition in siteDefinitions)
                    {
                        try
                        {
                            SiteDefinition.Current = siteDefinition;

                            var statusReport = new StringBuilder();

                            // ReIndex the current site (SiteDefinition.Current)
                            ContentIndexer.ReIndexResult reIndexResult = ContentIndexer.Instance.ReIndex(
                                status =>
                                {
                                    if (status.IsError)
                                    {
                                        string errorMessage = status.Message.StripHtml();
                                        if (errorMessage.Length > 0)
                                            statusReport.Append($"{errorMessage}");
                                    }
                                    OnStatusChanged($"Indexing job [{getNameOfDefinition(SiteDefinition.Current)}] [content]: {status.Message.StripHtml()}");
                                },
                                () => stopSignaled);

                             var siteMessage = $"Indexing job [{getNameOfDefinition(SiteDefinition.Current)}] [content]: {reIndexResult.PrintReport().StripHtml().Replace(Environment.NewLine, string.Empty)}{LineBreak}";

                            AddLogEntry(siteMessage);
                            message.Append(siteMessage);

                            if (stopSignaled)
                                return message.Append("Scheduled job was stopped").ToString();

                            if (statusReport.Length > 0)
                                message.Append($"{statusReport.ToString()}{LineBreak}");

                            // Update list of remaining sites to support resuming indexing, rather than restarting
                            remaining.RemoveWhere(s => siteDefinition.Id.ToString().Equals(s));
                            keyValue.Value = string.Join(",", remaining);
                            ApplicationCache.Set(nameof(EpiFindIncrementalIndexingJob), keyValue, Constants.Cache.OneYear);
                        }
                        catch (Exception ex)
                        {
                            message.Append($"{ex.ToString()}{LineBreak}");
                            LogException(ex);
                        }
                    }

                    // HACK: Use reflection to trigger the re-index of external best bets
                    var bestBetType = typeof(EPiServer.Find.Cms.BestBets.BestBetSelectorExtensions).Assembly.GetTypes()
                        .SingleOrDefault(x => x.FullName.EndsWith("BestBets.ExternalUrlBestBetHandlers", StringComparison.InvariantCultureIgnoreCase));

                    if (bestBetType != null)
                    {
                        var reIndexMethod = bestBetType.GetMethod("ReindexExternalUrlBestBets", BindingFlags.Static | BindingFlags.NonPublic);
                        if (reIndexMethod != null)
                        {
                            string result = reIndexMethod.Invoke(null, null)?.ToString();
                            message.Append($"{result}{LineBreak}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    message.Append($"{ex.ToString()}{LineBreak}");
                    LogException(ex);
                }
                finally
                {
                    SiteDefinition.Current = current;
                }

                return message.ToString();
            }
        }

        public override void Stop()
        {
            stopSignaled = true;
        }

        private void AddLogEntry(string message)
        {
            scheduledJobLogRepository.LogAsync(new Guid(JobId), new ScheduledJobLogItem
            {
                CompletedUtc = DateTime.UtcNow,
                Message = message,
                Status = ScheduledJobExecutionStatus.Succeeded,
            }).GetAwaiter().GetResult();
        }

        private void LogException(Exception exception)
        {
            logger.Critical($"Exception occurred in {nameof(EpiFindIncrementalIndexingJob)}", exception);
        }
    }
}



Tuesday, May 8, 2018

Asynchronously Throttling EPiFind Request To Stay under EPiFind Limit On Azure

ThrottledSemaphore


/// <summary>
    /// Throttled semaphore to allow maximum number of request concurrently per time period.
    /// </summary>
    /// <remarks>
    ///     <para>
    ///         In order to allow N no. of request concurrently for given time period <see cref="ThrottledSemaphore"/>
    ///         the caller should call the <see cref="TryLock"/> thorugh <see cref="using"/> as TryLock returns the IDisposable <see cref="ThrottledLock"/>
    ///         which tells caller whether the lock <see cref="ThrottledLock.IsLocked"/> has been taken or not.
    ///     </para>
    ///     <para>
    ///         <code>
    ///            private static ThrottledSemaphore throttledSemaphore = new ThrottledSemaphore();
    ///            using (var @lock = await throttledSemaphore.TryLock().ConfigureAwait(false))
    ///                   {
    ///                     if (@lock.IsLocked)
    ///                        return query.Track().Skip((page - 1) * count).Take(count).GetResult();
    ///                     throw new HttpResponseException((HttpStatusCode)429);
    ///                   }
    ///         </code>
    ///     </para>
    /// </remarks>
    public class ThrottledSemaphore
    {
        private ConcurrentQueue<DateTime> times;

        private SemaphoreSlim semaphore;

        public int MaxConcurrency;

        private TimeSpan Period;

        public ThrottledSemaphore(TimeSpan period, int maxConcurrency = 20)
        {
            Period = period;
            MaxConcurrency = maxConcurrency;
            semaphore = new SemaphoreSlim(maxConcurrency, maxConcurrency);
            times = new ConcurrentQueue<DateTime>();
        }

        public async Task<ThrottledLock> TryLock(int timeoutMilliseconds = 500)
        {
            if (await semaphore.WaitAsync(timeoutMilliseconds).ConfigureAwait(false))
            {
                await Wait().ConfigureAwait(false);

                return new ThrottledLock(ReleaseLock) { IsLocked = true };
            }

            return new ThrottledLock(null);
        }

        private void ReleaseLock()
        {
            lock (semaphore)
            {
                times.Enqueue(DateTime.UtcNow);
                semaphore.Release();
            }
        }

        private async Task Wait()
        {
            var now = DateTime.UtcNow;
            var lastTime = DateTime.MinValue;

            if (times.Count >= MaxConcurrency)
                times.TryDequeue(out lastTime);

            var until = lastTime.Add(Period);
            if (MaxConcurrency - semaphore.CurrentCount + times.Count >= MaxConcurrency && until > now)
            {
                await Task.Delay(until - now).ConfigureAwait(false);
            }
        }

        /// <summary>
        /// The disposable releaser tasked with releasing the semaphore.
        /// </summary>
        public sealed class ThrottledLock : IDisposable
        {
            /// <summary>
            /// A value indicating whether this instance of the given entity has been disposed.
            /// </summary>
            /// <value><see langword="true"/> if this instance has been disposed; otherwise, <see langword="false"/>.</value>
            /// <remarks>
            /// If the entity is disposed, it must not be disposed a second
            /// time. The isDisposed field is set the first time the entity
            /// is disposed. If the isDisposed field is true, then the Dispose()
            /// method will not dispose again. This help not to prolong the entity's
            /// life in the Garbage Collector.
            /// </remarks>
            private bool isDisposed;

            public bool IsLocked { get; set; }

            public delegate void TaskDisposeCallBack();

            /// <summary>
            /// Task dispose call back to release or dispose any resources requried for this runner.
            /// </summary>
            private TaskDisposeCallBack taskDisposeCallBack;

            public ThrottledLock(TaskDisposeCallBack callBack)
            {
                taskDisposeCallBack = callBack;
            }

            /// <summary>
            /// Finalizes an instance of the <see cref="ThrottledLock"/> class.
            /// </summary>
            ~ThrottledLock()
            {
                // Do not re-create Dispose clean-up code here.
                // Calling Dispose(false) is optimal in terms of
                // readability and maintainability.
                this.Dispose(false);
            }

            /// <summary>
            /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
            /// </summary>
            public void Dispose()
            {
                this.Dispose(true);

                // This object will be cleaned up by the Dispose method.
                // Therefore, you should call GC.SuppressFinalize to
                // take this object off the finalization queue
                // and prevent finalization code for this object
                // from executing a second time.
                GC.SuppressFinalize(this);
            }

            /// <summary>
            /// Disposes the object and frees resources for the Garbage Collector.
            /// </summary>
            /// <param name="disposing">
            /// If true, the object gets disposed.
            /// </param>
            private void Dispose(bool disposing)
            {
                if (this.isDisposed)
                {
                    return;
                }

                if (disposing)
                {
                    taskDisposeCallBack?.Invoke();
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // Note disposing is done.
                this.isDisposed = true;
            }
        }
    }



Content Search Service/Usage


public static class ContentSearchService
    {
        private static IClient client = SearchClient.Instance;

        private static ThrottledSemaphore searchThrottle;

        static ContentSearchService()
        {
            // Throttle the user search requests to to avoid exceeding our allowed requests/sec to Episerver Find
            searchThrottle = new ThrottledSemaphore(
                period: TimeSpan.FromSeconds(1),
                maxConcurrency: CalculateSearchConcurrency(ServerStateEvent.GetServerCount()));
            ServerStateEvent.OnServerCountChanged += count => searchThrottle.MaxConcurrency = CalculateSearchConcurrency(count);
        }
  
 public static async Task<UnifiedSearchResults> GetSearchResults(string keyword, IList<Type> types = null, SortBy sort = SortBy.Relevance, DateTime? before = null, int page = 1, int count = 24)
        {
            var query = client.UnifiedSearch().For(keyword) as ITypeSearch<ISearchContent>;

            // Filter for specific page types
            if (!types.IsNullOrEmpty())
                query = query.FilterByExactTypes(types);

            // Filter before a specific date time
            if (before.HasValue)
                query = query.Filter(t => t.SearchPublishDate.Before(before.Value.RoundToMinute(RoundingDirection.Floor)));

            switch (sort)
            {
                case SortBy.Earliest:
                    query = query.OrderBy(o => o.SearchPublishDate);
                    break;
                case SortBy.Latest:
                    query = query.OrderByDescending(o => o.SearchPublishDate);
                    break;
                default:
                    query = query
                        .BoostMatching(s => s.SearchTitle.Match(keyword), 2)
                        .BoostMatching(s => s.SearchSection.Match(keyword), 1.5);
                    break;
            }

            using (var @lock = await searchThrottle.TryLock().ConfigureAwait(false))
            {
                if (@lock.IsLocked)
                    return query.Track().Skip((page - 1) * count).Take(count).GetResult();

                throw new HttpResponseException((HttpStatusCode)429);
            }
        }

        private static int CalculateSearchConcurrency(int serverCount)
        {
            // Divide the maximum concurrent user searches between the active servers (excluding the CMS admin server).
            return serverCount > 1 ? (Settings.Search.MaxConcurrency / (serverCount - 1)) : 1;
        }
    }



Monitoring Azure Servers Turning On and Shutting down


 public static class ServerStateEvent
    {
        // HACK: Guid copied from EPiServer.Events.Clients.Internal.ServerStateService.StateEventId and may be subject to change without notice
        private static readonly Guid EventId = new Guid("{51da5053-6af8-4a10-9bd4-8417e48f38bd}");

        private static readonly Guid RaiserId = Guid.NewGuid();

        private static Event serverEvent;

        private static ILogger logger = LogManager.GetLogger(typeof(ServerStateEvent));

        private static IServerStateService stateService = LicensingServices.Instance.GetService<IServerStateService>();

        private static int? serverCount = null;

        /// <summary>
        /// Event occurs when there has been a change to the number of server instances running in the application
        /// </summary>
        public static event Action<int> OnServerCountChanged;

        public static void Register()
        {
            var registry = ServiceLocator.Current.GetInstance<IEventRegistry>();
            serverEvent = registry.Get(EventId);
            serverEvent.Raised += OnRaised;
        }

        public static int GetServerCount()
        {
            if (serverCount == null)
                serverCount = stateService.ActiveServers();

            return serverCount.Value;
        }

        private static void OnRaised(object sender, EventNotificationEventArgs args)
        {
            if (args.Param is StateMessage message && (message.Type == StateMessageType.Hello || message.Type == StateMessageType.Bye))
            {
                // TODO: Remove when logging is removed
                var serverCountBefore = serverCount;

                // Calculate the new server count and fire the change event
                serverCount = stateService.ActiveServers();
                OnServerCountChanged?.Invoke(serverCount.Value);

                // TODO: Remove logging when confident that event is firing when needed
                logger.Information($"A ServerStateEvent was raised. Application: {message.ApplicationName} | Type: {message.Type} | Servers Before: {serverCountBefore} | Servers After: {serverCount}");
            }
        }
    }




 [InitializableModule]
    [ModuleDependency(typeof(EventsInitialization))]
    public class ServerStateConfig : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            ServerStateEvent.Register();
        }

        public void Uninitialize(InitializationEngine context) { }
    }


Sunday, May 6, 2018

FIFO Semaphore

In one of our project requirement we had to built the max concurrency requirement for which we decided to use Semaphore but we had requirement to build FIFO Semaphore as by default Semaphore does not guarantee FIFO access to waiting thread.

Therefore, I have use ConcurrentQueue together with Semaphore to come with FIFO Sempahore




using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Shared.Locking
{
    public class ThreadPoolAsyncLock
    {
        private ConcurrentQueue<(SemaphoreSlim Semaphore, DateTime Time)> queue = new ConcurrentQueue<(SemaphoreSlim, DateTime)>();

        private (DateTime start, int requests) requestTime = (DateTime.UtcNow, 0);

        private SemaphoreSlim semaphoreSlims;

        public static TimeSpan Timeout = TimeSpan.FromMilliseconds(500);

        public static int MaxRequest = 30;

        public ThreadPoolAsyncLock(int maxThread = 30)
        {
            semaphoreSlims = new SemaphoreSlim(maxThread, maxThread);
        }

        public async Task<TaskRunner<TResult>> ScheduleTask<TResult>()
        {
            /* This lock will make sure no one enter in main semaphore unless some one from main semaphore releases this */
            await AcquireOrQueue().WaitAsync(Timeout).ConfigureAwait(false);

            /* Main semaphore wait */
            if (await semaphoreSlims.WaitAsync(Timeout).ConfigureAwait(false))
               {
                 new TaskRunner<TResult>(ReleaseLock) { IsLocked = true };
               }


            return new TaskRunner<TResult>(ReleaseLock);
        }

        private SemaphoreSlim AcquireOrQueue()
        {
            SemaphoreSlim slim = null;
            lock (semaphoreSlims)
            {
                if (semaphoreSlims.CurrentCount > 0 && (DateTime.UtcNow.Subtract(requestTime.start).Seconds > 1 || requestTime.requests < MaxRequest))
                {
                    slim = new SemaphoreSlim(1, 1);
                    Interlocked.Increment(ref requestTime.requests);
                }
                else
                {
                    slim = new SemaphoreSlim(0, 1);
                    queue.Enqueue((slim, DateTime.UtcNow));
                }
            }

            return slim;
        }

        /// <summary>
        /// Dequeue the thread waiting to enter main semaphore
        /// </summary>
        /// <remarks>
        /// It only allows the dequeue if the main semaphore is not full Or Max requests hasn't been reached within last second.
        /// </remarks>
        private void DeQueue()
        {
            lock (semaphoreSlims)
            {
                if (semaphoreSlims.CurrentCount == 0 || (requestTime.requests >= MaxRequest && DateTime.UtcNow.Subtract(requestTime.start).Seconds <= 1))
                    return;

                Interlocked.Decrement(ref requestTime.requests);
                if (queue.TryDequeue(out var semaphore))
                {
                    semaphore.Semaphore.Release();
                    requestTime.start = semaphore.Time;
                }
            }
        }

        private void ReleaseLock()
        {
            lock (semaphoreSlims)
            {
                semaphoreSlims.Release();
                DeQueue();
            }
        }

        /// <summary>
        /// The disposable releaser tasked with releasing the semaphore.
        /// </summary>
        public sealed class TaskRunner<TResult> : IDisposable
        {
            /// <summary>
            /// A value indicating whether this instance of the given entity has been disposed.
            /// </summary>
            /// <value><see langword="true"/> if this instance has been disposed; otherwise, <see langword="false"/>.</value>
            /// <remarks>
            /// If the entity is disposed, it must not be disposed a second
            /// time. The isDisposed field is set the first time the entity
            /// is disposed. If the isDisposed field is true, then the Dispose()
            /// method will not dispose again. This help not to prolong the entity's
            /// life in the Garbage Collector.
            /// </remarks>
            private bool isDisposed;

            
            public bool IsLocked { get; set; }

            /// <summary>
            /// A Task to run after acquiring and locking the thread.
            /// </summary>
            /// <remarks>
            /// If the entity acquires the lock, then entity shoule call <see cref="RunAsycn" /> or <see cref="Run"/> to run the task />
            /// </remarks>
            private Task<TResult> task;

            public delegate void TaskDisposeCallBack();

            /// <summary>
            /// Task dispose call back to release or dispose any resources requried for this runner.
            /// </summary>
            private TaskDisposeCallBack taskDisposeCallBack;

            public TaskRunner(TaskDisposeCallBack callBack)
            {
                taskDisposeCallBack = callBack;
            }

            /// <summary>
            /// Finalizes an instance of the <see cref="TaskRunner{TResult}"/> class.
            /// </summary>
            ~TaskRunner()
            {
                // Do not re-create Dispose clean-up code here.
                // Calling Dispose(false) is optimal in terms of
                // readability and maintainability.
                this.Dispose(false);
            }

            /// <summary>
            /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
            /// </summary>
            public void Dispose()
            {
                this.Dispose(true);

                // This object will be cleaned up by the Dispose method.
                // Therefore, you should call GC.SuppressFinalize to
                // take this object off the finalization queue
                // and prevent finalization code for this object
                // from executing a second time.
                GC.SuppressFinalize(this);
            }

            /// <summary>
            /// Disposes the object and frees resources for the Garbage Collector.
            /// </summary>
            /// <param name="disposing">
            /// If true, the object gets disposed.
            /// </param>
            private void Dispose(bool disposing)
            {
                if (this.isDisposed)
                {
                    return;
                }

                if (disposing)
                {
                    taskDisposeCallBack?.Invoke();
                }

                // Call the appropriate methods to clean up
                // unmanaged resources here.
                // Note disposing is done.
                this.isDisposed = true;
            }
        }
    }
}


Usage


    using (var @lock = await new ThreadPoolAsyncLock().ScheduledTask().ConfigureAwait(false))
            {
                if (@lock.IsLocked)
                    return query.Track().Skip((page - 1) * count).Take(count).GetResult();

                throw new HttpResponseException((HttpStatusCode)429);
            }


EPiFind Whole Word Matching

By Default EPiFind tokenizes the query word. If the word is composed of multiple words and we need to search the whole word instead of tokenized words  then its bit tricky. We found it bit difficult initially to search for whole given word.

At the end we figure out to use the regex to specifically tell EPiFind to match whole word instead of tokenizing.


query = query.Search(x => x.For(legacyVideoId, q =>
      {
        q.Query = $"*{legacyVideoId}*";
      }).InField(a => a.LegacyVideoId));

Tuesday, April 17, 2018

Web API Default Behaviour Overriding

Overriding Text Encoding

In one of our project we had an issue where one of invalid text character in web api was throwing exception instead of removing or replacing that bad character. After investigating we figure out that the default Encoding was UTF8 but EncoderFallback was set to throw Exception for invalid character instead of replacing with default Character.

Therefore, we just added this one below line to replace the text encoding behaviour

            config.Formatters.JsonFormatter.SupportedEncodings[0] = Encoding.UTF8;

The default system UTF8 Encoding fallback set to replace invalid character with "?"

public static void ConfigureWebApi(HttpConfiguration config)
        {
            ...
            ...

        // HACK: To allow JSON serialization to handle invalid characters
            config.Formatters.JsonFormatter.SupportedEncodings[0] = Encoding.UTF8;
            config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
            config.Services.Replace(typeof(IExceptionLogger), new GlobalExceptionLogger());
        }

Overriding GlobalExceptionHandler

We are using Azure Telemetry to log all of our errors therefore, we wanted to override the global exception handler to log all errors to telemetry. This is similar concept of Global.ascx function for web api.

the below line will replace the GlobalExceptionHandler for WebApi

            config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());


   public class GlobalExceptionHandler : ExceptionHandler
    {
        public override void Handle(ExceptionHandlerContext context)
        {
            var exception = context.Exception;
            var message = context.Exception.GetType().Name + ": " + context.Exception.Message;

            if (exception is HttpException httpException)
            {
                context.Result = new CustomErrorResult(context.Request, (HttpStatusCode)httpException.GetHttpCode(), message);
                return;
            }

            // Return HttpStatusCode for other types of exception.
            context.Result = new CustomErrorResult(context.Request, HttpStatusCode.BadRequest, message);
        }
    }
 


public class CustomErrorResult : IHttpActionResult
    {
        private readonly string _errorMessage;
        private readonly HttpRequestMessage _requestMessage;
        private readonly HttpStatusCode _statusCode;

        public CustomErrorResult(HttpRequestMessage requestMessage, HttpStatusCode statusCode, string errorMessage)
        {
            _requestMessage = requestMessage;
            _statusCode = statusCode;
            _errorMessage = errorMessage;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            return Task.FromResult(_requestMessage.CreateErrorResponse(_statusCode, _errorMessage));
        }
    }
  


Overriding GlobalExceptionLogger

We also wanted to replace the GlobalExceptionLogger to be our ExceptionLogger to push all errors to Azure Telemetry.

            config.Services.Replace(typeof(IExceptionLogger), new GlobalExceptionLogger());


public class GlobalExceptionLogger : ExceptionLogger
    {
        public override void Log(ExceptionLoggerContext context)
        {
            new TelemetryClient().TrackException(context.Exception);
        }
    }
 

Tuesday, December 19, 2017

Azure Storage Account File Storage Wrapper For Basic Functions

In one of our project we had to upload/download files to azure file storage.
Therefore, I build this small wrapper classes to make use of Azure file storage easy.

AzureStorageAccountClient

This client is supposed to provide function to connect to different storage account such as (Blob, File and etc storage)


    public class AzureStorageAccountClient
    {
        private readonly ILogger logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public AzureStorageAccountClient(string connectionString)
        {
            ConnectionString = connectionString;
        }

        public string ConnectionString { get; }

        public IAzureStorageShareClient FileClientConnect()
        {
            string message = string.Empty;
            try
            {
                return new AzureStorageShareClient(CloudStorageAccount.Parse(ConnectionString).CreateCloudFileClient());
            }
            catch (Exception ex)
            {
                logger.Critical("Error in Azure Client", ex);
                throw;
            }
        }
    }


AzureStorageShareClient

This class provides functions to perform on share storage client.


   public interface IAzureStorageShareClient
    {
        CloudFileClient CloudFileClient { get; }
    }

    public class AzureStorageShareClient : IAzureStorageShareClient
    {
        public AzureStorageShareClient(CloudFileClient cloudFileClient)
        {
            CloudFileClient = cloudFileClient;
        }

        public CloudFileClient CloudFileClient { get; }
    }   



AzureStorageFileDirectoryClient

This class provides functionality to different Directoy related operations.


    public interface IAzureStorageFileDirectoryClient
    {
        CloudFileShare CloudFileShare { get; }

        CloudFileDirectory CloudFileDirectory { get; set; }
    }

    public class AzureStorageFileDirectoryClient : IAzureStorageFileDirectoryClient
    {
        public AzureStorageFileDirectoryClient(CloudFileShare cloudFileShare)
        {
            CloudFileShare = cloudFileShare;
        }

        public CloudFileShare CloudFileShare { get; }

        public CloudFileDirectory CloudFileDirectory { get; set; }
    }


AzureStorageFileClient

This class provides functions to different File related operations


    public interface IAzureStorageFileClient
    {
        CloudFileDirectory CloudFileDirectory { get; set; }

        CloudFile CloudFile { get; }
    }

    public class AzureStorageFileClient : IAzureStorageFileClient
    {
        public AzureStorageFileClient(CloudFile cloudFile)
        {
            CloudFile = cloudFile;
        }

        public CloudFileDirectory CloudFileDirectory { get; set; }

        public CloudFile CloudFile { get; }
    }



As you have noted none of the above classes have any functions.
Below is the extension class that provides functions available to those interfaces.

AzureStorageAccountExtensions


    public static class AzureStorageAccountExtensions
    {
        public static async Task GetOrAddShare(this IAzureStorageShareClient client, string shareName)
        {
            var cloudFileShare = client.CloudFileClient.GetShareReference(shareName);
            await cloudFileShare.CreateIfNotExistsAsync().ConfigureAwait(false);
            return new AzureStorageFileDirectoryClient(cloudFileShare);
        }

        public static async Task GetOrAddFolder(this IAzureStorageFileDirectoryClient client, string folderPath)
        {
            var cloudRootDirectory = client.CloudFileShare.GetRootDirectoryReference();
            var cloudFileDirectory = cloudRootDirectory.GetDirectoryReference(folderPath);
            await cloudFileDirectory.CreateIfNotExistsAsync().ConfigureAwait(false);
            client.CloudFileDirectory = cloudFileDirectory;
            return client;
        }

        public static IEnumerable ListFiles(this IAzureStorageFileDirectoryClient client)
        {
            return client.CloudFileDirectory.ListFilesAndDirectories().Where(f => !f.Uri.LocalPath.Contains(".folder"));
        }

        public static async Task AddFile(this IAzureStorageFileDirectoryClient client, string fileName, long length)
        {
            await client.CloudFileDirectory.CreateIfNotExistsAsync().ConfigureAwait(false);

            var cloudFile = client.CloudFileDirectory.GetFileReference(fileName);
            if (!await cloudFile.ExistsAsync().ConfigureAwait(false))
            {
                await cloudFile.CreateAsync(length).ConfigureAwait(false);
            }

            return new AzureStorageFileClient(cloudFile);
        }

        public static async Task GetFile(this IAzureStorageFileDirectoryClient client, string fileName)
        {
            var cloudFile = client.CloudFileDirectory.GetFileReference(fileName);
            if (await cloudFile.ExistsAsync().ConfigureAwait(false))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    await cloudFile.DownloadToStreamAsync(ms).ConfigureAwait(false);
                    return ms.ToArray();
                }
            }

            return null;
        }

        public static async void UploadFile(this IAzureStorageFileClient client, byte[] data)
        {
            await client.CloudFile.UploadFromByteArrayAsync(data, 0, data.Length).ConfigureAwait(false);
        }
    }