2

I'm trying to make an aspnet core project using Unity.container.

My problem is to load the unity.config, so I'm trying make the project use the web.config

this is my startup.

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>().UseUnityServiceProvider(GetContainer());

    private static IUnityContainer GetContainer()
    {

        var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
        Dictionary<string, IUnityContainer> containers = new Dictionary<string, IUnityContainer>();
        InitiateContainers(unitySection, containers);
        return containers.Values.First();
    }
    internal const string DefaultContainerName = "Default";

    internal static void InitiateContainers(UnityConfigurationSection unitySection, IDictionary<string, IUnityContainer> containers)
    {
        foreach (var element in unitySection.Containers)
        {
            string name = string.IsNullOrEmpty(element.Name) ? DefaultContainerName : element.Name;
            if (!containers.ContainsKey(name))
            {
                containers.Add(name, new UnityContainer());
            }
            unitySection.Configure(containers[name], element.Name);
        }
    }

But the variable "unitySection" is always null.

How can I make the aspnet core app load the web.config file?

Do I need to import another nuget package?

Programmer
  • 121,791
  • 22
  • 236
  • 328
Luiz Bicalho
  • 813
  • 1
  • 12
  • 30

1 Answers1

3

Thanks to gunr2171 to post How to read web.config file in .Net Core app to another question.

The problem was solved by a comment in that question:

Just rename web.config to app.config that it will work.

Luiz Bicalho
  • 813
  • 1
  • 12
  • 30
  • 1
    App.config works because an ASP.NET Core web application is just a console app, and console apps can read from App.config. However, just because you can doesn't mean you should. There's a proper way to do configuration in ASP.NET Core, and this isn't it. However, if it's a requirement by some third-party library, you may not have a choice in the matter. – Chris Pratt Aug 22 '18 at 18:42
  • That's the case, unity container uses the app.config to load the registrations, they are converting the unity.container to use the json format, but there isn't a plan to relase this. so we have to use unity, I didn't find another DI container that uses configuration file and integrates with ASP.NET Core DI – Luiz Bicalho Aug 22 '18 at 18:52
  • That's fine. It's just a general caution that you should use it only for what you absolutely have to use it for. Don't go whole hog and start throwing in everything and the kitchen sink, just because it's there. – Chris Pratt Aug 22 '18 at 19:01
  • I only use the config to register what I can't load by default, or what I can't load by convention, for example, if I need to pass a parameter , or if I need to pass a dependency that is named. – Luiz Bicalho Aug 22 '18 at 19:45
  • I have similar requirement to be implemented in function app (NET Core 2.0 Azure Functions).. Function app is class library not console application. any way to achieve this? – Suresh Apr 25 '20 at 22:56