wcf 引用未正确生成 [英] wcf reference not being generated correctly

查看:20
本文介绍了wcf 引用未正确生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天早上在我的 WCF 引用中遇到了一个奇怪的问题.简而言之,我设置了一个双工服务,以便服务器可以通过发送数据协定中的对象来通知客户端.当客户端连接时,它会在服务器上运行一个函数,根据服务类的共享队列中的内容返回一个 List(Of NewItem).问题是,当我在客户端更新我的服务引用时,它说该函数返回一个 NewItem 对象,而不是一个 List(Of NewItem) 对象.我可以进入引用并手动将其更改为 List 对象,它会很好地传输.为什么服务引用生成器会随意更改我的返回类型?

Ran across a weird problem with my WCF references this morning. In a nutshell, I have a duplex service set up so that a server can notify a client by sending objects in a data contract. When a client connects, it runs a function on the server to return a List(Of NewItem) based on what's in the shared queue of the service class. The trouble is, when I update my service reference in the client, it says the function returns a NewItem object, not a List(Of NewItem) object. I can go into the reference and manually change it to a List object and it'll transfer just fine. Any ideas why the service reference generator would arbitrarily change my return type?

相关代码如下:

<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(简化版)中:

And in Reference.vb (simplified):

<System.ServiceModel.OperationContractAttribute(Action:="http://tempuri.org/INotifyService/GetNewServerItems", ReplyAction:="http://tempuri.org/INotifyService/GetNewServerItemsResponse")>  _
Function GetNewServerItems() As NotifyGateway.NewItem()

推荐答案

你和我落入了同一个陷阱:我们都误读了为代理方法生成的 VB.NET 代码,认为它返回的是单个项目.实际上,它返回的是一个数组.

You and I fell into the same trap: we both misread the VB.NET code generated for the proxy method as returning a single item. In fact, it is returning an array.

WSDL 使用 XML Schema 来描述请求和响应的形状.XML Schema 没有列表"的概念,只有一个序列.默认情况下,当它看到一系列项目时,添加服务引用"会生成一个数组.您可以更改此设置以生成列表",只需在添加服务引用"或配置服务引用"对话框的高级"选项卡上更改集合类型即可.

WSDL uses XML Schema to describe the shape of the request and response. XML Schema has no concept of a "list", only of a sequence. By default, when it sees a sequence of items, "Add Service Reference" generates an array. You can change this to generate a "List", simply by changing the collection type on the "Advanced" tab of the "Add Service Reference" or "Configure Service Reference" dialogs.

这篇关于wcf 引用未正确生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