在我的项目中,我有一个用于生成列表的WebService,当我运行WebService时,我得到NullReference Exception
中的c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\DefaultWsdlHelpGenerator.aspx
。
有人能指出我代码中的问题是什么吗?
我试过的代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using SubSonic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
public AutoComplete()
{
//InitializeComponent();
}
public string[] Getlist(string keywordstartswith)
{
IList<string> output = new List<string>();
Dictionary<string, string> mydict = new Dictionary<string, string>();
string QueryString = System.Configuration.ConfigurationManager.ConnectionStrings ["IUMSNXG"].ToString();
IDataReader obj_result = SearchApp.DBCon.LRS_SP_CBFM_Sel(keywordstartswith).GetReader();
DataTable dt = new DataTable();
dt.Load(obj_result);
if(dt.Rows.Count > 0)
{
while (obj_result.Read())
{
output.Add(string.Format("{0}~{1}", obj_result["AnimalCode"].ToString().TrimEnd(), obj_result["pk_animalid"].ToString().TrimEnd()));
}
}
return output.ToArray();
}
}
我得到的源错误是:
Line 1333:
Line 1334: OperationBinding FindHttpBinding(string verb) {
Line 1335: foreach (ServiceDescription description in serviceDescriptions) // Getting Error Here
{
Line 1336: foreach (Binding binding in description.Bindings) {
Line 1337: HttpBinding httpBinding = (HttpBinding)binding.Extensions.Find(typeof(HttpBinding));
堆栈跟踪
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.defaultwsdlhelpgenerator_aspx.FindHttpBinding(String verb) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\DefaultWsdlHelpGenerator.aspx:1335
ASP.defaultwsdlhelpgenerator_aspx.get_HttpPostOperationBinding() in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\DefaultWsdlHelpGenerator.aspx:526
ASP.defaultwsdlhelpgenerator_aspx.get_ShowingHttpPost() in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Config\DefaultWsdlHelpGenerator.aspx:541
ASP.defaultwsdlhelpgenerator_aspx.__Render__control20(HtmlTextWriter __w, Control parameterContainer) in c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 \Config\DefaultWsdlHelpGenerator.aspx:1574
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060
答案 0 :(得分:3)
如果为您的网站禁用了AutoEventWireup,则为asmx资源的get请求生成输出的ASP.Net处理程序不起作用。这是不幸的,因为禁用这是性能最佳实践。暂时Enable it in your web.config,并在完成后将其关闭。
(帽子提示为Bryan Allott)
答案 1 :(得分:2)
[WebMethod] 属性。 尝试编写您的网络方法,例如
[WebMethod]
public string[] Getlist(string keywordstartswith)
{
IList<string> output = new List<string>();
Dictionary<string, string> mydict = new Dictionary<string, string>();
string QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["IUMSNXG"].ToString();
IDataReader obj_result = SearchApp.DBCon.LRS_SP_CBFM_Sel(keywordstartswith).GetReader();
DataTable dt = new DataTable();
dt.Load(obj_result);
if (dt.Rows.Count > 0)
{
while (obj_result.Read())
{
output.Add(string.Format("{0}~{1}", obj_result["AnimalCode"].ToString().TrimEnd(), obj_result["pk_animalid"].ToString().TrimEnd()));
}
}
return output.ToArray();
}
将 [WebMethod] 属性附加到Public方法表示您希望将该方法作为XML Web服务的一部分公开
答案 2 :(得分:1)
作为解决方案,您可以添加&lt;%@ Page autoEventWireup =“true”%&gt;到顶部 C:\ Windows \ Microsoft.NET \框架\ v4.0.30319 \配置\ DefaultWsdlHelpGenerator.aspx