Pri*_*moz 6 c# persistence web-services stateful object-persistence
我现在有这样的事情:
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
}
...
}
Run Code Online (Sandbox Code Playgroud)
有可能创建有状态的Web服务,以便我可以重用SomeObj so并只调用同一对象上的方法吗?
因此,将使用此服务的客户端将首先调用web方法,该方法将创建so对象并返回一些ID.然后在后续调用中,Web服务将so基于ID 重用相同的对象.
编辑
这是我的实际代码:
[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);
}
Run Code Online (Sandbox Code Playgroud)
有状态的Web服务不可扩展,我不推荐它们.相反,您可以将昂贵操作的结果存储在缓存中.此缓存可以通过自定义提供程序分发,以获得更好的可伸
[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;
}
Run Code Online (Sandbox Code Playgroud)