views:

18

answers:

1

I have WebService:

public class Service1:WebService {        
        private readonly MNConnection _conn;
        private MNLpu _lpu;

        public Service1() {
            _conn = new MNConnection();
        }

        [WebMethod(EnableSession = true)]
        public void Open(string login, string password) {
            _conn.Open(login, password);
            _lpu = (MNLpu)_conn.CreateLPU();
        }

        [WebMethod(EnableSession = true)]
        public decimal Get() {
            return _lpu.Count;
        }
}

When I call it's from external console application, it's show me NullReferenceException on last line:

    CookieContainer cookie = new CookieContainer();
    Service1 lh = new Service1 {CookieContainer = cookie};
    lh.Open("login", "pwd");
    Console.WriteLine(lh.Get());

If remove Open() method from webservice and insert to constructor such lines it's works fine:

        _conn.Open(login, password);
        _lpu = (MNLpu)_conn.CreateLPU();

How to fix it? P.S. MNConnection - my own class, which works with OracleConnection.

+1  A: 

Your each call to a web method will invoke a new web service on server side, so it is not good to keep any private variables on Web Service.

For both calls, lh.Open and lh.Get, on server side, two different instances of WebService are created even if you have only one single instance of your proxy at client.

If you want to correct this, then you should only use HttpContext.Current.Session and store your useful instances of objects in this like...

You should change your web service as below...

    [WebMethod(EnableSession = true)] 
    public void Open(string login, string password) { 
        MNConnection _conn = new MNConnection();
        _conn.Open(login, password); 
        HttpContext.Current.Session["MyConn"] = _conn;
        HttpContext.Current.Session["LPU"] = _conn.CreateLPU();
    } 

    [WebMethod(EnableSession = true)] 
    public decimal Get() {
        MNLPU _lpu = HttpContext.Current.Session["LPU"] as MNLPU; 
        return _lpu.Count; 
    } 
Akash Kava