【问题标题】:Getting NullRef When Testing WCF Service测试 WCF 服务时获取 NullRef
【发布时间】:2014-09-02 20:37:35
【问题描述】:

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

情况(Web 表单、服务) 我现在需要远程访问应用程序(Web 表单、Internet)...我在与窗口项目相同的解决方案中创建了一个 WCF 服务项目。我没有对 winform 应用程序的流程进行任何更改。该服务直接在 BPL 之后进行。从流中提出的 Web(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 ; 
>      }

【问题讨论】:

  • 当它在 pharmutil 类中命中时,它会下降到 nullref 捕获:conn.ConnectionString = > ConfigurationManager.ConnectionStrings["PharmaConn"].ToString();但是,当 Windows 窗体运行时...没有问题...仅来自服务调用...仅供参考,我目前正在使用 WCF 测试客户端进行测试...

标签: c# .net winforms web-services wcf


【解决方案1】:

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

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

【讨论】:

  • 那行得通...但是,为什么服务调用会从 app.config 获取计数(web.conig 中没有连接字符串部分)然后尝试从网络获取连接字符串。配置?
猜你喜欢
  • 1970-01-01
  • 2016-02-17
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
相关资源
最近更新 更多