简体   繁体   中英

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. 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). This is an N-Tier application (UI, EntityObject (EO), Business Process Layer (BPL), Data Access Layer(DAL), Database (DB)). The Winform flow (WindowUI > BPL > DAL > DB) (This is 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. I've made no changes to the winform application's flow. The service goes after the BPL directly. Proposed web from flow (WebUI > WCF Service > BPL > DAL > DB)

There is a utilities class that houses a DB opener method that returns a SQLCommand object to the 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. 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. 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.

Advise: Check to see if your WCF service project has a web.config file and make sure it has the database connection string configured.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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