Guid is all 0's (zeros)?

C#.NetWcfWeb ServicesGuid

C# Problem Overview


I'm testing out some WCF services that send objects with Guids back and forth. In my web app test code, I'm doing the following:

var responseObject = proxy.CallService(new RequestObject
{
    Data = "misc. data",
    Guid = new Guid()
});

For some reason, the call to new Guid() is generating Guids with all 0's (zeros) like this:

00000000-0000-0000-0000-000000000000

What could be causing this?

C# Solutions


Solution 1 - C#

Use the static method Guid.NewGuid() instead of calling the default constructor.

var responseObject = proxy.CallService(new RequestObject
{
    Data = "misc. data",
    Guid = Guid.NewGuid()
});

Solution 2 - C#

Lessons to learn from this:

  1. Guid is a value type, not a reference type.

  2. Calling the default constructor new S() on any value type always gives you back the all-zero form of that value type, whatever it is. It is logically the same as default(S).

Solution 3 - C#

Try this instead:

var responseObject = proxy.CallService(new RequestObject
{
    Data = "misc. data",
    Guid = new Guid.NewGuid()
});

This will generate a 'real' Guid value. When you new a reference type, it will give you the default value (which in this case, is all zeroes for a Guid).

When you create a new Guid, it will initialize it to all zeroes, which is the default value for Guid. It's basically the same as creating a "new" int (which is a value type but you can do this anyways):

Guid g1;					// g1 is 00000000-0000-0000-0000-000000000000
Guid g2 = new Guid();		// g2 is 00000000-0000-0000-0000-000000000000
Guid g3 = default(Guid);	// g3 is 00000000-0000-0000-0000-000000000000
Guid g4 = Guid.NewGuid();	// g4 is not all zeroes

Compare this to doing the same thing with an int:

int i1;						// i1 is 0
int i2 = new int();			// i2 is 0
int i3 = default(int);		// i3 is 0

Solution 4 - C#

Try doing:

Guid foo = Guid.NewGuid();

Solution 5 - C#

Can't tell you how many times this has caught. me.

Guid myGuid = Guid.NewGuid(); 

Solution 6 - C#

In the spirit of being complete, the answers that instruct you to use Guid.NewGuid() are correct.

In addressing your subsequent edit, you'll need to post the code for your RequestObject class. I'm suspecting that your guid property is not marked as a DataMember, and thus is not being serialized over the wire. Since default(Guid) is the same as new Guid() (i.e. all 0's), this would explain the behavior you're seeing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDidaxisView Question on Stackoverflow
Solution 1 - C#Mark ByersView Answer on Stackoverflow
Solution 2 - C#Eric LippertView Answer on Stackoverflow
Solution 3 - C#JohnDView Answer on Stackoverflow
Solution 4 - C#Dylan SmithView Answer on Stackoverflow
Solution 5 - C#Matt DawdyView Answer on Stackoverflow
Solution 6 - C#Adam RobinsonView Answer on Stackoverflow