2010-06-04 75 views
3

我想通过使用StructureMap来传递null作为我的IFarmManager类的默认实例。我目前得到了我的Global.asax如下:如何使用StructureMap将null作为接口的默认实例?

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

然而,ArgumentNullException在运行时抛出:

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 

是否有可能使用null作为接口的默认实例?

我知道这个问题:StructureMap and passing null parameter to instance但它没有任何答案。

回答

1

看来做到这一点是如下的方式:

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

此语法不再受支持。如何用当前的StructureMap完成? – 2016-08-16 18:42:02

+0

不知道,对不起。我在不久之后转向了Autofac。 – 2016-08-16 18:43:01

相关问题