ResourceType not thread safe RRS feed

  • Question

  • Now, I'm not sure whether ResourceType needs to be thread safe or not, but it isn't. I'm caching ResourceType instances away, and reusing them between multiple instances of my metadata provider. When I issue a high number of queries against the data service I sometimes encounter an exception that appears to be related to a race, coming from ResourceType.GetPropertyInfoDecaredOnThisType. Looks like a key that is already in a dictionary is being added.


    If ResourceType isn't supposed to be thread safe, are you supposed to create the entire ResourceType/Set hierarchy per instance of the metadata provider? That seems like hard work.

    Thursday, April 14, 2011 8:53 PM

All replies

  • Hi,

    First make sure that you mark all your resource types as read-only (SetReadOnly) before you start handing them out on several threads. The resource types should be thread safe only AFTER you call SetReadOnly.

    Regardless there might be some problem - we will look more into this. Meanwhile you can use this trick to make it work though:

    After you call SetReadOnly, but still before you start using the types from multiple threads access the ResourceType.PropertiesDeclaredOnThisType property (or the Properties property). This will internally initialize the dictionary in question and you should not be hitting the problem anymore.


    Vitek Karas [MSFT]
    Friday, April 15, 2011 4:48 PM
  • Yeah. I tried to do this, but it hasn't addressed the issue. I'll keep looking.
    Friday, April 15, 2011 5:21 PM
  • Maybe if you could share a bit more about what you're doing...

    Do you ever inherit from the ResourceType or ResourceProperty?

    Do you use virtual properties (the LoadPropertiesDeclaredOnThisType method)?

    Full stacktrace of the exception would help a lot.


    Vitek Karas [MSFT]
    Friday, April 15, 2011 6:42 PM