Cannot initialize variables in constructor that's using dependency injection - dependency-injection

I have a constructor that's getting its dependencies from a Dependency Injection framework (Autofac). The problem is I can not do anything else in the constructor except getting those dependencies, if I write any statement inside the constructor other than those dependency assignments, I get the following error:-
No parameter less constructor is defined for this object
I have to comment the assignment of readonly string shown below for this to work:-
public RelationshipController(ICustomerService customerService,)
{
this.customerService = customerService;
//someReadonlyString = "abcd";
}
Can somebody tell me, What's happening?

When the requested service (in this case, ICustomerService) cannot be found in the container, Autofac will move on to look for a default (a.ka. the parameterless) constructor. You obviously have no default constructor, thus Autofac bails out with an exception.
My guess is that the real error here is in the registration code for the ICustomerService interface.

I see a RelationshipController which probably means that you are using ASP.NET MVC. You probably didn't register Autofac correctly in MVC. In that case, MVC will not use Aufofac and can only work with default constructors.

Related

Creating new object with .net Core Dependency Injection fails with missing parameter error

I'm trying to new-up an object and if that object's constructor contains a .Net Core-injected dependency (Serilog.ILogger) I get a compile error that "There is no argument given that corresponds to the required formal parameter ...".
I can add the ILogger injection in my MVC controller constructor and I get an instance of ILogger so I know the DI is working.
But if I setup a class:
using Serilog;
public class TestObject
{
public TestObject(ILogger logger) { }
}
And call it:
TestObject test = new TestObject();
I get the error message:
There is no argument given that corresponds to the required formal parameter 'logger' of 'TestObject.TestObject(ILogger)'
(However, injection DOES work in my CommandHandlers, for example, if the handler class is invoked by the MediatR mediator.)
Isn't the whole point of DI so that I don't have to pass in dependencies like this?:
var query = new GetAllUsers(logger);
If I want to use logging in my commands and queries am I going to have to create factories for all of my command and queries?
Am I missing something fundamental here?
Thank you in advance. It was surprisingly hard to get an answer via Google for this as the keywords "new", "dependency injection", etc. were too common.

Passing constructor arguments to OutputCacheProvider

I have implemented a custom OutputCacheProvider
public class MongoDBCacheProvider : OutputCacheProvider, IDisposable { ... }
The cacheprovider is registered like so:
<outputCache defaultProvider="MongoDBCacheProvider" enableOutputCache="true" >
<providers>
<add name="MongoDBCacheProvider" type="Mynamespace.Core.Caching.MongoDBCacheProvider, Mynamespace.Core" />
</providers>
</outputCache>
I need to pass some arguments to the constructor. I want to use Ninject to bind my cache provider.
this.Bind<System.Web.Caching.OutputCacheProvider>()
.To<Core.Caching.MongoDBCacheProvider>()
.WithConstructorArgument("databaseName", dbName);
More arguments have to be passed, but this is just an example. I'm sure simple solutions exist to somehow get that string there, but i would prefer to use Ninject like i use for all other classes.
This fails with the message: "No parameterless constructor defined for this object." The following binding doesn't work either.
this.Bind<Core.Caching.MongoDBCacheProvider>().ToSelf()
.InSingletonScope()
.WithConstructorArgument("databaseName", dbName);
I have verified that the binding runs before the error occurs. ASP .NET somehow bypasses the ninject bindings.
There doesn't seem to be any way to plug in a factory for it either.
Does anybody know how i can pass constructor arguments to a derived OutputCacheProvider?
Thank you.
You should be able to Inject into the object early in your web app life cycle by causing it to be injected during app startup.
NB you will have to use Property Setter Injection in preference to Constructor Injection, as there is likely no way to get the Cache Provider Provider to give you control of the instance creation.
See this blog post by #Remo Gloor

IOC on IValidationDictionary with Castle Windsor

