0

As you can see in C# Unity container, we can configure a container (a list of dependency and object creation policy) from a file, and it is very good when, I want change a dependency (backend for interface) in the runtime environment without need to upload my project again, or in a dockerise paradigm, I do not need to build the Docker image again.

At now, I want to migrate to the ASP.NET 6 built-in DI, How can I provide the same functionality in the built-in DI?

For example, something like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IProductService" type="UnityExample.Service.IProductService, UnityExample.Service" />
    <containers>
      <container name="Service">
        <register type="IProductService" mapTo="ProductService"/>
      </container>
    </containers>
  </unity>
</configuration>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
  • 1
    Loading from files isn't DI functionality and ASP.NET Core isn't Unity. *That* Unity is a somewhat ancient and overengineered attempt to create a DI container for *.NET Framework* only, that was abandoned in the 2010s - it didn't even make it to the .NET Framework 4 era. Back then the Patterns and Practices group tried to create *both* a DI *and* a Plugin framework. In .NET Core's DI on the other hand, you need *none* of that configuration, no attributes, to actually register and use services. – Panagiotis Kanavos Jun 02 '23 at 09:48
  • 1
    Perhaps you assumed .NET 6 is .NET Framework 6? It's not, it's .NET *Core* 6, a completely different runtime. The libraries created for .NET Framework can't run on .NET Core. – Panagiotis Kanavos Jun 02 '23 at 09:50
  • As for how to register services based on files, think about what you're asking first. You'll have to deploy *all* possible services in the same binary, using the file to pick one implementation or the other. You *can* do that very easily, as all registration methods like [AddSingleton](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addsingleton?view=dotnet-plat-ext-7.0) have overloads that accept a `Type` parameter. You could use Reflection to pick types based on names. – Panagiotis Kanavos Jun 02 '23 at 09:53
  • @PanagiotisKanavos Did you can some detail explanation about your solution, provide in third comments? – sorosh_sabz Jun 02 '23 at 09:57
  • You right about .NET 6, I want to migrate to .NET 6 from .NET Framework, and I want to achieve same functionality and flexibility in .NET 6 as I have in .Net Framework 4.5 – sorosh_sabz Jun 02 '23 at 09:58
  • If the actual question is how to load plugins, the docs explain this in [Creating docs with plugin support](https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support). – Panagiotis Kanavos Jun 02 '23 at 09:58
  • 2
    @sorosh_sabz: consider switching to Autofac. Autofac can [replace the builtin .NET Core's container](https://stackoverflow.com/a/69788937/941240) and also it supports loading configuration from a file. – Wiktor Zychla Jun 02 '23 at 10:27
  • `same functionality and flexibility in .NET 6 as I have in .Net Framework 4.5` Unity was very *inflexible*. That's while DI libraries like Autofac and SimpleInjector were created and still thrive. The .NET Core team decided from the start to provide basic middleware, not full replacements for other people's libraries. None of the P&P libraries made it to the 2020s. You can integrate other middleware with .NET Core, eg use Autofac as the backing end of IServiceProvider, Serilog for ILogger. – Panagiotis Kanavos Jun 02 '23 at 12:06
  • @WiktorZychla I think autofac is good alternative approach, thanks, Did you want provide answer this question? – sorosh_sabz Jun 02 '23 at 14:03
  • Sure, thanks. There goes – Wiktor Zychla Jun 02 '23 at 16:06

1 Answers1

0

While you could have a hard time making the new service provider compatible with Unity, consider switching to Autofac. Autofac can replace the builtin .NET Core's container and also it supports loading configuration from a file.

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106