Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save phaniav/71dbb438744770b0d2dc108b74e92170 to your computer and use it in GitHub Desktop.

Select an option

Save phaniav/71dbb438744770b0d2dc108b74e92170 to your computer and use it in GitHub Desktop.

Revisions

  1. @kamsar kamsar revised this gist Jan 31, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions ServiceCollectionExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,15 @@ public static void AddMvcControllers(this IServiceCollection serviceCollection,
    {
    serviceCollection.AddTransient(controller);
    }

    // h/t Sean Holmesby and Akshay Sura: this adds Web API controller support
    var apiControllers = GetTypesImplementing<ApiController>(assemblies)
    .Where(controller => controller.Name.EndsWith("Controller", StringComparison.Ordinal));

    foreach (var apiController in apiControllers)
    {
    serviceCollection.AddTransient(apiController);
    }
    }

    public static Type[] GetTypesImplementing<T>(params Assembly[] assemblies)
  2. @kamsar kamsar revised this gist Dec 6, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ServiceCollectionExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    using System.Globalization;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using System.Web.Mvc;
    using Microsoft.Extensions.DependencyInjection;
    @@ -11,6 +12,7 @@ namespace Sitecore.Foundation.DependencyInjection
    {
    public static class ServiceCollectionExtensions
    {
    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    public static void AddMvcControllersInCurrentAssembly(this IServiceCollection serviceCollection)
    {
    AddMvcControllers(serviceCollection, Assembly.GetCallingAssembly());
  3. @kamsar kamsar created this gist Sep 1, 2016.
    96 changes: 96 additions & 0 deletions ServiceCollectionExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Web.Mvc;
    using Microsoft.Extensions.DependencyInjection;

    namespace Sitecore.Foundation.DependencyInjection
    {
    public static class ServiceCollectionExtensions
    {
    public static void AddMvcControllersInCurrentAssembly(this IServiceCollection serviceCollection)
    {
    AddMvcControllers(serviceCollection, Assembly.GetCallingAssembly());
    }

    public static void AddMvcControllers(this IServiceCollection serviceCollection, params string[] assemblyFilters)
    {
    var assemblyNames = new HashSet<string>(assemblyFilters.Where(filter => !filter.Contains('*')));
    var wildcardNames = assemblyFilters.Where(filter => filter.Contains('*')).ToArray();

    var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(assembly =>
    {
    var nameToMatch = assembly.GetName().Name;
    if (assemblyNames.Contains(nameToMatch)) return true;

    return wildcardNames.Any(wildcard => IsWildcardMatch(nameToMatch, wildcard));
    })
    .ToArray();

    AddMvcControllers(serviceCollection, assemblies);
    }

    public static void AddMvcControllers(this IServiceCollection serviceCollection, params Assembly[] assemblies)
    {
    var controllers = GetTypesImplementing<IController>(assemblies)
    .Where(controller => controller.Name.EndsWith("Controller", StringComparison.Ordinal));

    foreach (var controller in controllers)
    {
    serviceCollection.AddTransient(controller);
    }
    }

    public static Type[] GetTypesImplementing<T>(params Assembly[] assemblies)
    {
    if (assemblies == null || assemblies.Length == 0)
    {
    return new Type[0];
    }

    var targetType = typeof(T);

    return assemblies
    .Where(assembly => !assembly.IsDynamic)
    .SelectMany(GetExportedTypes)
    .Where(type => !type.IsAbstract && !type.IsGenericTypeDefinition && targetType.IsAssignableFrom(type))
    .ToArray();
    }

    private static IEnumerable<Type> GetExportedTypes(Assembly assembly)
    {
    try
    {
    return assembly.GetExportedTypes();
    }
    catch (NotSupportedException)
    {
    // A type load exception would typically happen on an Anonymously Hosted DynamicMethods
    // Assembly and it would be safe to skip this exception.
    return Type.EmptyTypes;
    }
    catch (ReflectionTypeLoadException ex)
    {
    // Return the types that could be loaded. Types can contain null values.
    return ex.Types.Where(type => type != null);
    }
    catch (Exception ex)
    {
    // Throw a more descriptive message containing the name of the assembly.
    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
    "Unable to load types from assembly {0}. {1}", assembly.FullName, ex.Message), ex);
    }
    }

    /// <summary>
    /// Checks if a string matches a wildcard argument (using regex)
    /// </summary>
    private static bool IsWildcardMatch(string input, string wildcards)
    {
    return Regex.IsMatch(input, "^" + Regex.Escape(wildcards).Replace("\\*", ".*").Replace("\\?", ".") + "$", RegexOptions.IgnoreCase);
    }
    }
    }