<?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: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder? in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1538300#M12092</link>
    <description>&lt;P&gt;I know it been some time, but can you share you implementation how or where you added the Host initialization code. There was a bug in earlier version of the Pro, which was dependent of .Net Core 2.1 version libraries which has been fixed in Pro3.2 and higher. Have you tried this in the latest version?&lt;/P&gt;</description>
    <pubDate>Fri, 13 Sep 2024 14:54:22 GMT</pubDate>
    <dc:creator>thejuskambi</dc:creator>
    <dc:date>2024-09-13T14:54:22Z</dc:date>
    <item>
      <title>ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273377#M9581</link>
      <description>&lt;P&gt;I’ve been trying to implement proper Dependency Injection using Microsoft.Extensions.DependecyInjection and Microsoft.Extensions.Hosting but I keep running into this error whenever I try build my HostBuilder &amp;nbsp;“Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified”. I would appreciate any help, thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 02:15:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273377#M9581</guid>
      <dc:creator>diegom_geoowl</dc:creator>
      <dc:date>2023-03-30T02:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273594#M9584</link>
      <description>&lt;P&gt;I haven't used&amp;nbsp;&lt;SPAN&gt;Microsoft.Extensions.Hosting, however, it looks like your app seems to look for .NET 7.&amp;nbsp; &amp;nbsp;ArcGIS Pro is built on .NET 6.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 15:28:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273594#M9584</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2023-03-30T15:28:12Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273628#M9585</link>
      <description>&lt;P&gt;The issue is that I do not have that version of '&lt;SPAN&gt;System.Diagnostics.DiagnosticSource&lt;/SPAN&gt;' installed.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 16:20:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273628#M9585</guid>
      <dc:creator>diegom_geoowl</dc:creator>
      <dc:date>2023-03-30T16:20:47Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273633#M9586</link>
      <description>&lt;P&gt;&lt;SPAN&gt;System.Diagnostics.DiagnosticSource is part of the .NETCore app framework, so i am pretty sure if you install the .NET 7 runtime you have the desired assembly version.&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 16:30:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273633#M9586</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2023-03-30T16:30:39Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273771#M9587</link>
      <description>&lt;P&gt;I downloaded and installed the runtime, still the the same error. Super weird. I apologize for the inconvenience.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2023 19:18:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273771#M9587</guid>
      <dc:creator>diegom_geoowl</dc:creator>
      <dc:date>2023-03-30T19:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273960#M9591</link>
      <description>&lt;P&gt;I figured it out! I had change the installation directory for ArcGIS Pro! &lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2023 04:46:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1273960#M9591</guid>
      <dc:creator>diegom_geoowl</dc:creator>
      <dc:date>2023-03-31T04:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275399#M9603</link>
      <description>&lt;P&gt;I'm looking to do something similar. &amp;nbsp;Where in your module code/how did you set up your container? I'd love to see any sample code you're willing to share!&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 18:22:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275399#M9603</guid>
      <dc:creator>KarlKemp</dc:creator>
      <dc:date>2023-04-04T18:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275565#M9604</link>
      <description>&lt;P&gt;Not sure how the installation directory for ArcGIS Pro would fix the&amp;nbsp;&lt;SPAN&gt;“Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=7.0.0.0, C..." issue.&amp;nbsp; &amp;nbsp;Can you elaborate?&amp;nbsp; &amp;nbsp;Where did you install ArcGIS Pro?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 22:09:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275565#M9604</guid>
      <dc:creator>Wolf</dc:creator>
      <dc:date>2023-04-04T22:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275856#M9609</link>
      <description>&lt;P&gt;By default it installed&amp;nbsp; in the AppData. When I installed it in ProgramFiles the error was resolved. I'm assuming there was a possible permissions issue?&lt;/P&gt;&lt;P&gt;Also, the Microsoft.Extensions.Configuration.Abstractions dll (Version 2.0.0.0) located in the ArcGIS Pro folder is interfering with the&amp;nbsp;Microsoft.Extensions.Configuration.Abstractions Version 6.0.0 package installed in the AddIn project.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2023 16:35:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1275856#M9609</guid>
      <dc:creator>diegom_geoowl</dc:creator>
      <dc:date>2023-04-05T16:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1538300#M12092</link>
      <description>&lt;P&gt;I know it been some time, but can you share you implementation how or where you added the Host initialization code. There was a bug in earlier version of the Pro, which was dependent of .Net Core 2.1 version libraries which has been fixed in Pro3.2 and higher. Have you tried this in the latest version?&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2024 14:54:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1538300#M12092</guid>
      <dc:creator>thejuskambi</dc:creator>
      <dc:date>2024-09-13T14:54:22Z</dc:date>
    </item>
    <item>
      <title>Re: ArcGIS Pro SDK for .NET Add-In Module - Is there a way to implement Dependency Injection using Microsoft.Extensions.Hosting HostBuilder?</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1697114#M13499</link>
      <description>&lt;P&gt;For those wondering how to get this to work. You need to evaluate what version of .NET your version of Pro is targeted for, and what assemblies may conflict with ones you are trying to load in your Add-In. If you want to use Microsoft.Extensions.* libraries, or some System.* ones provided via NuGet, you need to make sure those versions don't conflict with assemblies already loaded by Pro. You should also be aware of any libraries you include that may try to dynamically scan for assemblies, which typically use the AppDomain.CurrentDomain instead of your Add-In's assembly--and account for that by registering an event handler to point to your Add-In's assembly during the Add-In module initialization phase.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. The first step is determine the .NET version you should target. For example, if you are on ArcGIS Pro 3.5 which targets .NET 8. Go to the installation folder and examine the `System.*` and `Microsoft.*` assemblies and hover over them to get the tooltip and examine the file version. This will clue you into what versions from NuGet will be compatible to use which will not confuse the assembly loader. Also be cognizant of the rest of the assemblies in the installation location, if any other of your dependencies may use them, like `DocumentFormat.OpenXml` which at this time uses a 2.X version, some modern libraries may require versions &amp;gt;= 3.x which will cause an assembly load error if you try to use a newer version.&lt;/P&gt;&lt;P&gt;2. Once you have determined the .NET version and assembly constraints, you can add in the required packages to your project. `Microsoft.Extensions.Hosting.8.0.1` should implicitly add pretty much everything you need for a powerful use of DI in your Add-In.&lt;/P&gt;&lt;P&gt;3. To effectively use DI in a per-module fashion for an Add-In, you can start by creating an abstract base class called `ServiceProviderModule&amp;lt;TModule&amp;gt;` which will contain everything you need to bootstrap your assembly and DI during the Module initialization. The following class will preload your Add-In assembly and expose a method to be implemented, `ConfigureServices` where you will register services, logging, configuration etc when your module inherits the ServiceProviderModule. Below the abstract module class is the `ServiceHost&amp;lt;TModule&amp;gt;` which contains and builds an `IHost` that the `ServiceProviderModule&amp;lt;TModule&amp;gt;` pulls services from via a static property.&lt;/P&gt;&lt;H3&gt;ServiceProviderModule&amp;lt;TModule&amp;gt;&lt;/H3&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Hosting;
