尝试运行我的Web服务WizardService.asmx时出现此错误:
System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Web.Services.Protocols.ServerProtocol.CreateServerInstance()
at System.Web.Services.Protocols.WebServiceHandler.Invoke()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
这是我在C#中的Web服务代码
[WebService(Namespace = "http://www.tempuri.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WizardService : WebService
{
private EventLog eventLog;
private WizardService()
{
eventLog = new EventLog("EktronWizardServiceLog", "localhost", "EktronWizardService");
}
我在网上看到的所有地方(包括这个网站)似乎都表明这个错误信息与MVC有关,但我没有使用MVC。这是一个ASMX .Net 4.5 Web服务。
答案 0 :(得分:5)
你有private
参数较少的构造函数,这是在类外无法访问的。创建构造public
,以便可以在类外部访问它以构造WizardService
的对象。
public WizardService()
{
eventLog = new EventLog("EktronWizardServiceLog", "localhost", "EktronWizardService");
}
<强> Access Modifiers (C# Programming Guide) 强>
public 类型或成员可以由同一程序集中的任何其他代码或引用它的其他程序集访问。
private 只能通过同一个类或结构中的代码访问类型或成员。
protected 只能通过同一个类或结构中的代码或从该类派生的类中访问类型或成员。
内部类型或成员可以由同一程序集中的任何代码访问,但不能从其他程序集访问。
您可以阅读有关访问修饰符here的更多信息。
答案 1 :(得分:2)
问题:您已使用private
访问修饰符声明了构造函数。所以在类之外无法访问它来构造对象。
解决方案:您应该拥有public
访问修饰符,以便从类外部访问构造函数以构造对象。
替换它:
private WizardService()
有了这个:
public WizardService()