简体   繁体   English

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

[英]No parameterless constructor defined for this object error in asp.net web service

I get this error when trying to run my web service, WizardService.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()

Here is my web service code in C#这是我在 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");
    }

Everywhere I have looked online (including this site) seems to indicate this error message has something to do with MVC, but I am not using MVC.我在网上看到的任何地方(包括这个网站)似乎都表明这个错误信息与 MVC 有关系,但我没有使用 MVC。 This is an ASMX .Net 4.5 web service.这是一个 ASMX .Net 4.5 网络服务。

You have private parameter less constructor which is not accessible outside the class.您有private参数构造函数,在类外无法访问。 Make the construct public so that it could be accessed outside the class for constructing objects of WizardService .将构造public以便可以在类之外访问它以构造WizardService对象。

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

Access Modifiers (C# Programming Guide) 访问修饰符(C# 编程指南)

public The type or member can be accessed by any other code in the same assembly or another assembly that references it. public类型或成员可以被同一程序集或引用它的另一个程序集中的任何其他代码访问。

private The type or member can be accessed only by code in the same class or struct. private类型或成员只能被同一类或结构中的代码访问。

protected The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class. protected类型或成员只能由同一类或结构中的代码或从该类派生的类中的代码访问。

internal The type or member can be accessed by any code in the same assembly, but not from another assembly. internal类型或成员可以被同一程序集中的任何代码访问,但不能从另一个程序集中访问。

You can read more about access modifiers here .您可以在此处阅读有关访问修饰符的更多信息。

Problem : You have declared your Constructor with private access modifier.问题:您已经使用private访问修饰符声明了您的构造函数。 so it is not accessible outside the class to construct the objects.因此无法在类外部访问以构造对象。

Solution : You should have public access modifier for the constructors tobe accessed from outside the class to construct the Objects.解决方案:您应该为要从类外部访问的构造函数使用public访问修饰符来构造对象。

Replace This:替换这个:

private WizardService()

With This:有了这个:

public WizardService()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET MVC中没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object in ASP.NET MVC ASP.NET MVC 4 + Ninject MVC 3 = 没有为此对象定义无参数构造函数 - ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object Unity和ASP.NET WebForms - 没有为此对象定义的无参数构造函数 - Unity and ASP.NET WebForms - No parameterless constructor defined for this object ASP.NET MVC:没有为此定义无参数构造函数 object - ASP.NET MVC: No parameterless constructor defined for this object 没有为此对象定义无参数构造函数ASP.NET网站 - No parameterless constructor defined for this object ASP.NET Website ASP.NET Core 标识用户管理器<ApplicationUser> -Error 没有为此对象定义无参数构造函数 - ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object 从AJAX调用Web服务时,没有为此对象定义无参数构造函数 - no parameterless constructor defined for this object when call web service from AJAX 错误:未为此对象定义无参数构造函数 - Error: No parameterless constructor defined for this object 访问Web服务时未定义无参数构造函数 - No parameterless constructor defined when accessing web service 在ASP.Net Web服务的构造函数中使用带有参数的自定义对象 - Using custom object with parameters in the constructor in ASP.Net web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM