Error - "An item with the same key has already been added" when retrieving Server information

Unanswered Error - "An item with the same key has already been added" when retrieving Server information

  • lundi 20 octobre 2008 14:51
     
     
    I have an application that gathers SQL server information for servers across the network and I'm seeing this error sporadically: "An item with the same key has already been added".  It happens on two separate lines:

    _Server.Information.NetName;
    and
    _Server.Databases.Count;

    where _Server is of type  Microsoft.SqlServer.Management.Smo.Server.  When I re-run the code, the problem goes away, and it's not consistent as of when it happens.  This started to happen when SMO 10 was installed, we never saw it with 9.0.

    Here is the stack trace for each one:
    _Server.Information.NetName:

       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at Microsoft.SqlServer.Management.Sdk.Sfc.FilterNodeAttribute..ctor(String name)
       at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(Axis ax)
       at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
       at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
       at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
       at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Load(AstNode ast)
       at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Compile(String strXPathExpression)
       at Microsoft.SqlServer.Management.Sdk.Sfc.UrnImpl.get_XPathExpression()
       at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req, Object ci)
       at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)
       at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetInitDataReader(String[] fields, OrderBy[] orderby)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ImplInitialize(String[] fields, OrderBy[] orderby)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.OnPropertyMissing(String propname, Boolean useDefaultValue)
       at Microsoft.SqlServer.Management.Smo.PropertyCollection.RetrieveProperty(Int32 index, Boolean useDefaultOnMissingValue)
       at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName, Boolean throwOnNullValue, Boolean useDefaultOnMissingValue)
       at Microsoft.SqlServer.Management.Smo.Information.get_NetName()
       at GetData() in C:\temp\Helper.cs:line 30


    _Server.Databases.Count:

       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetQueryTypeKeyFields(Type t)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitObjectsFromEnumResultsRec(SqlSmoObject currentSmoObject, XPathExpression levelFilter, Int32 filterIdx, IDataReader reader, Int32 columnIdx, Object[] parentRow, Boolean forScripting, List`1 urnList, Int32 startLeafIdx)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitObjectsFromEnumResults(Urn levelFilter, IDataReader reader, Boolean forScripting, List`1 urnList, Int32 startLeafIdx, Boolean skipServer)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitChildLevel(Urn levelFilter, ScriptingOptions so, Boolean forScripting)
       at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollection(Boolean refresh)
       at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_Count()
       at GetData() in C:\temp\Helper.cs:line 258

