开发者

WCF - Can I use an existing type to be passed through my WCF service

开发者 https://www.devze.com 2023-01-07 22:04 出处:网络
I have a service.I have开发者_高级运维 an existing class of business objects.What I would like to know is how can I pass a class through WCF from the business object assembly without having to create

I have a service. I have开发者_高级运维 an existing class of business objects. What I would like to know is how can I pass a class through WCF from the business object assembly without having to create a new class in my WCF site while appending or tags?

Here is an existing UDT: Namespace example: 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

Here is the same one I created with the data contract attributes: Namespace example: 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


There is an easy way to share types between client and service, just by adding reference to shared type assembly to your client BEFORE adding the service reference.

You can find the detailed scenario and sample project there:

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


ResourceTypeOption also appears to be a custom class, so you would to define that as part of the contract in its own class. The client has to know about that and so it needs its own contract. Clients already know how to deal with CLR types like string. Any other custom types would also have to be defined in the contract.

0

精彩评论

暂无评论...
验证码 换一张
取 消