Question

I've been developing a WCF Service where clients logon with their guid that they obtain when logging in to the client and the guid gets registered at the WCF Service. But every once in a while... about almost everytime I logon with the same user login. The guid from user 'x' gets re-used..

This is the code:

public void Logon(ReceiveClient rc, string name)
    {
        try
        {
            inst = new InstanceContext(rc);
            DMSNotificationClient = new DMSCSSendServiceClient(inst);

            var ci = Guid.NewGuid();
            ClientId = ci;

            LoggedOnSince = DateTime.Now;

            DMSNotificationClient.Logon(name);

            ReceiveMsg += OnReceiveMsg;
            FolderChange += OnFolderChange;

        }
        catch (Exception ex)
        {
            var dmsEx = new DmsException(ex);
            DmsException.WriteErrorsToLog(dmsEx);
        }
    }

After some analyses of my C# Code I'm asking you this question:

Can I say that a GUID is NOT unique?

Was it helpful?

Solution 2

Guids can be considered unique, as per the other answers.

Given that, the problem will lie in the ClientId property which must be either static or something similar (even ThreadStatic), which will cause the logon sessions to appear to "blend":

  • Session 1 Login ClientId == GuidA
  • Session 2 Login ClientId == GuidB
  • Session 1 Service Call ClientId == GuidB (as it was overridden by Session 2)

Edit

So just to clarify, static variables are available to all threads (therefore all requests) in an AppDomain. Therefore there is only one such variable, and each request is updating that same variable. How to avoid this in your scenario? ... that would be a different question.

OTHER TIPS

From MSDN:

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

In certain cases yes. In this specific case, no. The method you are calling clearly states that

The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low.

So the chance is decidedly non-zero. Admittedly I would expect "very low" to be in the range of "astronomically low" so I don't know why you get duplicates so easily, but the fact is that you cannot blindly trust the generated guid to be unique.

Can I say that a GUID is NOT unique?

No. See:

about almost everytime I logon with the same user login. The guid from user 'x' gets re- used..

So hey do not regenerate it but look up an existing Guid in a database. Simple like that. They do not use the Guid as "unique per session" but it defines a specific user.

This has nothing to do with GUID's guaranteed to be unique in creation because obviously they are not created for every login.

Technically, they're not (Pidgeon Hole Principle). But then again, it is very unlikely that you experience collisions on a regular basis. My best guess is that the reuse stems from some kind of caching, but I can't say more based on the information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top