Class Library Access in WCF
I have two classes in two files in a class library project, they are:
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
the other class is:
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
I used a WCF service using net.tcp connection, however, the issue is that I am able to use the login original classes directly like ( Public Logins As New ServiceLogins.Logins), but I am not able to the course class like that.
Following is the code for the two WCF services:
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" comm开发者_运维知识库and 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
Your help in this issue is appreciated. Thank you.
Edit:
<?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>
It's not entirely clear what you are trying to achieve but the Course class only exposes one public property named _sem that WCF will serialize as an array of Sem type objects. You should have no problems using the Courses type in the service code but since it is not part of the service contract, it is not available to clients consuming that service contract.
EDIT:
To make Courses available to the clients, you could do something along the lines of this:
'''''''Rest of contract snipped
<OperationContract()>
Function GetCourses() As WorkMateLib.Courses
'''''''Rest of contract snipped
The key is to add any class in the service you want exposed to clients part of the service contract by referencing it as either an input parameter or an output value of a service operation (function).
精彩评论