<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Register Services (Dependency Injection) in Revit API Forum</title>
    <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11439503#M16671</link>
    <description>&lt;P&gt;As far as I can tell, there is nothing like that in Revit. If you want some sort of dependency injection container, you'll need to write your own or use a 3rd party library. Revit uses the&amp;nbsp;Castle.Windsor library so if you want to use that you'll need to use the same version that Revit ships with (3.2.1.20 for Revit 2020 and 2023).&lt;/P&gt;</description>
    <pubDate>Thu, 22 Sep 2022 17:27:43 GMT</pubDate>
    <dc:creator>mhannonQ65N2</dc:creator>
    <dc:date>2022-09-22T17:27:43Z</dc:date>
    <item>
      <title>Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11438658#M16670</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm looking for a way to register services such as a Logger with Revit so that it can be called later on using Dependency Injection.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've previously worked with ASP.NET applications with the usual Startup.cs and IHost but can't seem to find the IServicesCollection through the IExternalApplication interface.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone provide some suggestions?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Jack&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2022 12:33:01 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11438658#M16670</guid>
      <dc:creator>jmadeley37KVE</dc:creator>
      <dc:date>2022-09-22T12:33:01Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11439503#M16671</link>
      <description>&lt;P&gt;As far as I can tell, there is nothing like that in Revit. If you want some sort of dependency injection container, you'll need to write your own or use a 3rd party library. Revit uses the&amp;nbsp;Castle.Windsor library so if you want to use that you'll need to use the same version that Revit ships with (3.2.1.20 for Revit 2020 and 2023).&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2022 17:27:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11439503#M16671</guid>
      <dc:creator>mhannonQ65N2</dc:creator>
      <dc:date>2022-09-22T17:27:43Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11439918#M16672</link>
      <description>&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'll take a look around in that case and see if I can engineer a solution.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I come up with anything I'll be sure to post back here.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Sep 2022 20:27:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11439918#M16672</guid>
      <dc:creator>jmadeley37KVE</dc:creator>
      <dc:date>2022-09-22T20:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11440470#M16673</link>
      <description>&lt;P&gt;I use a self-implemented DI container to host services in my application and it works completely fine.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 05:03:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11440470#M16673</guid>
      <dc:creator>Kennan.Chen</dc:creator>
      <dc:date>2022-09-23T05:03:15Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11440706#M16674</link>
      <description>&lt;P&gt;Hi Kennan,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you be able to provide a high-level overview of how you have structured your solution?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you using external libraries or Microsoft's own implementation of dependency injection?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Jack&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 08:08:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11440706#M16674</guid>
      <dc:creator>jmadeley37KVE</dc:creator>
      <dc:date>2022-09-23T08:08:20Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11441765#M16675</link>
      <description>&lt;P&gt;The simple definition of my container, which is an extended interface based on the System.IServiceProvider&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// &amp;lt;summary&amp;gt;
///     Use to manage instances and the creation of instances
/// &amp;lt;/summary&amp;gt;
public interface IContainer : IDisposable, IServiceProvider
{
    #region Others

    /// &amp;lt;summary&amp;gt;
    ///     Get an instance of a specific service
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TService"&amp;gt;The type of the service&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;returns&amp;gt;The instance of the service&amp;lt;/returns&amp;gt;
    TService GetService&amp;lt;TService&amp;gt;();

    /// &amp;lt;summary&amp;gt;
    ///     Inject the instance properties from the &amp;lt;see cref="IContainer" /&amp;gt;
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="instance"&amp;gt;The instance to inject&amp;lt;/param&amp;gt;
    void InjectProperties(object instance);

    /// &amp;lt;summary&amp;gt;
    ///     Register the type of the service to the container
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TService"&amp;gt;The type of the service&amp;lt;/typeparam&amp;gt;
    void Register&amp;lt;TService&amp;gt;();

    /// &amp;lt;summary&amp;gt;
    ///     Register the type of the service to the container with the implementation type
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TService"&amp;gt;The type of the service&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;typeparam name="TImpl"&amp;gt;The implementation type&amp;lt;/typeparam&amp;gt;
    void Register&amp;lt;TService, TImpl&amp;gt;() where TImpl : TService;

