簡體   English   中英

測試WCF服務時獲取NullRef

[英]Getting NullRef When Testing WCF Service

情況(Windows窗體):我創建了一個應用程序,比如說一個葯房應用程序,它可以添加患者,葯物,醫生...這是Winforms應用程序。 我可以毫無問題地添加患者...我有一個包含連接字符串的配置文件。 我可以毫無問題地訪問數據庫。 在頁面加載時,Winform會填充4個ComboBox(2個字母狀態,葯物,醫生,保險公司)。 這是一個N層應用程序(UI,EntityObject(EO),業務流程層(BPL),數據訪問層(DAL),數據庫(DB))。 Winform流(WindowUI> BPL> DAL> DB)(這是Intranet。

情況(Web窗體,服務)現在,我需要對應用程序(Web窗體,Internet)具有遠程訪問權限...我已經以與窗口項目相同的解決方案創建了WCF服務項目。 我沒有對Winform應用程序的流程進行任何更改。 該服務直接追隨BPL。 流程中的擬議網站(WebUI> WCF服務> BPL> DAL> DB)

有一個實用程序類,其中包含一個DB Opener方法,該方法將SQLCommand對象返回到BPL。

問題:當我嘗試“測試”該服務時,在PharmUtil catch語句中得到的是空引用,而運行Windows窗體時卻沒有得到。 我想念什么...?

"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 ; 
>      }

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM