Is it possible to create stateful web service in C#?
Stateful web services are not scalable and I wouldn't recommend them. Instead you could store the results of expensive operations in the cache. This cache could be distributed through custom 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;
}