【发布时间】:2011-10-28 14:54:43
【问题描述】:
今天早上我的 WCF 引用遇到了一个奇怪的问题。简而言之,我设置了一个双工服务,以便服务器可以通过发送数据契约中的对象来通知客户端。当客户端连接时,它会在服务器上运行一个函数以根据服务类的共享队列中的内容返回List(Of NewItem)。问题是,当我在客户端更新我的服务引用时,它说函数返回一个NewItem 对象,而不是List(Of NewItem) 对象。我可以进入参考并手动将其更改为List 对象,它会很好地传输。任何想法为什么服务引用生成器会任意更改我的返回类型?
以下是相关代码:
<ServiceContract(
CallbackContract:=GetType(INotifyCallback),
SessionMode:=ServiceModel.SessionMode.Required)>
Public Interface INotifyService
<OperationContract()>
Function GetNewServerItems() As List(Of NewItem)
End Interface
<DataContract>
<Serializable>
Public Class NewItem
<DataMember()>
Public Property ItemNum As String
<DataMember()>
Public Property Timestamp As DateTime
End Class
<ServiceBehavior(
ConcurrencyMode:=ServiceModel.ConcurrencyMode.Single,
InstanceContextMode:=ServiceModel.InstanceContextMode.Single)>
Public Class NotifyService
Implements INotifyService
Shared _server_items As New List(Of NewItem)
Public Function GetNewServerItems() As List(Of NewItem)
Return _server_items
End Function
End Class
在 Reference.vb 中(简体):
<System.ServiceModel.OperationContractAttribute(Action:="http://tempuri.org/INotifyService/GetNewServerItems", ReplyAction:="http://tempuri.org/INotifyService/GetNewServerItemsResponse")> _
Function GetNewServerItems() As NotifyGateway.NewItem()
【问题讨论】:
-
您是否通过右键单击项目并选择“添加服务引用”来添加引用?
-
你是不是不小心离开了
<ServiceContract>? -
@JohnSaunders - 并非偶然。我把它们排除在外是因为这是一份很长的合同,我只想包括最基本的内容。我已经把它们添加进去了。
-
对您的原始帖子的一个轻微但重要的澄清:该函数返回一个 NewItem 对象数组,而不是单个 NewItem 对象。您可以轻松地在接收端创建一个 List(Of NewItem) 并从数组中加载它,或者如果更改默认集合类型会导致其他副作用,则直接使用数组。