简体   繁体   English

ASMX Web服务方法单例

[英]ASMX Web Service Method Singleton

Not sure if this is the right terminology, let me explain what I want. 不知道这是否是正确的术语,请让我解释一下我想要什么。

I have a web service that's available on the network - the web service has 1 web method. 我有网络上可用的Web服务-该Web服务有1种Web方法。

What I want is... if the web service is running and performing tasks and another call is made to this web service, I want the 2nd call to fail or pend for a certain period of time then fail. 我想要的是...如果Web服务正在运行并且正在执行任务,并且对该Web服务进行了另一个调用,我希望第二个调用失败或暂挂一定时间然后失败。 Because only 1 instance of this web service should be called at once. 因为只应一次调用此Web服务的一个实例。

I was thinking of writing a value to the application object (like in asp.net) but then I have to be very careful to make sure that this value gets updated, in case of any errors, it might not... so this is dangerous, and would leave the web service in a state where no one can get to it. 我当时正在考虑向应用程序对象中写入一个值(例如在asp.net中),但随后我必须非常小心以确保更新此值,以防万一出现任何错误,它可能不会...所以这是危险,并且会使Web服务处于无法访问的状态。

Is there not a more dynamic way to determine if the web service is getting called or not? 没有更动态的方法来确定是否调用Web服务吗?

If you are using WCF, this is simple. 如果您使用的是WCF,这很简单。 Use the service throttling settings to specify that you want MaxConcurrentCalls = 1 and MaxInstances = 1. You'll also want to set the ConcurrencyMode to Single for your ServiceBehavior . 使用服务限制设置来指定您希望MaxConcurrentCalls = 1和MaxInstances =1。您还需要将ServiceBehaviorConcurrencyMode设置为Single。

You cannot do this with legacy ASMX web services. 您无法使用旧版ASMX Web服务执行此操作。 They have no support for different instance schemes. 他们不支持不同的实例方案。

I believe you can do this with WCF, as you can configure the service to have only a single instance. 我相信您可以使用WCF进行此操作,因为您可以将服务配置为仅具有一个实例。

I dont know much about web services on whether you can configure a web server to only start 1 instance of your web service, but you could try creating a mutex within your web service. 对于您是否可以将Web服务器配置为仅启动1个Web服务实例,我并不了解Web服务,但是您可以尝试在Web服务中创建互斥体。

A Mutex is an interprocess synchronization object which can be used to detect if another instance of your web service is running. Mutex是进程间同步对象,可用于检测Web服务的另一个实例是否正在运行。

So, what you can do is create a mutex with a name, then Wait on it. 因此,您可以做的是创建一个具有名称的互斥量,然后等待。 If more than 1 instance of your web service is alive, then the mutex will wait. 如果您的Web服务的多个实例仍在运行,则互斥锁将等待。

您可以在web方法内部实施检查,因为它将在同一IIS进程中运行

You could create a poor man's mutex and have the first instance create a file and have consecutive instances check the existence of the file. 您可以创建一个穷人的互斥体,并让第一个实例创建一个文件,并让连续的实例检查文件的存在。 Try Catch your web method and place the deletion of the file in the finally. 尝试捕获您的Web方法,然后将删除的文件最终放置。

If you are WCF I recommend "bobbymcr" answer, but for legacy web service you can use Monitor instead or mutex as mutex is costly (because it is a kernel object) but if you do not care about performance and responsiveness of the service use the Mutex simply. 如果您是WCF,我建议您使用“ bobbymcr”答案,但是对于旧版Web服务,您可以改用Monitor或Mutex,因为Mutex的成本很高(因为它是内核对象),但是如果您不关心服务的性能和响应速度,请使用互斥量简单。

See this sample for using Monitor class 有关使用Monitor类的信息,请参见此示例

private static object lockObject = new object();

public void SingleMethod()
{
try
{
Monitor.TryEnter(lockObject,millisecondsTimeout);
//method code
}
catch
{
}
finally
{
Monitor.Exit(lockObject);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM