How to pass null as a default instance of an interface using StructureMap?

I want to pass null as the default instance for my IFarmManager class using StructureMap. In my Global.asax application, I have the following:

ObjectFactory.Initialize(x =>
{
    x.ForRequestedType<IFarmManager>().TheDefault.Is.Object(null);
};

However, at runtime, an ArgumentNullException is thrown:

Value cannot be null.
Parameter name: anObject
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: anObject

Is it possible to use null as the default instance for an interface?

I know this question: StructureMap and passing a null parameter to an instance , but it has no answers.

+3
source share
1 answer

It seems the way to do this is as follows:

ObjectFactory.Initialize(x =>
{
    x.ForRequestedType<IFarmManager>().TheDefault.Is.ConstructedBy(() => null);
});
+1
source

Source: https://habr.com/ru/post/1748548/


All Articles