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?

1
  • 4
    I thought Asp.net core doesn't use web.config files? Commented Aug 22, 2018 at 17:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
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.
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.
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.