ASMX Web 服务方法单例

发布于 2024-08-06 03:18:11 字数 326 浏览 4 评论 0 原文

不确定这是否是正确的术语,让我解释一下我想要的。

我有一个网络上可用的 Web 服务 - 该 Web 服务有 1 个 Web 方法。

我想要的是...如果 Web 服务正在运行并执行任务,并且对此 Web 服务进行了另一个调用,我希望第二个调用失败或挂起一段时间然后失败。因为一次只能调用此 Web 服务的 1 个实例。

我正在考虑向应用程序对象写入一个值(例如在asp.net中),但是我必须非常小心以确保该值得到更新,如果出现任何错误,它可能不会......所以这是危险,并且会使 Web 服务处于无人能访问的状态。

是否没有更动态的方法来确定 Web 服务是否被调用?

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.

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. Because only 1 instance of this web service should be called at once.

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.

Is there not a more dynamic way to determine if the web service is getting called or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

长伴 2024-08-13 03:18:11

您无法使用旧版 ASMX Web 服务执行此操作。他们不支持不同的实例方案。

我相信您可以使用 WCF 来做到这一点,因为您可以将服务配置为只有一个实例。

You cannot do this with legacy ASMX web services. 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.

雨巷深深 2024-08-13 03:18:11

如果您使用 WCF,这很简单。使用服务限制设置来指定您希望 MaxConcurrentCalls = 1 和 MaxInstances = 1。您还需要设置 ConcurrencyMode 更改为 Single >服务行为。

If you are using WCF, this is simple. 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.

夜灵血窟げ 2024-08-13 03:18:11

我对 Web 服务了解不多,不知道是否可以将 Web 服务器配置为仅启动 Web 服务的 1 个实例,但您可以尝试在 Web 服务中创建互斥体。

Mutex 是一个进程间同步对象,可用于检测 Web 服务的另一个实例是否正在运行。

因此,您可以做的是创建一个具有名称的互斥体,然后等待它。如果您的 Web 服务有超过 1 个实例处于活动状态,则互斥体将等待。

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.

A Mutex is an interprocess synchronization object which can be used to detect if another instance of your web service is running.

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.

感悟人生的甜 2024-08-13 03:18:11

您可以在 webmethod 内部实现检查,因为它将在同一个 IIS 进程中运行

You could implement the check inside of the webmethod since it will be running in the same IIS process

只涨不跌 2024-08-13 03:18:11

您可以创建一个穷人的互斥体,并让第一个实例创建一个文件,并让连续的实例检查该文件是否存在。尝试捕获您的网络方法并将文件的删除放在最后。

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.

-柠檬树下少年和吉他 2024-08-13 03:18:11

如果您是 WCF,我推荐“bobbymcr”答案,但对于旧版 Web 服务,您可以使用 Monitor 或互斥体,因为互斥体成本高昂(因为它是内核对象),但如果您不关心服务的性能和响应能力,请使用简单地说就是互斥体。

请参阅此示例以了解如何使用 Monitor 类

private static object lockObject = new object();

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

}

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.

See this sample for using Monitor class

private static object lockObject = new object();

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文