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