简体   繁体   English

在ASP.Net Web服务的构造函数中使用带有参数的自定义对象

[英]Using custom object with parameters in the constructor in ASP.Net web service

I have a custom type in my asp.net web service. 我在asp.net Web服务中有一个自定义类型。 I have parameterized constructor for the same type. 我有相同类型的构造函数。 But when I try to use the same type on client side I can only see the default constructor with no parameter. 但是当我尝试在客户端使用相同的类型时,我只能看到没有参数的默认构造函数。 This is my type. 这是我的类型。

public class Task
{

    public string AssignedTo { get; set; }

    public int CallDuration { get; set; }

    public Task(string assignedTo,int callDuration)
    {
        AssignedTo = assignedTo;
        CallDuration = callDuration;
    }
}

I don't believe that SvcUtil (what VS uses when adding a service reference) will add it with the parameterized constructor. 我不相信SvcUtil(VS在添加服务引用时使用的东西)将其与参数化构造函数一起添加。

What you can do instead, is put the class into a shared library that's added as a normal reference to both the server and client. 您可以做的是,将类放入一个共享库,该添加库作为对服务器和客户端的常规引用添加。 When adding the service reference, reuse the types, and you can use your constructor because proxy types will not be created for reused types. 添加服务引用时,请重用类型,并且可以使用构造函数,因为不会为重用类型创建代理类型。

This is also useful if your types have helper methods you would like access to on the client. 如果您的类型具有您希望在客户端上访问的辅助方法,这也很有用。

Should work fine if you update the service reference on the client side. 如果您在客户端上更新服务引用,则应该可以正常工作。 Not sure in a traditional web service, but in WCF the service needs to be running to do this. 在传统的Web服务中不确定,但是在WCF中需要运行该服务才能做到这一点。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM