是否由Guid.NewGuid()返回了重复项? [英] Duplicate returned by Guid.NewGuid()?

查看:279
本文介绍了是否由Guid.NewGuid()返回了重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序可以为我们的一项服务生成模拟数据,以进行测试.每个数据项都有一个唯一的Guid.但是,当我们对模拟器进行一些小的代码更改后进行测试时,由它生成的所有对象都具有相同的Guid.

We have an application that generates simulated data for one of our services for testing purposes. Each data item has a unique Guid. However, when we ran a test after some minor code changes to the simulator all of the objects generated by it had the same Guid.

创建了一个数据对象,然后创建了一个for循环,在该循环中修改了对象的属性,包括一个新的唯一Guid,并通过远程处理将其发送到服务(可序列化,而不是按编组,如果这就是您的想法),循环然后再做一次,等等.

There was a single data object created, then a for loop where the properties of the object were modified, including a new unique Guid, and it was sent to the service via remoting (serializable, not marshal-by-ref, if that's what you're thinking), loop and do it again, etc.

如果我们在循环中放入一个小的Thread.Sleep(...),它将生成唯一的ID.我认为那是一条红鲱鱼.我创建了一个测试应用程序,它只是一个接一个地创建了一个guid,而没有一个重复.

If we put a small Thread.Sleep( ...) inside of the loop, it generated unique id's. I think that is a red-herring though. I created a test app that just created one guid after another and didn't get a single duplicate.

我的理论是,IL以导致此行为的方式进行了优化.但是关于我的理论已经足够了.你怎么看?我愿意接受建议和测试方法.

My theory is that the IL was optimized in a way that caused this behavior. But enough about my theories. What do YOU think? I'm open to suggestions and ways to test it.

更新:关于我的问题似乎有很多困惑,所以让我澄清一下.我不认为NewGuid()已损坏.显然,它可以工作.没关系!但是,某个地方存在一个错误,该错误会导致NewGuid()之一: 1)在我的循环中仅被调用一次 2)在我的循环中每次都被调用,但仅分配一次 3)我没想到的其他事情

UPDATE: There seems to be a lot of confusion about my question, so let me clarify. I DON'T think that NewGuid() is broken. Clearly it works. Its FINE! There is a bug somewhere though, that causes NewGuid() to either: 1) be called only once in my loop 2) be called everytime in my loop but assigned only once 3) something else I haven't thought of

此错误可能存在于我的代码中(可能是MOST)或在某个地方的优化中.

This bug can be in my code (MOST likely) or in optimization somewhere.

所以要重申我的问题,我应该如何调试这种情况?

So to reiterate my question, how should I debug this scenario?

(并且感谢您的精彩讨论,这确实有助于我澄清自己的想法)

(and thank you for the great discussion, this is really helping me clarify the problem in my mind)

更新#2:我很想发布一个显示问题的示例,但这是我的问题的一部分.我无法在整个应用程序套件(客户端和服务器)之外复制它.

UPDATE # 2: I'd love to post an example that shows the problem, but that's part of my problem. I can't duplicate it outside of the whole suite of applications (client and servers).

这是一个相关的代码段:

Here's a relevant snippet though:

OrderTicket ticket = new OrderTicket(... );

for( int i = 0; i < _numOrders; i++ )
{
    ticket.CacheId = Guid.NewGuid();
    Submit( ticket );  // note that this simply makes a remoting call
}

推荐答案

Submit是否进行异步调用,或者票证对象在任何阶段都进入另一个线程.

Does Submit do an async call, or does the ticket object go into another thread at any stage.

在代码示例中,您正在重用同一对象.如果Submit在短暂的延迟后(不进行复制)在后台线程中发送票证该怎么办.当您更改CacheId时,实际上是在更新所有待处理的提交.这也解释了为什么Thread.Sleep可以解决此问题.试试这个:

In the code example you are reusing the same object. What if Submit sends the ticket in a background thread after a short delay (and does not take a copy). When you change the CacheId you are actually updating all the pending submits. This also explains why a Thread.Sleep fixes the problem. Try this:

for( int i = 0; i < _numOrders; i++ )
{
    OrderTicket ticket = new OrderTicket(... );
    ticket.CacheId = Guid.NewGuid();
    Submit( ticket );  // note that this simply makes a remoting call
}

如果由于某种原因无法实现,请尝试以下操作,看看它们是否仍然相同:

If for some reason this is not possible, try this and see if they are still the same:

ticket.CacheId = new Guid("00000000-0000-0000-0000-" + 
     string.Format("{0:000000000000}", i));

这篇关于是否由Guid.NewGuid()返回了重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