【问题标题】:Class Library Access in WCFWCF 中的类库访问
【发布时间】:2011-07-01 02:29:01
【问题描述】:

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

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>

【问题讨论】:

    标签: vb.net wcf


    【解决方案1】:

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

    编辑:

    要向客户提供课程,您可以按照以下方式进行操作:

     '''''''Rest of contract snipped
    
     <OperationContract()>
     Function GetCourses() As WorkMateLib.Courses
    
     '''''''Rest of contract snipped
    

    关键是通过将其引用为服务操作(函数)的输入参数或输出值,在服务中添加您希望向客户端公开的任何类。

    【讨论】:

    • 您能否建议我如何使其成为服务合同的一部分。谢谢。
    • @Sixto Saez,我试过了,但是这样我应该启动一个服务客户端然后获取它,但是在我的登录服务中,我没有启动登录服务来获取类。
    • 在这种情况下,我不确定客户端如何根据显示的服务合同访问 Logins 类。您能否将显示定义为 ILoginsRelated 服务合同一部分的登录的 WSDL XML 部分添加到您的问题文本中?这可能会提供有关如何引用 Logins 的线索。
    • 答案在这个question shows how to configure netTcpBinding 以显示元数据端点(mex)。元数据就是我所指的 WSDL 文档。
    • 行为元素中的设置serviceMetadata httpGetEnabled="false" 设置将阻止您的服务显示WSDL。获取 WSDL 最简单的方法是 create a basicHttpBinding endpoint for displaying it in a browser.
    猜你喜欢
    • 2021-09-26
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多