Is it possible to create a statefull web service in C #?

I now have something like this:

public class Service1 : System.Web.Services.WebService { [WebMethod] public string Method1() { SomeObj so = SomeClass.GetSomeObj(); //this executes very long time, 50s and more return so.Method1(); //this exetus in a moment } [WebMethod] public string Method2() { SomeObj so = SomeClass.GetSomeObj(); //this executes very long time, 50s and more return so.Method2(); //this exetus in a moment } ... } 

Is it possible to make a statefull web service so that I can reuse SomeObj so and just call methods on the same object?

Thus, the client who will use this service will first call the web method, which will create the so object and return some identifier. And then in subsequent calls, the web service will use the same so object based on the identifier.

EDIT


Here is my actual code:

 [WebMethod] public List<ProcInfo> GetProcessList(string domain, string machineName) { string userName = "..."; string password = "..."; TaskManager tm = new TaskManager(userName, password, domain, machineName); return tm.GetRunningProcesses(); } [WebMethod] public bool KillProcess(string domain, string machineName, string processName) { string userName = "..."; string password = "..."; (new TaskManager(userName, password, domain, machineName);).KillProcess(processName); } 
+6
c # web-services persistence stateful
source share
3 answers

Oral web services do not scale, and I would not recommend them. Instead, you can save the results of expensive operations in the cache. This cache can be distributed through specialized providers for better scalability:

 [WebMethod] public string Method1() { SomeObj so = TryGetFromCacheOrStore<SomeObj>(() => SomeClass.GetSomeObj(), "so"); return so.Method1(); //this exetus in a moment } [WebMethod] public string Method2() { SomeObj so = TryGetFromCacheOrStore<SomeObj>(() => SomeClass.GetSomeObj(), "so"); return so.Method2(); //this exetus in a moment } private T TryGetFromCacheOrStore<T>(Func<T> action, string id) { var cache = Context.Cache; T result = (T)cache[id]; if (result == null) { result = action(); cache[id] = result; } return result; } 
+6
source share

Option 1

You can use your HttpSession.

 //this executes very long time, 50s and more, but only once. private SomeObj SessionSomeObj { get { var ret = (SomeObj)Session["SomeObjStore"] ?? SomeClass.GetSomeObj(); SessionSomeObj = ret; return ret; } set { Session["SomeObjStore"] = value; } } [WebMethod(EnableSession = true)] public string Method1() { return SessionSomeObj.Method1(); //this exetus in a moment } [WebMethod(EnableSession = true)] public string Method2() { return SessionSomeObj.Method2(); //this exetus in a moment } 

Please note that this will only work if you make one call to the client at the same time.

Option 2

You can leave the class as is, but use WebMethod in different ways. If you call from the generated .Net class, async methods are provided for these entries. Basically, you call the request method begin1 Method1 and get a callback after completion. You may need to configure the timeout parameter of the web service client class for this to work.

Option 3

You can use the caching functions of the SixPack library to make it easy !; -)


[Edited after comment] Option 1 has two static fields to allow two different instances, one per method, on request.


[Edited after further explanation] Using a session to ensure call status.

See: http://msdn.microsoft.com/en-us/library/aa480509.aspx

Option 3 has also been added.

+1
source share

Change the ServiceContract your interface to:

 [ServiceContract(SessionMode = SessionMode.Required)] 

And add the following attribute to your class:

 [ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.PerSession)] 

See http://msdn.microsoft.com/en-us/library/system.servicemodel.sessionmode.aspx for details.

0
source share

All Articles