using Module = ArcGIS.Desktop.Framework.Contracts.Module;

namespace Example.DependencyInjection.Module;

public abstract class ServiceProviderModule&amp;lt;TModule&amp;gt; : Module
{
    protected sealed override bool Initialize()
    {
        AppDomain.CurrentDomain.AssemblyResolve += ResolveExecutingAssembly;
        ServiceHost&amp;lt;TMarker&amp;gt;.Configure(ConfigureServices);
        return OnInitialize();
    }

    public static IServiceProvider Services =&amp;gt; ServiceHost&amp;lt;TModule&amp;gt;.Services;
    protected abstract void ConfigureServices(HostApplicationBuilder builder);
    protected virtual bool OnInitialize() =&amp;gt; base.Initialize();
    private Assembly? ResolveExecutingAssembly(object? sender, ResolveEventArgs args)
    {
        var directory = Path.GetDirectoryName(typeof(TModule).Assembly.Location);
        var name = $"{new AssemblyName(args.Name).Name}.dll";
        var path = Path.Combine(directory, name);
        return File.Exists(path) ? Assembly.LoadFrom(path) : null;
    }
    protected override bool CanUnload()
    {
        ServiceHost&amp;lt;TModule&amp;gt;.Shutdown();
        return base.CanUnload();
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;ServiceHost&amp;lt;TModule&amp;gt;&lt;/H3&gt;&lt;P&gt;The ServiceHost&amp;lt;TModule&amp;gt; also takes care of setting the base path of the HostApplicationBuilder.Configuration base path to the true location of the add-in assembly loaded. Without this, the default path is usually somewhere in a temp folder in AppData where your `appsettings.json` files will not be. Include any `appsettings.json` files as content and they will be packaged into the `esriAddInX` file when you build it.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using System;
using System.IO;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Example.DependencyInjection.Module;

internal static class ServiceHost&amp;lt;TModule&amp;gt;
{
    private static Action&amp;lt;HostApplicationBuilder&amp;gt;? _configure;
    private static readonly Lazy&amp;lt;IHost&amp;gt; _host = new (BuildHost, LazyThreadSafetyMode.ExecutionAndPublication);

    public static void Configure(Action&amp;lt;HostApplicationBuilder&amp;gt; configure)
    {
        if (_host.IsValueCreated)
            throw new InvalidOperationException("Host already built. Configure must be called before first service is resolved.");
        _configure = configure;
    }
    
    public static IServiceProvider Services =&amp;gt; _host.Value.Services;
    public static void Shutdown() =&amp;gt; _host.Value.Dispose();
    
    private static IHost BuildHost()
    {
        var builder = Host.CreateApplicationBuilder();
        builder.Configuration.SetBasePath(Path.GetDirectoryName(typeof(TModule).Assembly.Location));
        _configure?.Invoke(builder);
        return builder.Build();
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H3&gt;ExampleModule&lt;/H3&gt;&lt;P&gt;The following ExampleModule inherits from ServiceProviderModule&amp;lt;TModule&amp;gt; and supplies its own type as the assembly type. Using Serilog is shown as a "gotcha" I ran into when using some Sinks which can get resolved via assembly scanning if loading the Serilog configuration from an IConfiguration source. Just register your services in the ConfigureServices method and do all of your wiring up here. ConfigureServices is called when the Module initializes as seen in the ServiceProviderModule&amp;lt;TModule&amp;gt; code. Beware of any async code that could deadlock the UI thread during initialization.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;User secrets and non-production appsettings.json file loaded into the IConfiguration&lt;/LI&gt;&lt;LI&gt;Serilog is configured using the IConfiguration and set up for ILogger&amp;lt;T&amp;gt; use&lt;/LI&gt;&lt;/UL&gt;&lt;LI-CODE lang="csharp"&gt;using ArcGIS.Desktop.Framework;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;

namespace Example.DependencyInjection.Module
{
    internal class ExampleModule : ServiceProviderModule&amp;lt;ExampleModule&amp;gt;
    {
        private static ExampleModule? _this = null;
        public static ExampleModule Current =&amp;gt; _this ??= (ExampleModule)FrameworkApplication.FindModule("Example.DependencyInjection.Module_Module");
        protected override void ConfigureServices(HostApplicationBuilder builder)
        {
            if (!builder.Environment.IsProduction())
            {
                builder.Configuration.AddUserSecrets("8f807625-e49b-4df5-bcc3-fcf6b2027964");
                builder.Configuration.AddJsonFile($"./appsettings.{builder.Environment.EnvironmentName}.json");
            }
            else
            {
                builder.Configuration.AddJsonFile($"./appsettings.json");
            }

            // Dyanmic assembly loading still needs a little help with Serilog. Thankfully you can specify the assembly loading context here
            var context = DependencyContext.Load(typeof(ExampleModule).Assembly);
            var options = new ConfigurationReaderOptions(context); 
            var logger = Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(builder.Configuration, options)
                .CreateLogger();

            builder.Logging.ClearProviders();
            builder.Logging.AddSerilog(logger);
        }

        /// &amp;lt;summary&amp;gt;
        /// Called by Framework when ArcGIS Pro is closing
        /// &amp;lt;/summary&amp;gt;
        /// &amp;lt;returns&amp;gt;False to prevent Pro from closing, otherwise True&amp;lt;/returns&amp;gt;
        protected override bool CanUnload()
        {
            base.CanUnload();
            return true;
        }
        

    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now that module initialization is out of the way, you can work on adding DI to your components in your Add-In. A simple example is a button which calls a logger to log something. If you want your components to be testable you should follow the same DI principals of exposing a public constructor with arguments you want to use for dependency injection. While ArcGIS Pro requires public parameterless constructors for it to initialize components, public constructors can call other constructors internally on the class which contain the service resolution logic.&lt;/P&gt;&lt;H3&gt;ExampleButton&lt;/H3&gt;&lt;P&gt;In the following Button example, we inject two services, the IConfiguration and an ILogger&amp;lt;ExampleButton&amp;gt; which are registered to private readonly fields as best practice. Then, on the OnClick method, the logger writes an information method, and we display a MessageBox which contains a value provided by the IConfiguration as just a simple service example.&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Example.DependencyInjection.Module;

public class ExampleButton : Button
{
    private readonly IConfiguration _configuration;
    private readonly ILogger&amp;lt;ExampleButton&amp;gt; _logger;

    public ExampleButton() : this(
        ExampleModule.Services.GetService&amp;lt;IConfiguration&amp;gt;(),
        ExampleModule.Services.GetService&amp;lt;ILogger&amp;lt;ExampleButton&amp;gt;&amp;gt;())
    {
        
    }

    public ExampleButton(IConfiguration configuration, ILogger&amp;lt;ExampleButton&amp;gt; logger)
    {
        _configuration = configuration;
        _logger = logger;
    }

    protected override void OnClick()
    {
        _logger.LogInformation("This came from a dependency injected logger");
        MessageBox.Show($"Hello! I got this from the configuration: {_configuration.GetSection("Example")?.Get&amp;lt;string&amp;gt;()}");
    }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can go one step further, and if you need to resolve any services registered as transient services. Just inject `IServiceScopeFactory` as an injected service and create a service scope to pull out a transient or scoped service.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Apr 2026 20:16:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-for-net-add-in-module-is-there-a/m-p/1697114#M13499</guid>
      <dc:creator>DevinCarlson1</dc:creator>
      <dc:date>2026-04-18T20:16:44Z</dc:date>
    </item>
  </channel>
</rss>

