instantiating an object from a web service vs instantiating an object from a regular class

后端 未结 3 1838
礼貌的吻别
礼貌的吻别 2021-01-05 16:30

I have a very basic web service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace          


        
相关标签:
3条回答
  • 2021-01-05 16:51

    Becaue on the server side the class is created and disposed with EVERY call you make from the client... your client is just a "proxy" and doesn't correspond directly to an instance on the server side...

    You can either make myInt static or make the server side service class a Singleton... both options would mean that myIntis shared across ALL client... or you could implement some session management to achieve a client-specific myInt... using WCF for the server side seems IMHO the best solution - it comes with configurable options for singleton, session management etc.

    EDIT - as per comments:

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

    0 讨论(0)
  • 2021-01-05 17:05

    Services created through the older .asmx technology are not singleton instances. This means that each call you make to the server instantiates a new instance of the service each time. Two real solutions, either use static variables (eugh....), or switch to using WCF.

    0 讨论(0)
  • 2021-01-05 17:15

    webservice instance is destroyed at the end of each method call, so that's why you always get the same result. You need some way to persist that value.

    0 讨论(0)
提交回复
热议问题