c# - 在 asp.net web 服务中没有为此对象错误定义无参数构造函数

标签 c# asp.net web-services asmx

我在尝试运行我的 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 网络服务。

最佳答案

你有 private 参数少的构造函数,它在类外是不可访问的。使构造public 以便可以在类之外访问它以构造WizardService 的对象。

public WizardService()
{
    eventLog = new EventLog("EktronWizardServiceLog", "localhost", "EktronWizardService");
}

Access Modifiers (C# Programming Guide)

public 同一程序集中的任何其他代码或引用它的另一个程序集中的任何其他代码都可以访问该类型或成员。

private 类型或成员只能由同一类或结构中的代码访问。

protected 类型或成员只能由同一类或结构中的代码访问,或者在从该类派生的类中访问。

内部 同一程序集中的任何代码都可以访问类型或成员,但不能从另一个程序集中访问。

您可以阅读有关访问修饰符的更多信息 here .

关于c# - 在 asp.net web 服务中没有为此对象错误定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22781307/

相关文章:

c# - CheckBoxList 项之间的线

asp.net - "Invalid token ' , 结账后 ' in class, struct, or interface"

WCF Web 服务中的 C# bool 变量始终为 false/Datetime 变量始终为 DateTime.Min

c# - 使用锁的最佳实践

c# - 如何从 Web 服务获取 XML?

c# - 微软图形 API : Getting error "Authorization_IdentityNotFound"

c# - 用于创建实时应用程序的 ASP.net/C# 书籍

java - 如何使用基于 Java 的客户端使用 .NET(C#) 创建 SOAP Web 服务?

wcf - 服务架构(WCF 和 Delphi)

c# - 在 C# 中将金额转换为卢比和 paise 中的单词格式