DI IOC Autofac file-based configuration with Aspnet MVC core

358 views
Skip to first unread message

PA

unread,
Dec 14, 2017, 8:24:18 AM12/14/17
to Autofac
I have a backend project and have implemented IOC using autofac and registered my dependencies via Class container. However my aim us to move the Autofac commands /Register types to a json/xml file based configuration. I referred to link http://autofac.readthedocs.io/en/latest/configuration/xml.html and is not very clear. can someone help me.Thanks.

My container class looks like below:


using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Core;
using Admin.Api.StuffApi.Commands; 
using undefined.cmd;
using Microsoft.Extensions.Configuration;
//using Autofac.Util.Configuration.ConfigurationModule;
using Microsoft.Extensions.Configuration.Json;

namespace a_a
{
    public static class GlobalContainer
    {
        public static IContainer Container { get; set; }

        static GlobalContainer()
        {
            var builder = new ContainerBuilder();

             var config = new ConfigurationBuilder();
             config.AddJsonFile("IOCConfiguration.json");  
             var module = new ConfigurationModule(config.Build());
             builder.RegisterModule(module); 

            Container = builder.Build(); 
        }
    }
}
I have created a IOCConfiguration.Json file with autofac commands as below:
{
    //"defaultAssembly": "",
    "components": 
    [
        {
                    "type": "undefined.cmd.StuffController"
        }, 
        {
                "type": "Admin.Api.StuffApi.AppCode.Commands",
                "services": 
                [{
                    "type": "Admin.Api.StuffApi.Commands.ICommands"
                }]
        }
    ]
}

Issue: When I run the Http request/API, I get the message as : The requested service 'Admin.Api.StuffApi.Commands.ICommands' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

I understand that my Json configuration (Registering the components ) are not being recognized, could anyone help me if I am missing anything with respect to configuration.

Thanks

Travis Illig

unread,
Dec 14, 2017, 11:35:29 AM12/14/17
to Autofac
You're not specifying a default assembly but you're also not assembly qualifying your type names. Revisit the doc section on Default Assembly.

http://autofac.readthedocs.io/en/latest/configuration/xml.html#default-assembly

It also looks like you may need to also look at the docs on integration with ASP.NET Core. None of what you have actually appears to integrate with ASP.NET Core at all.

http://autofac.readthedocs.io/en/latest/integration/aspnetcore.html

And don't forget to look at the Examples repo as well as unit tests for the configuration stuff to see real working examples of things.

https://github.com/autofac/Examples/tree/master/src/AspNetCoreExample

If this doesn't unblock you I'd recommend asking the question on StackOverflow and putting a link to the question from here. There's a lot bigger group of people (including most of the folks here) over on StackOverflow.

Message has been deleted

PA

unread,
Dec 14, 2017, 11:52:18 AM12/14/17
to Autofac
I was not sure if a default assembly was required, also I I have different namespaces for my components and I mentioned my components with its namespaces.

Yes , I have posted the issue in Stackoverflow but non has responded yet :-(

Is it possible for you to point/advice bit more in my code with further information if you can.

Thanks.
Reply all
Reply to author
Forward
0 new messages