webservice System.Web.Services.Protocols.SoapException: 服务器无法处理请求。
webservice报错如下:
System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.TypeInitializationException: “CarTestWebservice.DAL.DBHelper”的类型初始值设定项引发异常。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
在 CarTestWebservice.DAL.DBHelper..cctor()
--- 内部异常堆栈跟踪的结尾 ---
在 CarTestWebservice.DAL.DBHelper.ExecuteInsertStoreProcedure(SqlParameter[] param, String storeName)
在 CarTestWebservice.DAL.TestRecordDAL.InsertOneTestRecord(String writeXmlDoc)
在 CarTestWebservice.Web.Service.write(String xmlDoc)
--- 内部异常堆栈跟踪的结尾 ---
service代码:
[WebMethod]
public string writeObjectOut(string xtlb, string jkxlh, string jkid, string WriteXmlDoc)
{
string resultXmlDoc = null;
if(xtlb == "01"&&jkid == "01C71")
{
TestRecordDAL record = new TestRecordDAL();
resultXmlDoc = record.InsertOneTestRecord(WriteXmlDoc);//这是我现在主要测试的代码,其他if中的没有用。
}
else if (xtlb == "01" && jkid == "01C72")
{
TestResult result = new TestResult();
resultXmlDoc = result.InsertOneTestResult(WriteXmlDoc);
}
else if (xtlb == "01" && jkid == "01C75")
{
JtsdzrbxInfoDAL bxinfo = new JtsdzrbxInfoDAL();
resultXmlDoc = bxinfo.InsertOnetJDCJTSGZRQZBXInfo(WriteXmlDoc);
}
else if (xtlb == "01" && jkid == "01C01")
{
CarRegisterDAL register = new CarRegisterDAL();
resultXmlDoc = register.InsertOneCarRegister(WriteXmlDoc); ;
}
return resultXmlDoc;
}
tResultRecordDAL类中的方法:
/// <summary>
/// 向tTestRecord表中插入一条记录
/// </summary>
/// <param name="writeXmlDoc">写入类xmldoc文本</param>
/// <returns>一个xml格式的字符串</returns>
public string InsertOneTestRecord(string writeXmlDoc)
{
string resultXmlDoc = null;
DataSet ds = XmlOperation.XmlDocToDataSet(writeXmlDoc);//将传来的一个xml格式的字符串转为dataset,传来的这个xml格式字符串只有一条记录
SqlParameter[] parameters = new SqlParameter[]
{
new SqlParameter("@JYLSH",SqlDbType.NVarChar,17),
new SqlParameter("@JCXDH",SqlDbType.NVarChar,11),
new SqlParameter("@JDCXH",SqlDbType.NVarChar,14),
new SqlParameter("@HPHM",SqlDbType.NVarChar,15),
new SqlParameter("@HPZL",SqlDbType.NVarChar,2),
//
new SqlParameter("@CLSBDH",SqlDbType.NVarChar,25),
new SqlParameter("@JYRQ",SqlDbType.DateTime),
new SqlParameter("@JYYXQZ",SqlDbType.DateTime),
new SqlParameter("@CJDW",SqlDbType.NVarChar,64),
new SqlParameter("@JBR",SqlDbType.NVarChar,30),
//
new SqlParameter("@JYZHJG",SqlDbType.NVarChar,1),
new SqlParameter("@WJPD",SqlDbType.NVarChar,1),
new SqlParameter("@DPDTPD",SqlDbType.NVarChar,1),
new SqlParameter("@CSBPD",SqlDbType.NVarChar,1),
new SqlParameter("@QYCWQPD",SqlDbType.NVarChar,1),
//
new SqlParameter("@CYCYDPD",SqlDbType.NVarChar,1),
new SqlParameter("@ZDPD",SqlDbType.NVarChar,1),
new SqlParameter("@DGFGQDPD",SqlDbType.NVarChar,1),
new SqlParameter("@DGPYPD",SqlDbType.NVarChar,1),
new SqlParameter("@GLPD",SqlDbType.NVarChar,1),
//
new SqlParameter("@CHPD",SqlDbType.NVarChar,1),
new SqlParameter("@DPPD",SqlDbType.NVarChar,1),
new SqlParameter("@LSPD",SqlDbType.NVarChar,1),
new SqlParameter("@ZCZBZLPD",SqlDbType.NVarChar,1),
};
//给参数数组赋值,通过循环dataset给参数数组一次赋值
for (int i = 0; i < ds.Tables[0].Columns.Count;i++ )
{
parameters[i].Value = ds.Tables[0].Rows[0][i];
}
int resultInt = DBHelper.ExecuteInsertStoreProcedure(parameters,"usp_InsertOnetTestRecord");//调用像表中添加一条记录的存储过程
resultXmlDoc = XmlOperation.getResultXmlDoc(resultInt);//通过resultInt来判断返回xml格式的字符串
return resultXmlDoc;
}
DBHelper类:
public class DBHelper
{
public DBHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
private static string conStr = ConfigurationManager.ConnectionStrings["IVS_SERVER"].ConnectionString;
public static SqlConnection getConnection()
{
return new SqlConnection(conStr);
}
public static int ExecuteInsertStoreProcedure(SqlParameter[] param,string storeName)
{
int result = 0;
using (SqlConnection con = getConnection())
{
con.Open();
SqlCommand cmd = new SqlCommand(storeName,con);
cmd.CommandType = CommandType.StoredProcedure;
foreach(SqlParameter parm in param)
{
cmd.Parameters.Add(parm);
}
result=cmd.ExecuteNonQuery();
con.Close();
}
return result;
}
}
我把它拿出来不用再WEBService,在控制台程序中测试没有出现这个问题,正常执行。为什么用在webservice中就会出这样的错误,请大哥帮我解决下,谢谢!
[最优解释]
“CarTestWebservice.DAL.DBHelper”的类型初始值设定项引发异常.错误很明显是一个初始化的值没赋(比如 private static string conStr = ConfigurationManager.ConnectionStrings["IVS_SERVER"].ConnectionString中是不是在webconfig中没有IVS_SERVER的值),单步调试一下,很快就能找到
[其他解释]
请遇到过一个错误的,帮忙解决下,谢谢拉,知道的帮忙解决下,不知道的就帮我顶帖吧,首先谢谢大家啦!
[其他解释]