Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 测试WCF服务时获取NullRef_C#_.net_Winforms_Web Services_Wcf - Fatal编程技术网

C# 测试WCF服务时获取NullRef

C# 测试WCF服务时获取NullRef,c#,.net,winforms,web-services,wcf,C#,.net,Winforms,Web Services,Wcf,情况(Windows窗体): 我已经创建了一个应用程序,比如说一个药房应用程序,可以添加患者、药物、医生。。。这是一个Winforms应用程序。我可以添加没有问题的患者。。。我有一个保存连接字符串的配置文件。我可以毫无问题地访问数据库。在页面加载时,Winform填充4个组合框(2个字母状态、药物、医生、保险公司)。这是一个N层应用程序(UI、EntityObject(EO)、业务流程层(BPL)、数据访问层(DAL)、数据库(DB))。Winform流(WindowUI>BPL>DAL>DB)

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

情境(网络表单、服务) 我现在需要远程访问该应用程序(web表单、Internet)…我已使用与Windows项目相同的解决方案创建了一个WCF服务项目。我未对winform应用程序的流进行任何更改。该服务直接跟踪BPL。建议的web from流(WebUI>WCF service>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 ; 
>      }
“从服务调用BPL:”
名称空间制药
公共数据集StateDS()
{
return StateBPL.StateFillDS();//将状态数据集返回给服务使用者
}
“调用util类和DAL”
名称空间制药
公共类StateBPL
{
公共静态数据集StateFillDS()
{
var cmdobj=new SQLCommand();
cndobj=phamapputil.OpenDB();//调用utilities类
cmdobj.commandText=“spGetStates”;//添加存储过程名称
return StateDAL.StateDSFill(cmdobj);//调用数据访问类并
//返回一个数据集
}
}
>“实用程序调用将命令对象返回给BPL减去
>“存储过程名称”
>实用类:
名称空间制药
>公共类phamapputil
>     {
>SqlConnection conn=新的SqlConnection();
>公共静态SqlCommand OpenDB()
>         {
>SqlConnection conn=新的SqlConnection();
>试一试
>             { 
>var connSettings=ConfigurationManager.connectionString;
>如果(connSettings.Count<1)
>                {
>WriteLine(“未找到连接字符串”);
>返回null;
>                }
>否则
>                {
>连接字符串=
>ConfigurationManager.ConnectionString[“PharmaConn”].ToString();
>conn.Open();
>SqlCommand cmd=新的SqlCommand();
>cmd.Connection=conn;
>cmd.CommandType=CommandType.storedProcess;
>返回cmd;
>                }
>             }
>catch(SqlException-ex)
>             {
>Debug.WriteLine(例如消息);
>返回null;
>             } 
>捕获(NullReferenceException ex)
>             {
>Debug.WriteLine(例如消息);
>返回null;
>             }
>最后
>             {
> 
>             }
>//返回;
>      }

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


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

当它在pharmutil类中命中时,它将下降到nullref捕获:conn.ConnectionString=>ConfigurationManager.ConnectionString[“PharmaConn”]。ToString();但是,当Windows窗体运行时……没有问题……仅来自服务调用……仅供参考,我正在使用WCF测试客户端进行测试……这是可行的……但是,为什么服务调用会从app.config获取计数(web.conig中没有connectionstring部分)然后尝试从web.config获取字符串?