简体   繁体   English

向Web Service C#添加构造函数

[英]Adding an Constructor to Web Service C#

Im trying to add an constructor to my webservice, that looks like this: 我正在尝试向我的网络服务中添加一个构造函数,如下所示:

public class ILotusNotesService : System.Web.Services.WebService
{
    List<Calendar> cal = new List<Calendar>();
    [WebMethod]
    public List<Calendar> GetAllCal(string room)
    {
        DateTime d_start = new DateTime(2012,1,3,12,30,0);
        DateTime d_end = new DateTime(2012, 1, 3, 13, 00, 0);
        Calendar c1 = new Calendar("Søren Steffensen", "Mødelokale 1", "Privat", d_start, d_end);
        DateTime d_start2 = new DateTime(2012, 1, 3, 13, 30, 0);
        DateTime d_end2 = new DateTime(2012, 1, 3, 14, 00, 0);
        Calendar c2 = new Calendar("Knud Olesen", "Mødelokale 1", "Privat", d_start2, d_end2);
        DateTime d_start3 = new DateTime(2012, 1, 3, 15, 30, 0);
        DateTime d_end3 = new DateTime(2012, 1, 3, 16, 00, 0);
        Calendar c3 = new Calendar("Morten Nielsen", "Mødelokale 1", "Miljø Politik", d_start3, d_end3);
        cal.Add(c1);
        cal.Add(c2);
        cal.Add(c3);
        return cal;
    }

And the constructor looks like this 构造函数看起来像这样

[DataContract(Name = "Calendar")]
public class Calendar: System.Web.Services.WebService
{
    [DataMember(Name = "_meetingHolder")]
    public String _meetingHolder { get; set; }

    [DataMember(Name = "_meetingRoom")]
    public String _meetingRoom { get; set; }

    [DataMember(Name = "_status")]
    public String _status { get; set; }

    [DataMember(Name = "_startTime")]
    public DateTime _startTime { get; set; }

    [DataMember(Name = "_endTime")]
    public DateTime _endTime { get; set; }


    public Calendar(string meetingHolder, string MeetingRoom,string status, DateTime startTime, DateTime endTime)
    {
        this._meetingHolder = meetingHolder;
        this._meetingRoom = MeetingRoom;
        this._status = status;
        this._startTime = startTime;
        this._endTime = endTime;
    }
}

But i keep getting this error: 但我不断收到此错误:

LotusNotesServiveLibrary.Calendar cannot be serialized because it does not have a default public constructor LotusNotesServiveLibrary.Calendar无法序列化,因为它没有默认的公共构造函数

When writing an object that can be serialized, you must provide a constructor that has no parameters (a default constructor ): 编写可序列化的对象时,必须提供不带参数的构造函数( 默认构造函数 ):

public Calendar()

The reason is that when the objects gets deserialized, the deserializer must have a default way of creating an instance of the object. 原因是当对对象进行反序列化时,反序列化器必须具有创建对象实例的默认方式。

That requirement has to be satisified, and once you add a constructor that takes parameters the compiler will not longer generate the default parameterless constructor for you. 必须满足该要求,并且一旦添加了带参数的构造函数,编译器将不再为您生成默认的无参数构造函数。 You can do this: 你可以这样做:

public Calendar() {
}

But then your member variables won't be initialized. 但是,您的成员变量不会被初始化。 If you have default values you'd like to use for them, you can also do this: 如果您要使用默认值,也可以执行以下操作:

public Calendar():this("meetingHolder", new MeetingRoom(), "status",
                       new DateTime(), new DateTime()) {
}

And you'll have to come up with reasonable default values. 而且,您必须提供合理的默认值。 Here I've just put dummy placeholders. 在这里,我只是放置了虚拟占位符。

It is because you have to have a constructor without any parameter. 这是因为您必须有一个没有任何参数的构造函数。 Create a constructor without a parameter and assign default values to your properties. 创建不带参数的构造函数,并将默认值分配给您的属性。

That is because you simply cannot do it that way. 那是因为您根本无法那样做。 When you publish a web service, the client will send data according to your data contract to you, and the .NET Framework will take that data and populate your data contract class (the Calendar class) using the properties you expose. 发布Web服务时,客户端将根据您的数据合同向您发送数据,.NET Framework将获取该数据并使用您公开的属性填充您的数据合同类Calendar类)

First of all, your data contract should not inherit from the WebService class. 首先,您的数据协定不应从WebService类继承。 Second, just provide the default parameter-less constructor, and it will all work itself out. 其次,只需提供默认的无参数构造函数,它就会自动工作。

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

相关问题 C#调用导航Web服务而不添加Web引用 - C# call nav web service without adding a web reference 在C#中为Web服务调用添加自定义SOAPHeader - Adding custom SOAPHeader in C# for a web service call 将肥皂标头添加到C#中的Web服务引用 - adding a soap header to a web service reference in c# ASP.NET Web服务:添加自定义属性C# - ASP.NET WEB SERVICE: Adding custom attribute C# C#ASP.Net添加在线Web服务 - C# ASP.Net adding online web service 部署 C# 项目并在安装时添加 web 服务参考 - Deployment of C# project and adding web service reference at the time of installation 类构造函数(来自C#Web服务)不会在C#MVC中自动实现属性 - Class constructor (from C# web service) won't auto-implement properties in C# MVC C#Web浏览器构造函数 - C# web browser constructor C# ASP .NET Web API dependency injection passing a property of the current Controller to a service class constructor - C# ASP .NET Web API dependency injection passing a property of the current Controller to a service class constructor C#-尽管使用了没有无参数构造函数的类,Web Service仍然可以工作 - C# - Web Service still working despite using a class that does not have a parameterless constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM