为什么价值没有反映在服务层
Why the value are not reflecting in the service layer
App.Config
<appSettings>
<add key="maxDiscount" value="25"/>
</appSettings>
这是我的CustomerConfig.cs
public class CustomerConfig
{
private static int GetValue(string key)
{
return Convert.ToInt16(ConfigurationManager.AppSettings[key]);
}
public static int MaxDiscount
{
get
{
return GetValue("maxDiscount");
}
}
服务Class
public class CustomerService : ICustomer
{
private readonly int _maxDiscount;
public CustomerService()
{
//the value is NOT reflecting here
_maxDiscount = CustomerConfig.MaxDiscount;
//watching at this line I see _maxDiscount = 0
}
}
注:-
以上所有class都在同一层(Class库项目)
而下面的控制器在另一层(UI层)
MVC 控制器。
public class SomeController : Controller
{
private readonly int _maxDiscount;
public SomeController()
: base()
{
//here the value 25 is reflecting
_maxDiscount = CustomerConfig.MaxDiscount;
}
}
为什么maxDiscount的值没有体现在服务层??
What is causing this & how do I fetch the value in the service layer??
ConfigurationManager
将从正在执行的程序集配置文件加载配置。在您的场景中,它将是 web.config
文件。
如果你想让它工作,你应该在你的 web.config
文件中添加那个设置,或者,你真的想那样做,你可以尝试使用 ConfigurationManager.OpenExeConfiguration Method 但我不知道是否这是个好主意(或者如果它适用于 dll)。
App.Config
<appSettings>
<add key="maxDiscount" value="25"/>
</appSettings>
这是我的CustomerConfig.cs
public class CustomerConfig
{
private static int GetValue(string key)
{
return Convert.ToInt16(ConfigurationManager.AppSettings[key]);
}
public static int MaxDiscount
{
get
{
return GetValue("maxDiscount");
}
}
服务Class
public class CustomerService : ICustomer
{
private readonly int _maxDiscount;
public CustomerService()
{
//the value is NOT reflecting here
_maxDiscount = CustomerConfig.MaxDiscount;
//watching at this line I see _maxDiscount = 0
}
}
注:- 以上所有class都在同一层(Class库项目) 而下面的控制器在另一层(UI层)
MVC 控制器。
public class SomeController : Controller
{
private readonly int _maxDiscount;
public SomeController()
: base()
{
//here the value 25 is reflecting
_maxDiscount = CustomerConfig.MaxDiscount;
}
}
为什么maxDiscount的值没有体现在服务层??
What is causing this & how do I fetch the value in the service layer??
ConfigurationManager
将从正在执行的程序集配置文件加载配置。在您的场景中,它将是 web.config
文件。
如果你想让它工作,你应该在你的 web.config
文件中添加那个设置,或者,你真的想那样做,你可以尝试使用 ConfigurationManager.OpenExeConfiguration Method 但我不知道是否这是个好主意(或者如果它适用于 dll)。