从WCF VB.NET返回Object()

时间:2011-08-17 20:00:46

标签: .net vb.net wcf

我有一个具有此功能的WCF服务:

 Public Function ObtenerGruposYAnalisisYConclusionesPredefinidas() As Object() Implements ISrvConclusionesNotas.ObtenerGruposYAnalisisYConclusionesPredefinidas
    Dim conclus As New ConclusionesNotasDL
    Return conclus.ObtenerGruposYAnalisisYConclusionesPredefinidas()
End Function

返回此处指定的值:

Public Function ObtenerGruposYAnalisisYConclusionesPredefinidas() As Object()
    Dim objeto(2) As Object
    objeto(0) = ObtenerGruposAnalisis()
    objeto(1) = ObtenerAnalisis()
    objeto(2) = ObtenerConclusionesPredefinidas()
    Return objeto
End Function

这些函数中的每一个都返回一个实体列表 如果我单独调用此函数,则返回其值。但我想要的只是拨打一个电话,而不是几个! 我希望能够在另一个应用程序中收到它:

Dim concluNotas As SrvConclusionesYNotas.SrvConclusionesNotasClient = New SrvConclusionesYNotas.SrvConclusionesNotasClient
    Dim objeto() As Object = concluNotas.ObtenerGruposYAnalisisYConclusionesPredefinidas()

我该怎么办?

我必须以某种方式序列化吗?该过程在服务中运行良好,但在另一个应用程序中没有接收到该对象,我有一个例外,它表明http不支持这种情况(发送一个对象数组)。

1 个答案:

答案 0 :(得分:2)

WCF仅序列化(和反序列化)它“知道”的对象类型。如果您的操作返回Object(),那么此时的WCF只知道Object()本身和Object。您需要使用ServiceKnownType属性向WCF指示哪些实际对象可以序列化(和反序列化),如下例所示。

Public Class StackOverflow_7098691
    Public Class GruposAnalisis
        Public Text As String
    End Class
    Public Class Analisis
        Public Text As String
    End Class
    Public Class ConclusionesPredefinidas
        Public Text As String
    End Class

    <ServiceContract()>
    Public Interface ITest
        <OperationContract()>
        <ServiceKnownType(GetType(GruposAnalisis))>
        <ServiceKnownType(GetType(Analisis))>
        <ServiceKnownType(GetType(ConclusionesPredefinidas))>
        Function ObtenerGruposYAnalisisYConclusionesPredefinidas() As Object()
    End Interface

    Public Class Service
        Implements ITest

        Public Function ObtenerGruposYAnalisisYConclusionesPredefinidas() As Object() Implements ITest.ObtenerGruposYAnalisisYConclusionesPredefinidas
            Dim objeto(2) As Object
            objeto(0) = ObtenerGruposAnalisis()
            objeto(1) = ObtenerAnalisis()
            objeto(2) = ObtenerConclusionesPredefinidas()
            Return objeto
        End Function

        Function ObtenerGruposAnalisis() As GruposAnalisis
            Dim result As GruposAnalisis = New GruposAnalisis()
            result.Text = "GruposAnalisis"
            Return result
        End Function

        Function ObtenerAnalisis() As Analisis
            Dim result As Analisis = New Analisis()
            result.Text = "Analisis"
            Return result
        End Function

        Function ObtenerConclusionesPredefinidas() As ConclusionesPredefinidas
            Dim result As ConclusionesPredefinidas = New ConclusionesPredefinidas()
            result.Text = "ConclusionesPredefinidas"
            Return result
        End Function
    End Class

    Public Shared Sub Test()
        Dim baseAddress As String = "http://" + Environment.MachineName + ":8000/Service"
        Dim host As ServiceHost = New ServiceHost(GetType(Service), New Uri(baseAddress))
        host.AddServiceEndpoint(GetType(ITest), New BasicHttpBinding(), "")
        host.Open()
        Console.WriteLine("Host opened")

        Dim factory As ChannelFactory(Of ITest) = New ChannelFactory(Of ITest)(New BasicHttpBinding(), New EndpointAddress(baseAddress))
        Dim proxy As ITest = factory.CreateChannel()

        Dim result = proxy.ObtenerGruposYAnalisisYConclusionesPredefinidas()
        For Each grupo In result
            Console.WriteLine(grupo)
        Next
    End Sub
End Class