WCF类'新'子未公开

时间:2013-10-30 18:53:23

标签: .net vb.net wcf

我的WCF服务包含一个类:

<DataContract()>
Public Class MyClass
    <DataMember()>
    Public Property MyProperty As Integer
    <DataMember()>
    Public Property MyOtherProperty As Integer
    Private Property mTotal As Integer
    <DataMember()>
    Public ReadOnly Property Total As Integer
        Get
            Return mTotal
        End Get
    End Property
    Public Sub New(prop1 As Integer, prop2 As Integer)
        mTotal = prop1 + prop2
    End Sub
End Class

当我尝试访问该服务时,我可以创建一个新的'MyClass'对象,但是'New'子没有公开,因此我无法提供参数,并且永远不会填充mTotal。这是一个限制还是我错过了什么?

3 个答案:

答案 0 :(得分:2)

您的参数化构造函数仅在服务器端可用,您无法从客户端调用它。您可以向ServiceContract添加一个函数,该函数调用该构造函数,然后返回结果。我使用VB已经好几年了,所以请原谅我,如果语法不对,但这应该给你正确的想法:

<OperationContract()>
Function CreateNewMyClass(prop1 As Integer, prop2 As Integer) as MyClass

实现看起来像这样:

Function CreateNewMyClass(prop1 As Integer, prop2 As Integer) as MyClass
    Return New MyClass(prop1, prop2) 
End Function

答案 1 :(得分:1)

SOAP Web服务不会公开任何特定于OO或.NET的内容。您不能公开构造函数,索引器,事件或类似的东西。

即使你&#34;暴露&#34;一个enum,你并没有真正暴露enum:只有一个字符串类型,它可以有几个枚举字符串值之一。没有相应的整数。

您也不能公开重载方法,也不能公开泛型。

答案 2 :(得分:0)

通过添加另一个无参数构造函数来更新您的类:

<DataContract()>
Public Class MyClass
    <DataMember()>
    Public Property MyProperty As Integer
    <DataMember()>
    Public Property MyOtherProperty As Integer
    Private Property mTotal As Integer
    <DataMember()>
    Public ReadOnly Property Total As Integer
        Get
            Return mTotal
        End Get
    End Property
    Public Sub New(prop1 As Integer, prop2 As Integer)
        mTotal = prop1 + prop2
    End Sub

   Public Sub New()
     ' default constructor
   End Sub
End Class
如果你没有明确定义一个构造函数,VB会给你一个默认的(无参数)构造函数,但是由于你创建了一个接受prop1prop2的构造函数,所以无参数构造函数会消失,除非你定义一个。

相关问题