WCF中的类库访问

时间:2011-06-30 12:04:28

标签: vb.net wcf

我在类库项目的两个文件中有两个类,它们是:

Public Class Logins
    Public CurrentUser As Login
    Public Function Authenticate(ByVal id As String, ByVal pw As String)
        Dim adpt As New WorkMateDataSetTableAdapters.LoginsTableAdapter
        For Each k As WorkMateDataSet.LoginsRow In adpt.GetDataByUserName(id)
            If String.Equals(k.UserPW, pw) Then
                CurrentUser = New Login(k.UserName, k.UserPW, k.UserType)
                Return CurrentUser
                Exit Function
            End If
        Next
        CurrentUser = Nothing
        Return Nothing
    End Function
End Class

Public Class Login
    Private _UserName As String
    Private _UserPW As String
    Private _UserType As String

    Property UserName
        Get
            Return _UserName
        End Get
        Set(value)
            _UserName = value
        End Set
    End Property
    Property UserPW
        Get
            Return _UserPW
        End Get
        Set(value)
            _UserPW = value
        End Set
    End Property
    Property UserType
        Get
            Return _UserType
        End Get
        Set(value)
            _UserType = value
        End Set
    End Property
    Public Sub New()
        UserName = ""
        UserPW = ""
        UserType = ""
    End Sub
    Public Sub New(ByVal Namee As String, ByVal pw As String, ByVal typee As String)
        UserName = Namee
        UserPW = pw
        UserType = typee
    End Sub

End Class

另一类是:

public Class Courses
    Public CoursesOffered As List(Of Course)
End Class

Public Class Course
    'Cource name, Exams, Papers
    Private _CourseCategory As String
    Private _CourseID As String
    Public _Sems As New List(Of Sem)
End Class

我使用net.tcp连接使用了WCF服务,但问题是我可以直接使用登录原始类(Public Logins As New ServiceLogins.Logins),但我无法使用课程类那样的。

以下是两个WCF服务的代码:

Imports System.ServiceModel
<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    Function Test(ByVal value As Integer) As WorkMateLib.Course

    <OperationContract()>
    Function GetData(ByVal value As Integer) As String

    ' TODO: Add your service operations here

End Interface

and the other file is:


Imports System.ServiceModel

' NOTE: You can use the "Rename" command on the context menu to change the interface name "ILoginsRelated" in both code and config file together.
<ServiceContract()>
Public Interface ILoginsRelated

    <OperationContract()>
    Function Authentication(ByVal login As WorkMateLib.Login) As WorkMateLib.Login

End Interface

感谢您对此问题的帮助。谢谢。

编辑:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WorkMateWCF.WorkMateWCFService1">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8523/WorkMateWCFServiceHost" />
          </baseAddresses>
        </host>
      </service>
      <service name="WorkMateWCF.LoginsRelated">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration=""
          contract="WorkMateWCF.ILoginsRelated">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8732/WCFLoginsHost" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

1 个答案:

答案 0 :(得分:0)

目前还不完全清楚您要实现的目标,但 Course 类只公开一个名为 _sem 的公共属性,WCF将序列化为Sem类型对象的数组。使用服务代码中的课程类型应该没有问题,但由于是服务合同的一部分,因此使用该服务合同的客户无法使用它。

编辑:

为了让客户可以使用课程,您可以采取以下措施:

 '''''''Rest of contract snipped

 <OperationContract()>
 Function GetCourses() As WorkMateLib.Courses

 '''''''Rest of contract snipped

关键是通过将其作为输入参数或服务操作(函数)的输出值引用,将要公开的服务中的任何类添加到服务合同的客户端部分。