tags:

views:

352

answers:

2

Can someone advice me how to prevent this error. An item with the same key has already been added.?

//  Failed to find a matching SessionFactory so make a new one.
        if (sessionFactory == null)
        {
            Check.Require(File.Exists(sessionFactoryConfigPath),
                          "The config file at '" + sessionFactoryConfigPath + "' could not be found");

            Configuration cfg = new Configuration();
            cfg.Configure(sessionFactoryConfigPath);

            /*MINE*/
            var persistenceModel = new PersistenceModel();
            persistenceModel.AddMappingsFromAssembly(Assembly.Load("EMedicine.Core"));
            persistenceModel.Configure(cfg);
            /*END_OF_MINE*/

            //  Now that we have our Configuration object, create a new SessionFactory
            sessionFactory = cfg.BuildSessionFactory();

            if (sessionFactory == null)
            {
                throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
            }

            if (sessionFactoryConfigPath != null) sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
        }

Error is here: sessionFactory = cfg.BuildSessionFactory();

A: 

Try the following:

if (
    sessionFactoryConfigPath != null && 
    sessionFactories.ContainsKey(sessionFactoryConfigPath)
) {
    sessionFactory = cfg.BuildSessionFactory();

    if (sessionFactory == null)
    {
        throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
    }

    sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
} else (sessionFactoryConfigPath != null) {
    sessionFactory = sessionFactories[sessionFactoryConfigPath];
}
Obalix
Thx for your answer but now i get Object reference not set to an instance of an object in second else statementif (interceptor != null) { session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(interceptor); } else { session = GetSessionFactoryFor(sessionFactoryConfigPath).OpenSession(); }
senzacionale
Just retrieve the existing sessionFactory ... see Edit.
Obalix
senzacionale
nop my idea is not ok...
senzacionale
If you are using VS see what variable is null. And then try to find out why it is null. From what I have seen from your code I would look at the session creation above and try to find out why the session is not created.
Obalix
All the time is problem this line: sessionFactory = cfg.BuildSessionFactory(); Problem is that if connect to app 4 times at the same time crashes.
senzacionale
Obalix what if i use try catch block? Is it smart?
senzacionale
Not really, IMHO it is smarter to find another way to limit the executions of this section of code. One way could be the use of a singleton, but I do not know whether this fits your application design. Another way could be to use a flag that indicates whether the section of code has executed. And yet another, if you have multiple threads, could be the use of a `lock (lockobject) { }` block. But as I said it depends upon the requriements and the design of you application.
Obalix
Hi, thx for your answer.Configuration cfg = new Configuration();cfg.Configure(sessionFactoryConfigPath);Found the problem. I need to check if key (cfg.BuildSessionFactory()) already exist. Maybe do you know any methods for this?
senzacionale
A: 

Is this solution OK?

try { // Now that we have our Configuration object, create a new SessionFactory sessionFactory = cfg.BuildSessionFactory();

                    if (sessionFactory == null)
                    {
                        throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
                    }

                    if (sessionFactoryConfigPath != null)
                        sessionFactories.Add(sessionFactoryConfigPath, sessionFactory);
                }
                catch (Exception)
                { 
                    if (sessionFactoryConfigPath != null)
                        sessionFactory = (ISessionFactory) sessionFactories[sessionFactoryConfigPath];
                }
senzacionale