    /// &amp;lt;summary&amp;gt;
    ///     Register the type of the service to the container with a delegate to create the instance
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;typeparam name="TService"&amp;gt;The type of the service&amp;lt;/typeparam&amp;gt;
    /// &amp;lt;param name="instanceCreator"&amp;gt;The delegate to create the service instance&amp;lt;/param&amp;gt;
    void Register&amp;lt;TService&amp;gt;(Func&amp;lt;TService&amp;gt; instanceCreator);

    #endregion
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The simple implementation of IContainer which is a singleton&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public class SimpleContainer : IContainer
{
    #region Constructors

    static SimpleContainer()
    {
        Container = new SimpleContainer();
    }

    private SimpleContainer()
    {
        this.Singleton&amp;lt;IContainer&amp;gt;(this);
    }

    #endregion

    #region Properties

    public static IContainer Container { get; }

    private static ConcurrentDictionary&amp;lt;Type, IEnumerable&amp;lt;PropertyInfo&amp;gt;&amp;gt; InjectableProperties { get; } =
        new ConcurrentDictionary&amp;lt;Type, IEnumerable&amp;lt;PropertyInfo&amp;gt;&amp;gt;();

    private Dictionary&amp;lt;Type, Func&amp;lt;object&amp;gt;&amp;gt; Registrations { get; } = new Dictionary&amp;lt;Type, Func&amp;lt;object&amp;gt;&amp;gt;();

    #endregion

    #region Interface Implementations

    public T GetService&amp;lt;T&amp;gt;()
    {
        return (T) GetService(typeof(T));
    }

    /// &amp;lt;summary&amp;gt;
    ///     Inject the properties attributed with InjectAttribute
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="instance"&amp;gt;The instance&amp;lt;/param&amp;gt;
    public void InjectProperties(object instance)
    {
        if (null == instance) return;
        GetInjectableProperties(instance)
            .ToList()
            .ForEach(property =&amp;gt; { property.SetValue(instance, GetService(property.PropertyType), null); });
    }

    public void Register&amp;lt;TService&amp;gt;()
    {
        Register&amp;lt;TService, TService&amp;gt;();
    }

    public void Register&amp;lt;TService, TImpl&amp;gt;() where TImpl : TService
    {
        InternalRegister&amp;lt;TService&amp;gt;(() =&amp;gt;
        {
            var implType = typeof(TImpl);
            return typeof(TService) == implType
                        ? this.CreateInstance(implType)
                        : GetService(implType);
        });
    }

    public void Register&amp;lt;TService&amp;gt;(Func&amp;lt;TService&amp;gt; instanceCreator)
    {
        InternalRegister&amp;lt;TService&amp;gt;(() =&amp;gt; instanceCreator());
    }

    public void Dispose()
    {
        Registrations.Clear();
    }

    public object GetService(Type serviceType)
    {
        if (Registrations.TryGetValue(serviceType, out var creator))
        {
            return creator();
        }

        if (!serviceType.IsAbstract)
        {
            return this.CreateInstance(serviceType);
        }

        throw new Exception($"No registration found for service : {serviceType}");
    }

    #endregion

    #region Others

    private static IEnumerable&amp;lt;PropertyInfo&amp;gt; GetInjectableProperties(object obj)
    {
        var type = obj.GetType();
        var properties = InjectableProperties.GetOrAdd(type,
            t =&amp;gt;
            {
                return t.GetProperties(BindingFlags.Instance
                                        | BindingFlags.SetProperty
                                        | BindingFlags.NonPublic
                                        | BindingFlags.Public)
                        .Where(property =&amp;gt;
                                property.GetCustomAttributes(typeof(InjectAttribute), false).Any())
                        .Where(property =&amp;gt; property.GetValue(obj, null) == null).ToList();
            });
        return properties;
    }

    private void InternalRegister&amp;lt;TService&amp;gt;(Func&amp;lt;object&amp;gt; creator)
    {
        var key = typeof(TService);
        if (Registrations.ContainsKey(key))
            Registrations[key] = creator;
        else
            Registrations.Add(typeof(TService), creator);
    }

    #endregion
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And then I created a base class for all the objects who wants to consume the DI ability of the container&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// &amp;lt;summary&amp;gt;
///     A base object which can automatically inject properties attributed with &amp;lt;see cref="InjectAttribute" /&amp;gt; from the
///     &amp;lt;see cref="IContainer" /&amp;gt;
/// &amp;lt;/summary&amp;gt;
public abstract class ContainerObject
{
    #region Constructors

    // ReSharper disable once UnusedMember.Global
    protected ContainerObject()
    {
        InjectProperties();
    }

    #endregion

    #region Properties

    protected IContainer Container =&amp;gt; SimpleContainer.Container;

    #endregion

    #region Others

    protected void InjectProperties()
    {
        Container?.InjectProperties(this);
    }

    #endregion
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Of course with the help of InjectAttribute, I can inject service automatically&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;/// &amp;lt;summary&amp;gt;
///     Inject an object to a property from the &amp;lt;see cref="IContainer" /&amp;gt;, the property should always have a setter
/// &amp;lt;/summary&amp;gt;
[AttributeUsage(AttributeTargets.Property)]
public class InjectAttribute : Attribute
{
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To better register services to the container, I created some extension methods around IContainer&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class ContainerExtensions
{
    #region Others

    public static IContainer Singleton&amp;lt;TService&amp;gt;(this IContainer container, TService instance)
    {
        container.Register(() =&amp;gt; instance);
        return container;
    }

    public static IContainer Singleton&amp;lt;TService&amp;gt;(this IContainer container, Func&amp;lt;TService&amp;gt; instanceCreator)
    {
        var lazy = new Lazy&amp;lt;TService&amp;gt;(instanceCreator);
        container.Register(() =&amp;gt; lazy.Value);
        return container;
    }

    public static IContainer Singleton&amp;lt;TService, TImpl&amp;gt;(this IContainer container)
        where TImpl : TService
    {
        return container.Singleton&amp;lt;TService, TImpl&amp;gt;(() =&amp;gt;
            typeof(TService) == typeof(TImpl) ? container.CreateInstance&amp;lt;TImpl&amp;gt;() : container.GetService&amp;lt;TImpl&amp;gt;());
    }

    public static IContainer Singleton&amp;lt;TService, TImpl&amp;gt;(this IContainer container, Func&amp;lt;TImpl&amp;gt; instanceCreator)
        where TImpl : TService
    {
        var lazy = new Lazy&amp;lt;TImpl&amp;gt;(instanceCreator);
        container.Register&amp;lt;TService&amp;gt;(() =&amp;gt; lazy.Value);
        return container;
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TImpl&amp;gt;(this IContainer container)
        where TImpl : TService1, TService2
    {
        var implType = typeof(TImpl);
        return container.Singleton&amp;lt;TService1, TService2, TImpl&amp;gt;(() =&amp;gt; typeof(TService1) == implType ||
                                                                        typeof(TService2) == implType
                                                                            ? container.CreateInstance&amp;lt;TImpl&amp;gt;()
                                                                            : container.GetService&amp;lt;TImpl&amp;gt;());
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TImpl&amp;gt;(this IContainer container, Func&amp;lt;TImpl&amp;gt; instanceCreator)
        where TImpl : TService1, TService2
    {
        var lazy = new Lazy&amp;lt;TImpl&amp;gt;(instanceCreator);
        container.Register&amp;lt;TService1&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService2&amp;gt;(() =&amp;gt; lazy.Value);
        return container;
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TService3, TImpl&amp;gt;(this IContainer container)
        where TImpl : TService1, TService2, TService3
    {
        var implType = typeof(TImpl);
        return container.Singleton&amp;lt;TService1, TService2, TImpl&amp;gt;(() =&amp;gt; typeof(TService1) == implType ||
                                                                        typeof(TService2) == implType ||
                                                                        typeof(TService3) == implType
                                                                            ? container.CreateInstance&amp;lt;TImpl&amp;gt;()
                                                                            : container.GetService&amp;lt;TImpl&amp;gt;());
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TService3, TImpl&amp;gt;(this IContainer container, Func&amp;lt;TImpl&amp;gt; instanceCreator)
        where TImpl : TService1, TService2, TService3
    {
        var lazy = new Lazy&amp;lt;TImpl&amp;gt;(instanceCreator);
        container.Register&amp;lt;TService1&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService2&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService3&amp;gt;(() =&amp;gt; lazy.Value);
        return container;
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TService3, TService4, TImpl&amp;gt;(this IContainer container)
        where TImpl : TService1, TService2, TService3, TService4
    {
        var implType = typeof(TImpl);
        return container.Singleton&amp;lt;TService1, TService2, TImpl&amp;gt;(() =&amp;gt; typeof(TService1) == implType ||
                                                                        typeof(TService2) == implType ||
                                                                        typeof(TService3) == implType ||
                                                                        typeof(TService4) == implType
                                                                            ? container.CreateInstance&amp;lt;TImpl&amp;gt;()
                                                                            : container.GetService&amp;lt;TImpl&amp;gt;());
    }

    public static IContainer Singleton&amp;lt;TService1, TService2, TService3, TService4, TImpl&amp;gt;(this IContainer container, Func&amp;lt;TImpl&amp;gt; instanceCreator)
        where TImpl : TService1, TService2, TService3, TService4
    {
        var lazy = new Lazy&amp;lt;TImpl&amp;gt;(instanceCreator);
        container.Register&amp;lt;TService1&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService2&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService3&amp;gt;(() =&amp;gt; lazy.Value);
        container.Register&amp;lt;TService4&amp;gt;(() =&amp;gt; lazy.Value);
        return container;
    }

    #endregion
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And also an extension around System.IServiceProvider to create service instances&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public static class ServiceProviderExtensions
{
    #region Others

    /// &amp;lt;summary&amp;gt;
    ///     Create the instance of the implementation type, inject all the constructor parameters and inject properties
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="container"&amp;gt;The instance of &amp;lt;see cref="IServiceProvider" /&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="implementationType"&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
    public static object CreateInstance(this IServiceProvider container, Type implementationType)
    {
        var ctor = implementationType
                    .GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance |
                                    BindingFlags.Instance).OrderByDescending(x =&amp;gt; x.GetParameters().Length).First();
        var parameterTypes = ctor.GetParameters().Select(p =&amp;gt; p.ParameterType);
        var dependencies   = parameterTypes.Select(container.GetService).ToArray();
        var instance = Activator.CreateInstance(implementationType,
            BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance |
            BindingFlags.Instance, null, dependencies, CultureInfo.CurrentCulture);
        return instance;
    }

    public static T CreateInstance&amp;lt;T&amp;gt;(this IServiceProvider container)
    {
        return (T) container.CreateInstance(typeof(T));
    }

    public static T GetService&amp;lt;T&amp;gt;(this IServiceProvider container)
    {
        var service = container.GetService(typeof(T));
        return (T) service;
    }

    /// &amp;lt;summary&amp;gt;
    ///     Inject all the injectable properties
    /// &amp;lt;/summary&amp;gt;
    /// &amp;lt;param name="container"&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="instance"&amp;gt;&amp;lt;/param&amp;gt;
    /// &amp;lt;param name="selector"&amp;gt;&amp;lt;/param&amp;gt;
    internal static void InjectProperties(this IServiceProvider container, object instance, Func&amp;lt;PropertyInfo, bool&amp;gt; selector)
    {
        if (null == instance) return;
        try
        {
            instance.GetType().GetProperties().Where(selector).ToList()
                    .ForEach(property =&amp;gt;
                            property.SetValue(instance, container.GetService(property.PropertyType), null));
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    #endregion
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now I can write a service and register it on App starts up.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public interface IMyServiceA
{
  void Method();
}

public class MyServiceA: IMyServiceA
{
  void Method()
  {

  }
}

public class App: ContainerObject, IExternalApplication
{
  public Result OnShutdown(UIControlledApplication application)
  {
      try
      {
        // do some cleaning
        return Result.Succeeded;
      }
      catch (Exception e)
      {
          return Result.Failed;
      }
  }

  public Result OnStartup(UIControlledApplication application)
  {
      try
      {
        this.Container.Singleton&amp;lt;IMyServiceA, MyServiceA&amp;gt;()
      }
      catch (Exception e)
      {
          MessageBox.Show(e.ToString(), @"Oops!");
      }

      return Result.Succeeded;
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In another service I can consume a registered service like this(property injection and constructor injection)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public interface IMyServiceB
{
  void Method();
}

// property injection
public class MyServiceB: ContainerObject, IMyServiceB
{
  [Inject]
  private IMyServiceA ServiceA{ get; set; }
  void Method()
  {
    this.ServiceA.Method()
  }
}

public interface IMyServiceC
{
  void Method();
}

// constructor injection
// MyServiceC is better to be registered to the Container to get created automatically with an instance of IMyServiceA
public class MyServiceC: ContainerObject, IMyServiceC
{
  public MyServiceC(IMyServiceA serviceA)
  {
    this.ServiceA = serviceA;
  }
  private IMyServiceA ServiceA{ get; set; }
  void Method()
  {
    this.ServiceA.Method()
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It is just a basic design of my DI system with detail reduced. Hope it helps&lt;/P&gt;</description>
      <pubDate>Fri, 23 Sep 2022 17:05:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11441765#M16675</guid>
      <dc:creator>Kennan.Chen</dc:creator>
      <dc:date>2022-09-23T17:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11442461#M16676</link>
      <description>&lt;P&gt;Really cool implementation, maybe a simple library for that could be cool.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only DI for Revit I know is this framework: &lt;A href="https://github.com/engthiago/Onboxframework/" target="_blank"&gt;https://github.com/engthiago/Onboxframework/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Sep 2022 00:23:13 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11442461#M16676</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2022-09-24T00:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11442715#M16677</link>
      <description>&lt;P&gt;I created this code years ago for the reason to build the wheels to enhance my technical skills. There are some really awesome open source DI frameworks out there developers can leverage, which makes my DI system look like a toy.&lt;span class="lia-unicode-emoji" title=":face_with_tears_of_joy:"&gt;😂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Moreover, it is part of my company project which means it requires much refactoring or completely redesign before I can publish it legally. Just like I need to reduce much detail before I can publish it here partially. Revit.Async is such a library I extracted from what I've created in my company project but with a completely new design.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Sep 2022 07:28:47 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/11442715#M16677</guid>
      <dc:creator>Kennan.Chen</dc:creator>
      <dc:date>2022-09-24T07:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: Register Services (Dependency Injection)</title>
      <link>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/12424159#M16678</link>
      <description>&lt;P&gt;Today I created a simple project with your implementation to experiment, kinda add others missing features.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://github.com/ricaun-io/RevitAddin.DI.Simple" target="_blank"&gt;https://github.com/ricaun-io/RevitAddin.DI.Simple&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Kinda was able to create a generic &lt;STRONG&gt;RevitCommand&lt;/STRONG&gt;.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[Transaction(TransactionMode.Manual)]
public class RevitCommand&amp;lt;T&amp;gt; : IContainerObject, IExternalCommand where T : ICommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
    {
        this.GetService&amp;lt;T&amp;gt;().Execute();
        return Result.Succeeded;
    }
}&lt;/LI-CODE&gt;&lt;LI-CODE lang="csharp"&gt;public interface ICommand
{
    void Execute();
}&lt;/LI-CODE&gt;&lt;LI-CODE lang="csharp"&gt;public class CommandA : ICommand
{
    private readonly UIApplication uiapp;

    public CommandA(UIApplication uiapp)
    {
        this.uiapp = uiapp;
    }
    public void Execute()
    {
        System.Windows.MessageBox.Show($"{this.GetType().Name} - {uiapp}");
    }
}&lt;/LI-CODE&gt;&lt;P&gt;And create the button using my &lt;A href="https://www.nuget.org/packages/ricaun.Revit.UI" target="_blank" rel="noopener"&gt;ricaun.Revit.UI&lt;/A&gt; package.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;ribbonPanel.CreatePushButton&amp;lt;RevitCommand&amp;lt;CommandA&amp;gt;&amp;gt;();&lt;/LI-CODE&gt;&lt;P&gt;DI is fun.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2023 19:36:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/revit-api-forum/register-services-dependency-injection/m-p/12424159#M16678</guid>
      <dc:creator>ricaun</dc:creator>
      <dc:date>2023-12-06T19:36:50Z</dc:date>
    </item>
  </channel>
</rss>

