I have a service called ServiceConfiguration wich has a non-default ctor. I'm trying to register it through Autofac with a factory method:
builder.Register(c=>LoadConfiguration())
.As<ServiceConfiguration>();
And here is the simple LoadConfiguration method:
private ServiceConfiguration LoadConfiguration() {
const string fileName = "app.json";
var json = File.ReadAllText(fileName, Encoding.UTF8);
var model = JsonConvert.DeserializeObject<ServiceConfiguration>(json);
return model;
}
I expect Autofac to call the LoadConfiguration when I asked to resolve ServiceConfiguration. But it seems it's trying to call a ctor on ServiceConfiguration. E.g. calling this line:
var c = container.Resolve<ServiceConfiguration>();
causes this error:
Autofac.Core.DependencyResolutionException: 'An error occurred during the activation of a particular registration. See the inner exception for details. Registration: Activator = ServiceConfiguration (ReflectionActivator), Services = [ServiceConfiguration], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope'
and the InnerException is:
DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'ServiceConfiguration' can be invoked with the available services and parameters: Cannot resolve parameter 'JobsConfiguration jobs' of constructor 'Void .ctor(JobsConfiguration)'.
Can you find out what I'm missing?
UPDATE:
I'm not missing a registration of JobsConfiguration. Actually it's not a registrable type and I don't want to register it. I don't want to ctor get called at all (the service is getting build from a json file), instead whenever somebody asked for ServiceConfiguration I want Autofac to call LoadConfiguration() and use it's returned value.
UPDATE2:
After a while, it seems extracting an interface from ServiceConfiguration - say IServiceConfiguration - and registering/resolving the interface works just fine. But i cannot figure out why! I mean this:
builder.Register(c=>LoadConfiguration())
.As<IServiceConfiguration>();
var c = container.Resolve<IServiceConfiguration>();
works. But the concrete version not. So why? What's the difference?