Reputation: 2970
My Web Service being called
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
private string password;
[WebMethod]
public void SetPassword(string pPassword)
{
password = pPassword;
}
private bool ValidPassword()
{
return (password == ConfigurationManager.AppSettings["Token"].ToString());
}
[WebMethod]
public DataSet GetAccountInfo(string account)
{
ValidPassword();
//logic here
}
The web page doing the call
Dim mySvc As New RNX.Service
mySvc.SetPassword("passwordhere")
Dim dsAcctInfo As DataSet = mySvc.GetAccountInfo(strAcct)
Both methods are executing just fine... SetPassword finishes, and then using the same Web Service object, the caller then runs GetAccountInfo, which checks that it is a ValidPassword().
However, at this time, the variable is null again... as in, not retaining the value.
Upvotes: 0
Views: 471
Reputation: 340
Every time the service is called a new instance is created. Services aren't designed to maintain state. They are supposed to be stateless. So, the first time it's called with mySvc.SetPassword, a new instance of class Service is instantiated and password is set. Nothing is returned then the Service instance is destroyed. Then it's called again with mySvc.GetAccountInfo(strAcct), a new instance of class Service is instantiated and password is not set so ValidPassword() returns false.
If you want to maintain state you could use Session to store the password (though this seems like a bad idea!)
Upvotes: 2
Reputation: 35363
You should enable session as [WebMethod(EnableSession=true)]
and store the password in Session
.
[WebMethod(EnableSession=true)]
public void SetPassword(string pPassword)
{
Session["password"] = pPassword;
}
Upvotes: 2