2014-09-02 99 views
0

情況(Windows窗體): 我創建了一個應用程序讓我們說一個藥房應用程序,添加患者,藥物,醫生...這是一個Winforms應用程序。我可以添加沒有問題的患者......我有一個保存連接字符串的配置文件。我可以毫無問題地訪問數據庫。在頁面加載時,Winform會填充4個組合框(2個字母狀態,藥物,醫生,保險公司)。這是一個N層應用程序(UI,EntityObject(EO),業務流程層(BPL),數據訪問層(DAL),數據庫(DB))。 Winform流程(WindowUI> BPL> DAL> DB)(這是內部網絡)測試WCF服務時獲取NullRef

情況(Web窗體,服務) 我現在需要遠程訪問應用程序(Web表單,Internet)我已經在窗口項目的同一個解決方案中創建了一個WCF服務項目,我沒有改變winform應用程序的流程,這個服務直接跟在BPL之後,從流程提交web(WebUI> WCF Service> BPL> DAL> DB )

有容納返回SqlCommand對象的BPL一個DB首戰方法的實用工具類

問題: 當我嘗試「測試」的服務,我收到了nullref在我運行Windows窗體時沒有得到的PharmUtil catch語句中。我錯過了什麼......?

"Call to the BPL from the service:" 
    Namespace pharmapp 
    public DataSet StateDS() 
    { 
     return StateBPL.StateFillDS(); //returns the state dataset to the service consumer 
    } 

    "Calls the util class and the DAL" 
     Namespace pharmapp 
     public class StateBPL 
     { 
      Public static DataSet StateFillDS() 
      { 
       var cmdobj = new SQLCommand(); 
       cndobj = PharmAppUtil.OpenDB(); //calls the utilities class 
       cmdobj.commandText = "spGetStates"; //adds stored procedure name 
       return StateDAL.StateDSFill(cmdobj); // call the Data access class and 
                //returns a dataset 
      } 
     } 

> "Utilities calls returns a command object to the BPL minus the 
> stored procedure name" 
>  Utilties Class: 
     Namespace pharmapp 
>  public class PharmAppUtil 
>  { 
>   SqlConnection conn = new SqlConnection(); 
>   public static SqlCommand OpenDB() 
>   { 
>    SqlConnection conn = new SqlConnection(); 
>    try 
>    { 
>    var connSettings = ConfigurationManager.ConnectionStrings; 
>    if (connSettings.Count < 1) 
>    { 
>     Debug.WriteLine("NO Connection string found."); 
>     return null; 
>    } 
>    else 
>    { 
>     conn.ConnectionString = 
>      ConfigurationManager.ConnectionStrings["PharmaConn"].ToString(); 
>     conn.Open(); 
>     SqlCommand cmd = new SqlCommand(); 
>     cmd.Connection = conn; 
>     cmd.CommandType = CommandType.StoredProcedure; 
>     return cmd; 
>    } 
>    } 
>    catch (SqlException ex) 
>    { 
>     Debug.WriteLine(ex.Message); 
>     return null; 
>    } 
>    catch (NullReferenceException ex) 
>    { 
>     Debug.WriteLine(ex.Message); 
>     return null; 
>    } 
>    finally 
>    { 
> 
>    } 
>    //return ; 
>  } 
+0

當它在pharmutil類中命中時,它將落入nullref catch:conn.ConnectionString = > ConfigurationManager.ConnectionStrings [「PharmaConn」]。ToString();但是,當Windows窗體運行...沒有問題...只有從服務調用...僅供參考,我使用WCF測試客戶端進行測試... – Suge 2014-09-02 21:00:49

回答

0

當你嘗試測試WCF服務,它可能正在運行的web服務,並使用web.config文件,試圖獲取連接字符串。當您運行WinForms應用程序時,WCF服務可能直接鏈接到Winforms項目中,並且在這種情況下,它使用winforms應用程序中的app.config檢索連接字符串。

建議:檢查您的WCF服務項目是否有web.config文件,並確保它已配置數據庫連接字符串。

+0

這工作......但是,爲什麼服務調用會從app.config獲得計數(web.conig中沒有連接字符串部分),然後嘗試從web.config獲取connstring? – Suge 2014-09-03 12:07:38