是否可以用 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);
}
I have now 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 stateful web service so that I can reuse SomeObj so
and just call methods on the same object?
So the client which will use this service would first call web method which would create so
object and return some ID.
And then in subsequent calls the web service would reuse the same so
object based on ID.
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有状态 Web 服务不可扩展,我不会推荐它们。相反,您可以将昂贵操作的结果存储在缓存中。该缓存可以通过自定义提供程序进行分发,以获得更好的可扩展性:
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:
选项 1
您可以使用 HttpSession。
请注意,只有在每个客户端一次调用一个呼叫时,此方法才有效。
选项 2
您可以按原样保留该类,但以不同的方式使用 WebMethod。如果您从 .Net 生成的类调用,则会为这些事件提供异步方法。基本上,您调用 Method1 开始请求方法并在执行完成时获得回调。您可能需要调整 Web 服务客户端类的超时参数才能使其正常工作。
选项 3
您可以使用缓存功能 SixPack 库 可以轻松完成此操作! ;-)
[在评论后编辑] 选项 1 中现在有两个静态字段,可根据要求允许两个不同的实例,每个方法一个。
[在评论后编辑进一步说明] 使用会话使调用有状态。
请参阅:http://msdn.microsoft.com/en-us/library/aa480509.aspx
还添加了选项 3。
Option 1
You can use your HttpSession.
Note that this will only work if one call per client is made at a time.
Option 2
You can leave the class as is but use the WebMethod differently. If you are calling from a .Net generated class,
async
methods are provided for these occurrences. Basically you invoke the Method1 begin request method and get a call back when the execution is finished. You might need to tweak the timeout parameter of the web service client class for this to work though.Option 3
You can use the caching features of the SixPack library to do this effortlessly! ;-)
[Edited after comment] There are now two static fields in option 1 to allow two different instances, one per method, as requested.
[Edited after further explanation] Using Session to make the calls stateful.
See: http://msdn.microsoft.com/en-us/library/aa480509.aspx
Also added Option 3.
将接口的
ServiceContract
更改为:并将以下属性放入您的类中:
请参阅 http://msdn.microsoft.com/en-us/library/system.servicemodel.sessionmode.aspx 了解更多信息和示例。
Change the
ServiceContract
of your interface into:And put the following attribute on your class:
See http://msdn.microsoft.com/en-us/library/system.servicemodel.sessionmode.aspx for more information and an example.