Creating an instance of an object from a web service and creating an object from a regular class

I have a very simple web service:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace WebService1 { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { public int myInt = 0; [WebMethod] public int increaseCounter() { myInt++; return myInt; } [WebMethod] public string HelloWorld() { return "Hello World"; } } } 

when I run this project that opens my browser showing me the service: enter image description here


in another solution: (console application)

I can connect to this service by adding the link:

enter image description here

enter image description here

then click the add web link button: enter image description here

Finally, I type in the URL of the newly created service: enter image description here

Now I can create an instance of the object from the Service1 class from my console application as:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication36 { class Program { static void Main(string[] args) { localhost.Service1 service = new localhost.Service1(); // here is the part I don't understand.. // from a regular class you will expect myInt to increase every time you call // the increseCounter method. Even if I call it twice I always get the same result. int i; i=service.increaseCounter(); i=service.increaseCounter(); Console.WriteLine(service.increaseCounter().ToString()); Console.Read(); } } } 

why doesn't myInt increase every time I call the boostCounter method? every time I call this method, it returns 1.

+6
source share
3 answers

Services created using older .asmx technology are not instances of a single-user network. This means that every call you make to the server starts a new instance of the service each time. Two real solutions use either static variables (eugh ....) or switch to using WCF.

+4
source

On the server side, a class is created and created with EVERY call you make from the client ... your client is just a "proxy" and does not correspond directly to the server-side instance ...

You can either make myInt static or make a server-side class of service Singleton ... both parameters mean that myInt is common to ALL the client ... or you can implement some session management to achieve the client-specific myInt ... using WCF for the server side seems to be the best IMHO solution - it comes with customizable options for singleton, session management, etc.

EDIT - according to the comments:

With WCF, you can have session-managed .NET clients, which in turn allows you to have different (client-specific) values ​​for myInt ...

+1
source

the webservice instance is destroyed at the end of each method call, so you always get the same result. You need to somehow preserve this value.

0
source

Source: https://habr.com/ru/post/898235/


All Articles