WCF - 我可以使用现有类型通过我的 WCF 服务传递吗
我有一个服务。我有一个现有的业务对象类。我想知道的是如何从业务对象程序集中通过 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种简单的方法可以在客户端和服务之间共享类型,只需在添加服务引用之前向客户端添加对共享类型程序集的引用即可。
您可以在那里找到详细的场景和示例项目:
http://blog.walteralmeida.com/2010/08/wcf-tips-and-tricks-share-types- Between-server-and-client.html
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 似乎也是一个自定义类,因此您可以将其定义为其自己的类中的协定的一部分。客户必须了解这一点,因此需要自己的合同。客户端已经知道如何处理字符串等 CLR 类型。任何其他自定义类型也必须在合同中定义。
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.