0

I am making a project using WebServices in C#. I wanted to ask you, how can I give a value to a variable from Client to a Web Service?

For example: In the web service I have a variable and two Methods, getVariable() and setVariable(bool a);

bool var = false;

[WebMethod]
public void setVariable(bool a)
{
 var = a;
}

[WebMethod]
public bool getVariable()
{
 return var;
}

This is how my web service looks (it's simple because I am learning).

My client: //In the client I added the web service as a Service Reference and added this code:

ServiceReference.ServiceSoapClient obj = new ServiceReference.ServiceSoapClient();


private void Form_Load(object sender, EventArgs e)
{
    obj.setVariable(true);
    label1.Text = obj.getVariable().ToString();
}

And when I load my form, the label1.Text isn't equal with "True" but with "False"!! Which means that it didn't execute this code: obj.setVariable(true); My professor said in the class something that WebService are now "full...." (but I couldn't hear it well), he said you have to find a way to make WebServices "ful..."

Can someone help me ?

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
  • 2
    He probably said stateful, as opposed to stateless. http://stackoverflow.com/questions/988819/stateful-webservice – flup May 04 '13 at 18:51
  • 2
    Not a good way, just for learing purposes: try `static bool var = false;` and see the difference – I4V May 04 '13 at 18:57
  • 1
    That would be a really bad approach, especially for learning purposes. – Wiktor Zychla May 04 '13 at 18:58
  • 2
    Var is a keyword. Don't name your variables the same as a keyword, even if it doesn't throw an error. Just a good practice. – Yatrix May 04 '13 at 18:59
  • yes @flup, it was stateful.... – Tarion Selmani May 04 '13 at 18:59
  • @Yatrix, i know that, i just used it as an example – Tarion Selmani May 04 '13 at 19:00
  • @TarionSelmani "[Traditionally](http://en.wikipedia.org/wiki/Foobar)" the words Foo, Bar, Baz, and Qux are used for placeholder when you need to make up variable/class names. If you need more than 4, you likely need to simplify your example :) – Scott Chamberlain May 04 '13 at 19:13
  • ASMX is a legacy technology, and should not be used for new development. WCF should be used for all new development of web service clients and servers. One hint: Microsoft has retired the [ASMX Forum](http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/threads) on MSDN. – John Saunders May 04 '13 at 19:14

3 Answers3

3

Web services are stateless by default, which means that they don't retain state between calls. So a value set in one call won't be available for usage by the next call.

If you need a stateful service, there are several ways to go. this is probably the simplest.

It sounds like you are doing this for learning purposes, in which case I suggest reading up on why it is not a good practice to develop using stateful services. Try this one for starters.

Addys
  • 2,461
  • 15
  • 24
2

Since web services are stateless you can't do what you are trying to do.

Although you have only one client instance, for each call a server-side instance gets created. That means that in the first call you set the variable on an object, which then goes out of scope (since it's tied to that request). When you do make the second call, a new instance of your service gets created and this new instance obviously has no knowledge of the previous one.

If you want to do something like that you need the save the state. This can be done in various ways:

  • Save the value in the Application object (HttpContext.Current.Application("myvar") = a)
  • Save it in session state (you need to add an attribute to the service class to enable session state, then you do the same as with the Application but accessing HttpContext.Current.Session
  • Save it in a database
  • Save it in a file
  • ...
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • 1
    You can also use WF Services, if you're using newer .NET versions, albeit this comes down to the same thing - you need to create an instance of a workflow. However things like instancing and saving to database (persisting) are pretty much done "out of the box". :) – MBender May 04 '13 at 19:08
0

@I4V - Thanks for your comment on my post.

I just added static before the bool variables and it worked.

BenMorel
  • 34,448
  • 50
  • 182
  • 322