Toutes les réponses

  • lundi 20 octobre 2008 16:06
    Modérateur
     
     

    Hello,

     

    You have 2 problems in line 30 ( for _Server.Information.NetName ) and 258 ( for _Server.Databases.Count )

    I suggest to try these 2 problems separately.

     

    But before, could you tell us whether you have the problem with only one server always the same ?

    If it is the case, you may have a problem on the level of this server ( Edition/Version of this Server and the OS of the "workstation" where this servier is residing )

    Have you some trouble of response time on your network ?

     

    First for _Server.Information.NetName,please, could you post your code around the line 30 ? 

    Also , could you give some explanations about the definition of _Server ( how do you obtain it ? ) ?

     

    We are waiting for your feedback to try to help you more efficiently

     

    Have a nice day

     

     

  • lundi 20 octobre 2008 16:26
     
     
    Hello,
    These are 2 separate problems, they don't happen at the same time.  I just posted the details on both problems.  Sometimes the exceptions gets thrown from line 30 and sometimes from line 258.  Then I re-run the same exact code and the problem doesn't happen again.

    _Server is instantiated this way:

    using (SqlConnection _Connection = Connection.OpenConnection())
    {
    Server _Server = new Server(new ServerConnection(_Connection));
    InventoryData _InventoryData = new InventoryData();
    _InventoryData.serverName = _Server.Name;
    _InventoryData.hostComputer = _Server.Information.NetName;
    _InventoryData.reportDate = DateTime.Now
    ...
    ...
    ...
    }

    So those are lines 25 through 31, there's really not much interesting stuff going on around there.  The properties for InventoryData are of type String and DateTime, there's no logic in the accessors.  I'm seeing this in multiple scenarios, with different servers.  I'm even getting it agains my local sql server (no network involved).  We are running the code in a multi-threaded application, so I see it when gettting inventory data for 2 servers at the same time (separate threads), but the servers are completely separate servers.  
    Like I said before, this application has run with no problems for months and we are seeing the problem since SMO 10 got involved.

  • lundi 20 octobre 2008 17:50
    Modérateur
     
     

    Hello,

     

    I will search whether this problem has already reported.

     

    I have seen nothing abnormal in your code ( except i am never using "using", i prefer to use the try/catch method because i have the habit to treat the errors by myself )

     

    Have a nice day

     

  • lundi 20 octobre 2008 17:56
     
     
    Ok, thanks...but just to make sure we are on the same page, the using statement has nothing to do with try/catch, it's just used to let the runtime dispose of disposable objects by calling IDisposable methods, you can still wrap that code in a try catch and handle it manually.
  • vendredi 2 janvier 2009 14:18
     
     

    Hello,

    I have an application which is experiencing a problem very similar to the one EstebanFG reported.  It throws the same error ("An item with the same key has already been added") when attempting to read a property of an SMO server object.  My application is also multi-threaded, also only throws the error sporadically, and also only started doing so after I upgraded to SMO 10, prior to which it had been running daily for almost two years. 

    The error occurs when the application (on separate threads) tries to read the following property for multiple servers simultaneously:

    Server.Settings.DefaultFile

    Here's the stack trace:

    System.ArgumentException was unhandled by user code
      Message="An item with the same key has already been added."
      Source="mscorlib"
      StackTrace:
           at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
           at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
           at Microsoft.SqlServer.Management.Sdk.Sfc.FilterNodeAttribute..ctor(String name)
           at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(Axis ax)
           at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
           at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
           at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)
           at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Load(AstNode ast)
           at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Compile(String strXPathExpression)
           at Microsoft.SqlServer.Management.Sdk.Sfc.UrnImpl.get_XPathExpression()
           at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetObjectInfo(Object ci, RequestObjectInfo req)
           at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetObjectInfo(Object connectionInfo, RequestObjectInfo requestObjectInfo)
           at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.FixPropertyList(Object connectionInfo, Urn urn, String[] fields, RequestFieldsTypes requestFieldsType)
           at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)
           at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)
           at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetInitDataReader(String[] fields, OrderBy[] orderby)
           at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ImplInitialize(String[] fields, OrderBy[] orderby)
           at Microsoft.SqlServer.Management.Smo.SqlSmoObject.Initialize(Boolean allProperties)
           at Microsoft.SqlServer.Management.Smo.SqlSmoObject.OnPropertyMissing(String propname, Boolean useDefaultValue)
           at Microsoft.SqlServer.Management.Smo.PropertyCollection.RetrieveProperty(Int32 index, Boolean useDefaultOnMissingValue)
           at Microsoft.SqlServer.Management.Smo.PropertyCollection.GetValueWithNullReplacement(String propertyName, Boolean throwOnNullValue, Boolean useDefaultOnMissingValue)
           at Microsoft.SqlServer.Management.Smo.Settings.get_DefaultFile()
           at SQLRefreshAgent.RefreshAgent.RefreshDatabases() in C:\Projects\SQLRefresh 2.0\SQLDBRefreshAgent\RefreshAgent.vb:line 393
           at SQLRefreshAgent.RefreshAgent.RefreshDatabaseWorker_DoWork(Object Sender, DoWorkEventArgs e) in C:\Projects\SQLRefresh 2.0\SQLDBRefreshAgent\RefreshAgent.vb:line 360
           at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
           at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

    I would be very interested to know if anyone has identified a solution or workaround.

    Thanks!

  • lundi 2 février 2009 17:15
     
     

    Hi,

    I have a similar issue using Smo 10. Looks like a race condition in a shared state because this bugs never happen in single threaded code. I'm using different instance of Smo object on different thread and this bugs happens on the first run after a database restore. Probably a timing issue.

    {"An item with the same key has already been added."}

       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetQueryTypeKeyFields(Type t)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitObjectsFromEnumResultsRec(SqlSmoObject currentSmoObject, XPathExpression levelFilter, Int32 filterIdx, IDataReader reader, Int32 columnIdx, Object[] parentRow, Boolean forScripting, List`1 urnList, Int32 startLeafIdx)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.AdvanceInitRec(SqlSmoObject currentSmoObject, XPathExpression levelFilter, Int32 filterIdx, IDataReader reader, Int32 columnIdx, Int32 columnOffset, Object[] parentRow, Boolean forScripting, List`1 urnList, Int32 startLeafIdx)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitObjectsFromEnumResultsRec(SqlSmoObject currentSmoObject, XPathExpression levelFilter, Int32 filterIdx, IDataReader reader, Int32 columnIdx, Object[] parentRow, Boolean forScripting, List`1 urnList, Int32 startLeafIdx)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitObjectsFromEnumResults(Urn levelFilter, IDataReader reader, Boolean forScripting, List`1 urnList, Int32 startLeafIdx, Boolean skipServer)
       at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitChildLevel(Urn levelFilter, ScriptingOptions so, Boolean forScripting)
       at Microsoft.SqlServer.Management.Smo.Database.PrefetchObjects(ScriptingOptions options, IEnumerable`1 filters)
       at Microsoft.SqlServer.Management.Smo.Database.PrefetchTables(ScriptingOptions options, String tableFilter)
       at Microsoft.SqlServer.Management.Smo.Database.PrefetchObjectsImpl(Type objectType, ScriptingOptions scriptingOptions)

    Dany

  • lundi 2 mars 2009 20:52
     
     
     Hello,

    We are seeing a similar issue using Smo 10. Our application is also multi threaded, we are able to reproduce the issue nearly every time when connecting to a 2008 Sql Server Express database (used for SQL Replication), this code is generally run after the replicated database has been created or the snapshot update is run, which is similar to Dany L's note of seeing it after a database restore.

    Code:
    this.database = new Smo.Database(this.server, this.context.DataStoreName);
    this.database.Refresh(); //<-- Error occurs from this call

    ArgumentException: An item with the same key has already been added.

    Stacktrace:
        at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)  
        at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.FilterNodeAttribute..ctor(String name)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(Axis ax)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.FilterTranslate.decode(AstNode node)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Load(AstNode ast)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression.Compile(String strXPathExpression)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.UrnImpl.get_XPathExpression()  
        at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req, Object ci)  
        at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)  
        at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req)  
        at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetInitDataReader(String[] fields, OrderBy[] orderby)  
        at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ImplInitialize(String[] fields, OrderBy[] orderby)  
        at Microsoft.SqlServer.Management.Smo.SqlSmoObject.Initialize(Boolean allProperties)  
        at Microsoft.SqlServer.Management.Smo.SqlSmoObject.Refresh()  
        --- End of inner exception stack trace ---  
        at Microsoft.SqlServer.Management.Smo.SqlSmoObject.Refresh()  
        at Microsoft.SqlServer.Management.Smo.Database.Refresh()  
        at DatabaseController..ctor(DatabaseContext context)

    Thanks,
    Mike
  • dimanche 23 août 2009 18:15
     
     
    I'm seeing the same issue with smo version 10.  The app is multi-threaded. Each thread connects to a different SQL server instance and in the failing case, the two instances are on the same physical machine. Looks like it's an Smo bug.
  • dimanche 21 février 2010 15:05
     
     
    I have this same problem, and it does seem to be to do with multi-threaded use of SMO.

    You can find the solution here: http://support.microsoft.com/kb/971914/
  • jeudi 7 juillet 2011 16:35
     
     
    This bug seems to be back in 2008 R2 CU7.  It went away for a while, but now is back and driving me nuts because it kills our automated processes randomly.  It always fails when trying to disable/enable JOBS via SMO.
  • jeudi 27 octobre 2011 11:01
     
     

    Hello,

     

    Has this been fixed? I have an SMO component for SSIS that is constantly giving random errors with "ArgumentException: An item with the same key has already been added.".

     

    My SSIS version is "10.50.1797 - 2534352 Cumulative update package 8 (CU8) for SQL Server 2008 R2"

     

    Thank you


    Luis Simões
  • mardi 14 août 2012 21:05
    Modérateur
     
     

    Could you please report this issue to connect feedback https://connect.microsoft.com/sqlServer/feedback

    Please post a code sample that we can use to reproduce the problem you mentioned.

    Thanks

    Sethu Srinivasan [MSFT]

    SQL Server

    http://blogs.msdn.com/sqlagent