2010-11-19 42 views
0

我有一個.NET 3.5解決方案與一個asp.net(網站)項目與fluentnhibernate和它的測試項目(類庫項目)。我引用了測試項目中的asp.net項目以及所有fluentnhibernate/nhibenate dll。NullReferenceExpection在一個asp.net nhibernate解決方案的集成測試項目

我不明白的是,在運行webform(從瀏覽器命中)時,讓我們說Test.aspx,架構的構建是成功的,我可以看到我的數據庫中的表。 這裏是我的Test.aspx.cs

致電
public partial class Test : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     ISession session = SessionManager.Instance.OpenSession(); 
     SessionManager.BuildSchema(session); 
    } 
} 

我碰巧使用在我的測試類的CanGenerateSchema方法相同的方法方法,它總是失敗

[TestFixture] 
public class CanGenerateSchemaTestSuite 
{ 
    [Test] 
    public void CanGenarateSchema() 
    { 
     ISession session = SessionManager.Instance.OpenSession(); 
     SessionManager.BuildSchema(session); 
    } 
} 

這裏是SessionManager我使用:

public sealed class SessionManager 
{ 
    private readonly ISessionFactory _sessionFactory; 


    public static ISessionFactory SessionFactory 
    { 
     get { return Instance._sessionFactory; } 
    } 


    private ISessionFactory GetSessionFactory() 
    { 
     return _sessionFactory; 
    } 

    public static SessionManager Instance 
    { 
     get { return NestedSessionManager._sessionManager; } 
    } 

    public ISession OpenSession() 
    { 
     return Instance.GetSessionFactory().OpenSession(); 
    } 


    private static Configuration SaveConfigs; 

    private SessionManager() 
    { 
     try 
     { 

      if (_sessionFactory == null) 
      { 
      //from the debugging the code breaks from here when trying to get connectionstring. 
       string constring = ConfigurationManager.AppSettings["localdb"].ToString(); 
       FluentConfiguration configuration = Fluently.Configure() 
        .Database(
         MsSqlConfiguration.MsSql2005.ConnectionString(constring)) 
        .Mappings(m => 
        { 
         m.FluentMappings.AddFromAssemblyOf<myproject.model.Request>(); 
         m.FluentMappings.AddFromAssemblyOf<myproject.model.Route>(); 
        }) 
        .ExposeConfiguration((x) => 
        { 
         SaveConfigs = x; 
         x.SetProperty("current_session_context_class", "thread_static"); 

        }); 
       _sessionFactory = configuration.BuildSessionFactory(); 
      } 
     } 
     catch (Exception ex) 
     { 
      Console.Write(ex.Message); 

     } 

    } 


    public static void BuildSchema(ISession session) 
    { 
     var export = new SchemaExport(SaveConfigs); 
     export.Execute(false,true,false,session.Connection,null); 
    } 

    class NestedSessionManager 
    { 
     internal static readonly SessionManager _sessionManager = new SessionManager(); 
    } 
} 

所以從我的評論NullReferenceException發生在訪問connectionstring時。我沒有解釋爲什麼會發生這種情況。我肯定這是一些棘手的問題,我無法克服它。如果有人能在這裏給我一隻手,我將非常感激。謝謝你的閱讀。

回答

1

ConfigurationManager.AppSettings["localdb"] from Test.aspx將從您的web.config文件在Web項目中拉動。

該文件將無法訪問您的測試項目(我假設您的測試是從您的網站單獨的項目)。你應該能夠通過使用正確的localdb設置將app.config文件添加到測試項目中,或者使用FluentNHibernate的流暢構建器而不是使用配置字符串來解決此問題。

舉例app.config文件:

<?xml version="1.0"?> 
<configuration> 
<appSettings> 
    <add key="localdb" value="yourconnectionstring" /> 
</appSettings> 
</configuration> 
+0

thanks.i想過這一點,在我看來,這將是多餘的具有測試project.It另一ConnectionString的工作現在。但這是一種好的做法嗎?只是一個問題 – 2010-11-19 19:05:48

+0

這取決於它變化的頻率。如果你還沒有,也許會想到Fluent Builders,那麼你的代碼就被編譯在一個地方。 http://wiki.fluentnhibernate.org/Database_configuration – 2010-11-19 19:09:57

0

如果ConfigurationManager.AppSettings["localdb"]返回的值是null那麼.ToString()調用將導致NullReferenceException

確保存在「localdb」設置。

相關問題