我現在是這樣的:是否可以在C#中創建有狀態的Web服務?
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
}
...
}
是否有可能讓有狀態的web服務,這樣我可以重用SomeObj so
並調用同一個對象的方法?
因此,將使用此服務的客戶端將首先調用web方法,該方法將創建so
對象並返回一些ID。 然後在隨後的調用中,Web服務將根據ID重用相同的so
對象。
編輯
這裏是我的實際代碼:
[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);
}
你可以存儲在緩存中的所有對象或有什麼限制嗎? 我的對象包含與各種計算機的打開WMI連接。 – Primoz 2010-11-07 13:10:48
默認情況下,緩存存儲在內存中,如果您開始在低內存上運行,它將自動被驅逐。所以是的,你可以隨心所欲地存儲,但你應該經常檢查對象是否在緩存中,並且永遠不要因爲存儲它而依賴它。 – 2010-11-07 13:13:00
@Darin Dimitrov,你應該通過使用鎖來使你的解決方案類型安全。當一個請求正在執行「result = action();」行,這需要時間,任何其他請求也會看到緩存爲空,並重復相同的「result = action();」線。見http://en.wikipedia.org/wiki/Double-checked_locking – Sklivvz 2010-11-07 13:22:25