You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.Net WCF如何将自定义类数组作为参数传递至客户端

我来帮你搞定WCF单通道架构下传递自定义类集合的问题——要让服务端的自定义类数组/集合顺利传到客户端,核心是让你的类符合WCF的数据契约规则,同时配置正确的服务契约。下面是具体的步骤和完整代码示例:

1. 给自定义类标记数据契约与数据成员

WCF的序列化机制依赖DataContract和DataMember标记来识别要传递的类型和成员,所以首先要给你的ServerChildClass补上这些标记:

' ServerModule.dll
Imports System.Runtime.Serialization

<DataContract(Namespace:="http://yourdomain.com/wcf/contracts")>
Public Class ServerChildClass
    Private _integer As Integer

    ' 用属性而非公共字段,是WCF推荐的做法
    <DataMember>
    Public Property MyInteger As Integer
        Get
            Return _integer
        End Get
        Set(value As Integer)
            _integer = value
        End Set
    End Property

    ' 必须显式添加无参构造函数,DataContractSerializer依赖它
    Public Sub New()
    End Sub

    ' 可选:带参构造函数,方便初始化数据
    Public Sub New(intValue As Integer)
        _integer = intValue
    End Sub
End Class
2. 在服务契约中定义返回集合的操作

接下来要在你的WCF服务接口(服务契约)里定义返回该类集合的方法,推荐用List(Of T)或者IEnumerable(Of T)来传递集合(数组也支持,但集合后续处理更灵活):

Imports System.ServiceModel

<ServiceContract(Namespace:="http://yourdomain.com/wcf/contracts")>
Public Interface IMyService
    <OperationContract>
    Function GetChildClassCollection() As List(Of ServerChildClass)

    ' 如果坚持用数组,也可以这么写:
    '<OperationContract>
    'Function GetChildClassArray() As ServerChildClass()
End Interface

然后实现这个服务契约:

Public Class MyService
    Implements IMyService

    Public Function GetChildClassCollection() As List(Of ServerChildClass) Implements IMyService.GetChildClassCollection
        ' 模拟生成测试数据
        Dim childList As New List(Of ServerChildClass)()
        childList.Add(New ServerChildClass(100))
        childList.Add(New ServerChildClass(200))
        childList.Add(New ServerChildClass(300))
        Return childList
    End Function
End Class
3. 客户端调用的正确姿势

当你在客户端通过“添加服务引用”生成代理后,就可以直接调用服务方法获取集合了:

' 客户端代码示例
Using serviceClient As New MyServiceClient()
    Dim receivedCollection As List(Of ServerChildClass) = serviceClient.GetChildClassCollection()
    
    ' 遍历处理返回的数据
    For Each item In receivedCollection
        Console.WriteLine($"获取到的整数值:{item.MyInteger}")
    Next
End Using
几个必须注意的坑
  • 无参构造函数不能少:WCF的序列化器必须依赖无参构造函数来实例化对象,哪怕是空实现也要显式写出来。
  • 统一命名空间:服务端和客户端的数据契约、服务契约最好用同一个命名空间,避免序列化时出现类型不匹配的问题。
  • 大数据量调整配置:如果要传递的集合很大,记得修改服务端和客户端的绑定配置,调大maxReceivedMessageSize和maxArrayLength参数,防止触发消息大小限制的错误:
<!-- 服务端app.config/web.config示例 -->
<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="LargeDataBinding" maxReceivedMessageSize="2147483647">
                <readerQuotas maxArrayLength="2147483647" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <services>
        <service name="ServerModule.MyService">
            <endpoint address="" binding="basicHttpBinding" bindingConfiguration="LargeDataBinding" contract="ServerModule.IMyService" />
        </service>
    </services>
</system.serviceModel>

内容的提问来源于stack exchange,提问作者askdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:26:36