简体   繁体   English

测试WCF服务时获取NullRef

[英]Getting NullRef When Testing WCF Service

Situation (Windows Form): I've created an application lets say a Pharmacy Application that add's patients, medications, Doctors... This is a Winforms application. 情况(Windows窗体):我创建了一个应用程序,比如说一个药房应用程序,它可以添加患者,药物,医生...这是Winforms应用程序。 I can add patients with no problems... I have a configuration file that holds connection string. 我可以毫无问题地添加患者...我有一个包含连接字符串的配置文件。 I can access the DB with no problem. 我可以毫无问题地访问数据库。 On page load, the Winform populates 4 ComboBoxes (2 letter states, medications, doctors, Insurance companies). 在页面加载时,Winform会填充4个ComboBox(2个字母状态,药物,医生,保险公司)。 This is an N-Tier application (UI, EntityObject (EO), Business Process Layer (BPL), Data Access Layer(DAL), Database (DB)). 这是一个N层应用程序(UI,EntityObject(EO),业务流程层(BPL),数据访问层(DAL),数据库(DB))。 The Winform flow (WindowUI > BPL > DAL > DB) (This is intranet. Winform流(WindowUI> BPL> DAL> DB)(这是Intranet。

Situation (Web Form, Service) I now need to have remote access to the application (web form, Internet)... I've created a WCF service project in the same solution as the window's project. 情况(Web窗体,服务)现在,我需要对应用程序(Web窗体,Internet)具有远程访问权限...我已经以与窗口项目相同的解决方案创建了WCF服务项目。 I've made no changes to the winform application's flow. 我没有对Winform应用程序的流程进行任何更改。 The service goes after the BPL directly. 该服务直接追随BPL。 Proposed web from flow (WebUI > WCF Service > BPL > DAL > DB) 流程中的拟议网站(WebUI> WCF服务> BPL> DAL> DB)

There is a utilities class that houses a DB opener method that returns a SQLCommand object to the BPL. 有一个实用程序类,其中包含一个DB Opener方法,该方法将SQLCommand对象返回到BPL。

Problem: When I try to "test" the service I get a nullreference in the PharmUtil catch statement that I don't get when I run the Windows form. 问题:当我尝试“测试”该服务时,在PharmUtil catch语句中得到的是空引用,而运行Windows窗体时却没有得到。 What am I missing...? 我想念什么...?

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

As you try to TEST the WCF service, it probably is running as webservice and is using a web.config file to try and retrieve the connection string. 当您尝试测试WCF服务时,它可能正在作为Web服务运行,并且正在使用web.config文件来尝试检索连接字符串。 When you run the WinForms app, the WCF service is potentially linked directly in the Winforms project, and in that case it used the app.config from the winforms app to retrieve the connection string. 当您运行WinForms应用程序时,WCF服务可能会直接链接到Winforms项目中,在这种情况下,它将使用winforms应用程序中的app.config来检索连接字符串。

Advise: Check to see if your WCF service project has a web.config file and make sure it has the database connection string configured. 建议:检查WCF服务项目是否具有web.config文件,并确保已配置数据库连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM