繁体   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