2017-09-19 51 views
0

我在服务和客户端之间共享的项目中有Class2和Class1。不从WCF服务返回的继承类

<DataContract> _ 
Public Class Class1 
    <DataMember> _ 
    Public Property Test 
End Class 

<DataContract()> _ 
Public Class Class2(Of ReturnType) 
    <DataMember> _ 
    Public Property Test As String 
    <DataMember()> _ 
    Public Property Items() As ReturnType() 
End Class 

这是我的服务接口:

<ServiceContract(Name:="Service")> _ 
Public Interface IService 

    <OperationContract()> _ 
    Function GetStuff() As Class2(Of Class1) 

End Interface 

我的服务实现:

Public Class Service 
    Implements IService 

    Public Function GetStuff() As Class2(Of Class1) Implements IService.GetStuff 
     Dim results As Class2(Of Class1) = New Class2(Of Class1) 
     Dim oClass1 As New Class1 

     results.Test = "test" 
     oClass1.Test = "test" 
     results.Items = {oClass1} 
     Return results 

    End Function 
End Class 

在我服务的项目我也有从共享类继承的Class1的:

Public Class Class1 
    Inherits [Shared].Entities.Class1 

End Class 

我的客户端代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim oEndpoint As New System.ServiceModel.EndpointAddress(New Uri("net.tcp://localhost/service"), _ 
          System.ServiceModel.EndpointIdentity.CreateUpnIdentity("")) 
    Dim oClient As New ServiceClient("IService", oEndpoint) 

    Dim oList As Class2(Of Class1) = oClient.GetStuff() 
    MessageBox.Show(oList.Test) 
    MessageBox.Show(oList.Items.Length.ToString) 
End Sub 

Public Class ServiceClient 
    Inherits System.ServiceModel.ClientBase(Of IService) 
    Implements IService 

    Public Sub New(ByVal endpointConfigurationName As String, _ 
       ByVal endpoint As System.ServiceModel.EndpointAddress) 
     MyBase.New(endpointConfigurationName, endpoint) 
    End Sub 

    Public Function GetStuff() As Class2(Of Class1) Implements IService.GetStuff 

     Dim oRet As Class2(Of Class1) = MyBase.Channel.GetStuff() 
     Return oRet 
    End Function 
End Class 

运行按钮,点击会产生两个消息框一个字符串“测试”(预期)和一个带有字符串0(我预期1)。只要我从服务项目中排除继承的Class1,它就会按预期工作。

事情是这个项目多年来一直这样工作,它只是当我最近做了一个改变,并重建它,它已停止工作。我怎样才能使这个工作?很显然,在我的真实项目中,服务项目中的Class1包含一些我想使用的附加功能。

回答

0

对我来说,解决的办法是将名称空间添加到派生类DataContract属性中。这已经在基类中出现了。