Samphore Silm usage in two diffrent project.

Dani_S 2,726 Reputation points
2024-04-07T10:44:27.83+00:00

Hi,

I have maui app in windows, .NET 8.

I have also worker service in NET 8.

I have class librarary common to both projects that use Samphore Silm.

SessionTokenSingelton.GetInstance().GetSessionToken(key);

Both apps running on same machine.

Does every app has its own copy or since Samphore Silm it cross processes is the same for both apps?

Thanks,

namespace XXXXXX

{

public class SessionTokenSingelton

{

    /// <summary>

    /// The class instance as singelton

    /// </summary>

   private static SessionTokenSingelton _sessionTokenSingelton;


   

    /// <summary>

    /// The lock object

    /// </summary>

    private static readonly object _syncObject = new object();

    /// <summary>

    /// The samfor slim

    /// </summary>

    private static readonly SemaphoreSlim _semLock = new SemaphoreSlim(1, 1);

    private SessionTokenSingelton()

    {

    }

    /// <summary>

    /// Get one insatnce of SessionTokenSingelton using double-check locking

    /// </summary>

    /// <returns>Returns one insatnce of RoutesStatusSingelton using double-check locking</returns>

    public static SessionTokenSingelton GetInstance()

    {

        if (_sessionTokenSingelton == null)

        {

            lock (_syncObject)

            {

                if (_sessionTokenSingelton == null)

                {

                    _sessionTokenSingelton = new SessionTokenSingelton();

                }

            }

        }

        return _sessionTokenSingelton;

    }


    

    public async Task<string> GetSessionToken(string key)

    {


        

        await _semLock.WaitAsync();

        try

        {

          // critical code

             return token;

        }

        catch(Exception ex)

        {                

            return null;

        }

        finally

        {

            _semLock.Release();

        }

    }

}
```}

.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,120 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,286 Reputation points
    2024-04-07T16:13:44.0933333+00:00

    Semaphore Silm Is tried to the process. Use windows o/s global semaphores if you want share between processes.

    https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createsemaphorew?redirectedfrom=MSDN


  2. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,571 Reputation points Microsoft Vendor
    2024-04-08T03:14:32.6933333+00:00

    Hello,

    Semaphores are of two types: local semaphores and named system semaphores. Local semaphores are local to an application, system semaphores are visible throughout the operating system and are suitable for inter-process synchronization. The SemaphoreSlim is a lightweight alternative to the Semaphore class that doesn't use Windows kernel semaphores. Unlike the Semaphore class, the SemaphoreSlim class doesn't support named system semaphores. You can use it as a local semaphore only. The SemaphoreSlim class is the recommended semaphore for synchronization within a single app. Update:

    Although Semaphore's API documentation mentions that communication can be done via system semaphores. However, system semaphores cannot be declared by a program.

    Tokens are obviously a custom persistence variable, which means that this cannot be achieved with Semaphore.

    For your needs, since the token is in its lifetime, there are two programs that need to read it. Therefore, it's really a persistence problem for a variable rather than a synchronization problem.

    For variable persistence, you can easily do this through a file.

    For file persistence, you can write it to a file when the token is generated. After that, you can read this file from the Worker and Client to verify the token.

    Since you only need to read and write to a file, you can refer to this official example.

    File Class Examples

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.