Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this static constructor is not getting called?

I am creating asp.net web service. I have one class, whose static constructor is not getting called when I try to initialize object of that class. I am not able to understand this behavior. Inside static constructor I am reading values from web.config file.

Here is part of code :

[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
    }
}

While debugging web service I noticed that only instance constructor is getting called and control never goes to static constructor.

Anyone know why this is happening?

I am using VS 2008 Express edition and C#.

EDIT

Actually this AppController is console based project. I have added that project as a reference inside Web service project and then using it. If I use AppController from command line, it works fine, but its not working from inside web service project.

like image 403
Shekhar Avatar asked Sep 13 '11 10:09

Shekhar


1 Answers

My guess is that it's called before you expected it to be called. If you have already debugged your site but not recycled the AppPool, it's very likely that the static constructor has already been run. Similarly anything that accesses any static members will also call the static constructor if it hasn't already been called.

like image 81
Rune FS Avatar answered Sep 24 '22 03:09

Rune FS