测试WCF服务时获取NullRef

本文关键字:获取 NullRef 服务 WCF 测试 | 更新日期: 2023-09-27 18:20:20

情况(Windows窗体):我创建了一个应用程序,比如说药房应用程序,它添加了患者、药物、医生。。。这是一个Winforms应用程序。我可以添加没有问题的患者。。。我有一个保存连接字符串的配置文件。我可以毫无问题地访问数据库。在页面加载中,Winform填充了4个组合框(2个字母的州、药物、医生、保险公司)。这是一个N层应用程序(UI、EntityObject(EO)、业务流程层(BPL)、数据访问层(DAL)、数据库(DB))。Winform流(WindowUI>BPL>DAL>DB)(这是内部网。

情境(Web表单、服务)我现在需要远程访问应用程序(web表单、Internet)。。。我在与窗口项目相同的解决方案中创建了一个WCF服务项目。我没有对winform应用程序的流进行任何更改。该服务直接追上BPL。建议的web from flow(WebUI>WCF服务>BPL>DAL>DB)

有一个实用程序类,其中包含一个DB opener方法,该方法将SQLCommand对象返回给BPL。

问题:当我尝试"测试"服务时,我在PharmUtil catch语句中得到了一个null引用,而我在运行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服务时获取NullRef

当您尝试测试WCF服务时,它可能正在作为webservice运行,并且正在使用web.config文件尝试检索连接字符串。当您运行WinForms应用程序时,WCF服务可能直接链接到WinForms项目中,在这种情况下,它使用WinForms应用程序中的app.config来检索连接字符串。

建议:检查WCF服务项目是否有web.config文件,并确保已配置数据库连接字符串。