我正在创建一个ASP.NET Web服务。我有一个类,其静态构造函数在我尝试初始化该类的对象时没有被调用。我无法理解这种行为。在静态构造函数内,我正在从web.config文件中读取值。
以下是代码的一部分:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
AppController extractor;
public Service()
{
try
{
extractor = new AppController();
}
catch(Exception ex)
{
// I am not getting exception at this point.
}
}
}
public class AppController
{
static string converterBatchFilePath = null;
static string personalProfileOutputFolderPath = null;
static AppController()
{
// reading some settings from web.config file
try
{
converterBatchFilePath = ConfigurationManager.AppSettings["WordToTextConverterBatFilePath"];
}
catch(Exception ex)
{ // }
}
public AppController()
{
// do some initialization
}
}
在调试 Web 服务时,我发现只有实例构造函数被调用,而控制流从未进入静态构造函数。
有人知道这是为什么吗?
我正在使用 VS 2008 Express 版本和 C#。
编辑
实际上,这个 AppController 是一个基于控制台的项目。我已将该项目作为引用添加到 Web 服务项目中,然后在其中使用它。如果我从命令行中使用 AppController,则可以正常工作,但从 Web 服务项目中无法正常工作。
static AppController
的内容放入 try...catch 中,并显示/记录异常。 - CodesInChaos