WCF - 我可以使用现有类型通过我的WCF服务传递

时间:2010-07-20 16:42:07

标签: asp.net wcf datacontractserializer datacontract

我有服务。我有一个现有的业务对象类。我想知道的是如何通过WCF从业务对象程序集传递类而不必在附加或标记时在我的WCF站点中创建新类?

这是一个现有的UDT: 命名空间示例: Application.BusinessObjects.Appointments

Public Structure AppointmentResource
    Private _id As String
    Private _type As ResourceTypeOption
    Private _name As String

    Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

    Property type() As ResourceTypeOption
        Get
            Return CType(_type, Int32)
        End Get
        Set(ByVal value As ResourceTypeOption)
            _type = value
        End Set
    End Property

    Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
        _id = id
        _type = type
        _name = name
    End Sub
End Structure

这是我使用数据协定属性创建的相同内容: 命名空间示例: Application.Service.Appointments

<DataContract()> _
    Public Structure AppointmentResource
        Private _id As String
        Private _type As ResourceTypeOption
        Private _name As String

        <DataMember()> _
        Property id() As String
            Get
                Return _id
            End Get
            Set(ByVal value As String)
                _id = value
            End Set
        End Property

        <DataMember()> _
        Property type() As ResourceTypeOption
            Get
                Return CType(_type, Int32)
            End Get
            Set(ByVal value As ResourceTypeOption)
                _type = value
            End Set
        End Property

        <DataMember()> _
        Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Public Sub New(ByVal id As String, ByVal type As ResourceTypeOption, ByVal name As String)
            _id = id
            _type = type
            _name = name
        End Sub
    End Structure

2 个答案:

答案 0 :(得分:1)

有一种简单的方法可以在客户端和服务之间共享类型,只需在添加服务引用之前向客户端添加对共享类型程序集的引用。

您可以在那里找到详细的方案和示例项目:

http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types-between-server-and-client.html

答案 1 :(得分:0)

ResourceTypeOption似乎也是一个自定义类,因此您可以在其自己的类中将其定义为合同的一部分。客户必须知道这一点,因此需要自己的合同。客户端已经知道如何处理像字符串这样的CLR类型。任何其他自定义类型也必须在合同中定义。