I'm new to Castle Windsor and am just using the latest version. I've created entries for my repositories which are working fine but I have one final dependency that I'm passing into my controller.
I've created a ModelStateWrapper which inherits from IValidationDictionary. The ModelStateWrapper takes a ModelStateDictionary in it's constructor so that in my code I can pass the following as an example:
IMembershipService _memSvc;
IValidationDictionary _validationService;
public AccountController()
{
_validationService = new ModelStateWrapper(this.ModelState);
_memSvc = new MembershipService(_validationService);
}
In my tests I can then do this using Moq:
var v = new Mock<ModelStateDictionary>();
_validationService = new ModelStateWrapper(v.Object);
_service = new MembershipService(_validationService);
I can't seem to get Castle to inject ModelState in with the ModelStateWrapper. I have no idea where to start and it seems I can't just 'ignore it' and try to manually inject as Castle is searching for dependencies and throwing me an error saying a dependency is remaining.
How do I configure Castle Windsor to use the ModelStateWrapper based off IValidationDictionary and also include ModelState as the constructor parameter?
Lloyd
It seems like you have a circular dependency (never a good thing). You can get around it by using an Abstract Factory as described in this very similar question.
However, although you may be able to solve the problem like this, it would be better to redesign the API to make the circular dependency go away. Circular dependencies often indicate a design flaw.
You're doing it wrong, and your wrongdoing has nothing to do with the container you're using.
Just do it like this, if you absolutely need to:
public AccountController(IValidationService service)
{
_validationService = service;
_memSvc = new MembershipService(_validationService);
}
then as you're registering your component, use an OnCreate method:
container.Register(
Component.For<AccountController>()
.WheveverEleseYouNeedHere()
.OnCreate((k, controller) =>
controller.ValidationService.Init(controller.ModelState)));

How do I pass constructor arguments with dependency injection using Castle.Windsor?

I am wiring up my first SubSonic 3 application (in an ASP.NET MVC 1.0 front-end) and am looking at Rob's SimpleRepository. I'm using Castle.Windsor as an injection framework.
In my application startup, I configure Castle to bind a SubSonic SimpleRepository to a SubSonic IRepository. Nothing complicated there. However, the SimpleRepository has a ctor overload which takes two values: a connection string name and a set of SimpleRepositoryOptions. Not having dug too deep into Castle in the past, it's not clear if there is a way to specify the ctor arguments via configuration (or some other means).
Right now, I have a custom implementation of the SimpleRepository that explicitly creates a SimpleRepository with those arguments in it's parameterless ctor, but if I want to change these at any point in time, it requires changing the code and recompiling.
Is there a more elegant way to configure Castle to take constructor arguments?
If you're configuring Windsor using an XML file, you define your ctor arguments and their values like this:
<component id="repository" service="IRepository" type="SimpleRepository" ...>
&ltparameters>
<connectionString>your connection string</connectionString>
...
&lt/parameters>
</component>
See the Windsor configuration reference for more info:
http://www.castleproject.org/container/documentation/v1rc3/manual/windsorconfigref.html
See this wiki page. What's called "parameters" on that page is either a constructor parameter (which is a required parameter) or a property (optional parameter)

CastleWindsor filling the class fields too

I am a beginner using castle windsor; and kinda introduced to it with Apress Pro Mvc book. In the project that I am working at; I use castlewindsor controller factory instead of mvc controller factory; so i can have parametrized constructors and i can inject the dependencies.
Is there a way to tell the windsorcontroller factory to inject the values to the properties of the controller class without going through constructor?
The reason I want to do this is because I have Logging dependency; Emailler Dependency; Database Dependency; Theme Engine dEpendency; and I dont want to use this many parameters parameter in the constructor.
By default, when Windsor resolves a service implementation, it will populate all properties with public setters that it can satisfy.
However, take notice that sometime it does make sense to put the dependency resolving in the constructor, for that fact that it guarantees that any instance will always be in a valid state. Consider Unit Testing scenario, where the person writing the test will go crazy about the need to know which dependencies should be supplied. When all dependencies goes into the c'tor, the tester will have no choice but to supply the tested instance with all the required dependencies (as stubs or mocks).
Anyway, as for your question, Windsor support C'tor and property injection by default
Castle Windsor will automatically fill any properties with public setters that it knows how to fill.
This means if you have a class
public MyClass {
public SomeDependency {get; set;}
}
As long as the container is configured to know how to resolve SomeDependency it will attempt to resolve and inject it.
Sometimes I've found this default behavior to be hassle. This facility will give you finer grained control over the process.

Resources