没有进入“有状态的服务很糟糕”的讨论......你如何创建一个?如何在asp.net中创建一个有状态的XML WebService?
由于缺乏有用的搜索结果..似乎无论我是在寻找错误的条款还是积极劝阻。我能找到的最佳链接是this one on MSDN
给出我目前的应用程序的简短上下文。 Rails Web App => ASP.Net Xml webservice => Legacy C#/C++ code
到目前为止好。现在事实证明,当我向WebMethod发出第一个请求时,它需要启动一些进程/资源,这些进程/资源在每次调用时创建使用配置都很昂贵。所以我在考虑重用这些资源。
首先,我试着将它们变成成员变量 - 只有灯泡时刻,每个请求由MyWebService
类的新实例和不同的线程提供服务。
的联系表明,要做到这一点的方法是
- 使用Session散列/属性集合的情况下,国家为每个用户。
- 使用应用程序的散列/属性集合的情况下,国家是全球
问题:
- 这是正确的?我是否缺少一些魔法属性?
- 我尝试了应用程序散列表方法...在第一个请求上填充某些已知键。但是,在第二个请求中,散列表处于干净/无项目状态。
- 如何处理同步?锁定私有成员对象的.net方式不起作用。
我的Web服务类看起来是这样的..
[WebService(Namespace = "http://Walkthrough/XmlWebServices/",
Description="A temperature conversion xml web service")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class TemperatureConverter : System.Web.Services.WebService, IDisposable
{
[WebMethod(Description = "This method converts celsius to fahrenheit")]
public double convert_fahrenheit_to_celsius(double temp_in_fahrenheit)
{
/// code
}
}
更新: 我仍然符合Q#2挣扎。有一些奇怪的行为
protected void Application_Start(object sender, EventArgs e) // global.asax.cs
{
Application["UserName"] = "Gishu";
}
myWebMethod // MyWebService.cs
{
if (Application["myKey"] == null)
LoadHeavyObjectsAndCacheKeys(); // set Application["myKey"] between Application.Lock() Unlock() calls.
...
}
“UserName”键写入成功 - 它存在于散列中。但是,我在我的web方法中编写的键值对不能在当前的Web请求中生存。在下一个请求中,散列表仍然只有一个项目 - “UserName”。由于这是ApplicationState,我假设我不必将EnableSession属性设置为true。
感谢您的确认..但是我有一些麻烦让应用程序状态持久..见更新到我的问题。另外我正在运行ASP.Net开发服务器 - 会导致这种行为吗? – Gishu 2009-08-06 05:35:13