如何在分布式.Net应用程序中一次只能让一个用户访问资源(屏幕)?

时间:2022-09-11 18:51:47

I have a client server based windows forms application that needs an administrator only screen. The administrator functionality needs to be implemented in such a way that at any given time only one administrator can access that screen. The windows forms client application talks to the server using .NET Remoting. And the server side is distributed on multiple machines.

我有一个基于客户端服务器的Windows窗体应用程序,只需要一个管理员屏幕。管理员功能需要以这样的方式实现,即在任何给定时间只有一个管理员可以访问该屏幕。 windows使用.NET Remoting形成客户端应用程序与服务器的对话。服务器端分布在多台机器上。

2 个解决方案

#1


3  

This can be done most easily through using a DB table. Typically the DB is already fault tolerant and is a safe resource to use for a shared lock scenario. Just have a lock table that contains some info about the locked resource, who has it, when, etc.

这可以通过使用DB表最容易地完成。通常,DB已具有容错能力,是用于共享锁定方案的安全资源。只需要一个锁表,其中包含有关锁定资源的一些信息,拥有它的人,何时等。

You can also use one of the various "state servers" on the market to store lock state. This is introducing a failure point though, unless you invest in one of the newer distributed state technologies.

您还可以使用市场上的各种“状态服务器”之一来存储锁定状态。这会引入一个失败点,除非您投资于一种较新的分布式状态技术。

However, you are really setting yourself up for further issues. You'll need a screen to allow forcing an unlock, viewing who has the lock, etc. You're best off looking at why this lock is really needed. Is it a technology or business requirement? You can more easily and cleanly implement a pessimistic data updating scenario which would improve user experience.

但是,你真的为自己的问题做好了准备。你需要一个屏幕来强制解锁,查看谁有锁等等。你最好看看为什么真的需要这个锁。这是技术还是业务要求?您可以更轻松,更干净地实施悲观数据更新方案,从而改善用户体验。

#2


0  

You need to maintain a static member on the server that indicates if the window is currently in use. Make sure it is thread-safe by using the lock() function when setting the value. You can then check that value before showing the admin screen.

As far as the server side is concerned, you mention that it exists on multiple PC's. Is this a load balanced kind of topolgy which operate as one virtual server? If so you may need to persist the value in a database. Think of it in the same way that ASP.Net persists Session State. I can exist on server, but if there is a server farm it can be moved to SQL Server for use by all servers.

您需要在服务器上维护一个静态成员,指示该窗口当前是否正在使用中。设置值时,使用lock()函数确保它是线程安全的。然后,您可以在显示管理员屏幕之前检查该值。就服务器端而言,您提到它存在于多台PC上。这是一种负载均衡的topolgy,它作为一个虚拟服务器运行吗?如果是这样,您可能需要将值保留在数据库中。可以想象它与ASP.Net持续会话状态的方式相同。我可以存在于服务器上,但如果有服务器场,则可以将其移动到SQL Server以供所有服务器使用。

#1


3  

This can be done most easily through using a DB table. Typically the DB is already fault tolerant and is a safe resource to use for a shared lock scenario. Just have a lock table that contains some info about the locked resource, who has it, when, etc.

这可以通过使用DB表最容易地完成。通常,DB已具有容错能力,是用于共享锁定方案的安全资源。只需要一个锁表,其中包含有关锁定资源的一些信息,拥有它的人,何时等。

You can also use one of the various "state servers" on the market to store lock state. This is introducing a failure point though, unless you invest in one of the newer distributed state technologies.

您还可以使用市场上的各种“状态服务器”之一来存储锁定状态。这会引入一个失败点,除非您投资于一种较新的分布式状态技术。

However, you are really setting yourself up for further issues. You'll need a screen to allow forcing an unlock, viewing who has the lock, etc. You're best off looking at why this lock is really needed. Is it a technology or business requirement? You can more easily and cleanly implement a pessimistic data updating scenario which would improve user experience.

但是,你真的为自己的问题做好了准备。你需要一个屏幕来强制解锁,查看谁有锁等等。你最好看看为什么真的需要这个锁。这是技术还是业务要求?您可以更轻松,更干净地实施悲观数据更新方案,从而改善用户体验。

#2


0  

You need to maintain a static member on the server that indicates if the window is currently in use. Make sure it is thread-safe by using the lock() function when setting the value. You can then check that value before showing the admin screen.

As far as the server side is concerned, you mention that it exists on multiple PC's. Is this a load balanced kind of topolgy which operate as one virtual server? If so you may need to persist the value in a database. Think of it in the same way that ASP.Net persists Session State. I can exist on server, but if there is a server farm it can be moved to SQL Server for use by all servers.

您需要在服务器上维护一个静态成员,指示该窗口当前是否正在使用中。设置值时,使用lock()函数确保它是线程安全的。然后,您可以在显示管理员屏幕之前检查该值。就服务器端而言,您提到它存在于多台PC上。这是一种负载均衡的topolgy,它作为一个虚拟服务器运行吗?如果是这样,您可能需要将值保留在数据库中。可以想象它与ASP.Net持续会话状态的方式相同。我可以存在于服务器上,但如果有服务器场,则可以将其移动到SQL Server以供所有服务器使用。