WCF, adapter and Linq

Posts   
 
    
Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 21-Mar-2009 22:24:49   

RuntimeBuild="01162009" RuntimeVersion="2.6.0.0"

Is there an example of using Linq in WCF web service with VB? I tried to use ILLBLGenProQuery to produce an EntityCollection to pass through the service, but I'm getting the error:

"The underlying connection was closed: The connection was closed unexpectedly."

And I can't find a complete example in the docs.

Here is one of my query attempts:

Public Function GetStaffHoursByStaffID_Linq(ByVal staff_id As String) As IEntityCollection2 Implements IService1.GetStaffHoursByStaffID_Linq Dim Hours As EntityCollection(Of MyStaffHoursEntity) = Nothing Using adapter As New DataAccessAdapter() Dim metadata As New LinqMetaData(adapter) Dim query = From hr In metadata.StaffHours _ Where hr.StaffId = staff_id _ Select hr Hours = CType(query, ILLBLGenProQuery).Execute(Of EntityCollection(Of MyStaffHoursEntity))() End Using Return Hours End Function

(I tried StaffHoursEntity instead of MyStaffHoursEntity, too) Thanks in advance.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 22-Mar-2009 13:22:00   

please check the full execption, as the INNER exception is likely going to tell you what's wrong. LLBLGen Pro doesn't make the connection so if it drops, something else is wrong. Also, did you specify the EntityCollection(Of MyStaffHoursEntity) type as a ServiceKnownType using the attribute?

Frans Bouma | Lead developer LLBLGen Pro
Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 23-Mar-2009 20:26:06   

Otis wrote:

please check the full execption, as the INNER exception is likely going to tell you what's wrong. LLBLGen Pro doesn't make the connection so if it drops, something else is wrong. Also, did you specify the EntityCollection(Of MyStaffHoursEntity) type as a ServiceKnownType using the attribute?

Hi,

I added ServiceKnownType(GetType(EntityCollection(Of MyStaffHoursEntity))) to my Service Interface. However, after I update the service on the client I get (16) compilation errors in the reference.vb file for the type 'schema' (Type 'schema' is not defined) and suggests I change 'schema' to 'SchemaType'.

Am I missing an Imports statement in the Service Interface?

Thanks

Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 23-Mar-2009 22:52:05   

Also,

In a couple of the xsd files generated by updating the service on the client the following xml is causing a compilation error:

<xs:element ref="xs:schema" />

The tool-tip error message says: "The 'http://www.w3.org/2001/XmlSchema:schema' element is not declared."

Thanks again

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 24-Mar-2009 09:57:39   

Please code against the service via an interface. you write an interface, which is shared between server and client (a better way to keep things in sync than a proxy stub).

Please check this Example

Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 24-Mar-2009 13:58:10   

Hello Walaa,

The examples you site are for using the Predicate methods, not Linq. I am able to get things to work using Predicates following your excellent documentation for doing so. However, that's not the problem.

The problem is using Linq. There is no documentation (that I can find) to follow that uses Linq and WCF services together.
disappointed

My Service interface has: <OperationContract()> _ Function GetStaffHoursByStaffID_Linq(ByVal staff_id As String) As IEntityCollection2

The function that calls the service:

Private Sub GetHoursWithWCF_andLinq(ByVal staff_id As String) Dim Hours As EntityCollection(Of MyStaffHoursEntity) = Nothing Using proxy As New WCFStaffHoursAdapter.Service1Client Hours = CType(proxy.GetStaffHoursByStaffID_Linq(staff_id), EntityCollection(Of MyStaffHoursEntity)) End Using Dim hour As MyStaffHoursEntity If Hours IsNot Nothing Then For Each hr In Hours hour = CType(hr, MyStaffHoursEntity)

            Console.WriteLine("{0} {2}", hour.StaffId, hour.LocationCode)
        Next
    Else
        Console.WriteLine("Hours is Nothing")
    End If
End Sub

I think the issue is in passing EntityCollection(of MyStaffHoursEntity) instead of just EntityCollection (using the IEntityCollection2 for the Interface). Am I using using the correct interface for EntityCollection(of T)?

Or, perhaps I do not understand your message.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 24-Mar-2009 14:09:30   

Let me quote myself wink

Otis wrote:

Also, did you specify the EntityCollection(Of MyStaffHoursEntity) type as a ServiceKnownType using the attribute?

Thus, on the service interface definition? That's the way to get a generic collection across. Otherwise WCF can't deal with the generic as it has to use 1 specific type per type crossing the wire, it can't use generic types for that.

Frans Bouma | Lead developer LLBLGen Pro
Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 24-Mar-2009 17:41:03   

Hi Otis,

I'm not trying to be difficult. I love this product. But, I did do what you asked in regard to adding EntityCollection(of MyStaffHoursEntity) as a ServiceKnownType. But, as I wrote above, after I update the service in the client I get design-time compilation errors in the generated reference.vb file and in Service12.xsd file and Service13.xsd file.

Here is a typical place where there occurs an error in the reference.vb file (only when having EntityCollection(of MyStaffHoursEntity) as a ServiceKnownType):

<System.Xml.Serialization.XmlElementAttribute([Namespace]:="http://www.w3.org/2001/XMLSchema", Order:=0)> _ Public Property schema() As schema Get Return Me.schemaField End Get Set Me.schemaField = value Me.RaisePropertyChanged("schema") End Set End Property

The bolded term above gets the error and the tooltip Type 'schema' is not defined.

Here is a copy of the Service12.xsd file that gets generated.

<?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:tns="http://schemas.datacontract.org/2004/07/StaffHoursAdapter.HelperClasses" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/StaffHoursAdapter.HelperClasses" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="EntityCollectionOfMyStaffHoursEntityxKGLtsp9"> <xs:sequence> <**xs:element ref="xs:schema" /> <xs:any /> </xs:sequence> </xs:complexType> <xs:element name="EntityCollectionOfMyStaffHoursEntityxKGLtsp9" nillable="true" type="tns:EntityCollectionOfMyStaffHoursEntityxKGLtsp9" /> <xs:complexType name="EntityCollection"> <xs:sequence> <xs:element **ref="xs:schema" /> <xs:any /> </xs:sequence> </xs:complexType> <xs:element name="EntityCollection" nillable="true" type="tns:EntityCollection" /> </xs:schema>

The design-time compiler doesn't like where I've bolded it. The tooltip for the error at **xs:element **states: The 'http://www.w3.org/2001/XMLSchema:schema' element is not allowed (I don't know where the "xKGLtsp9" part of the element name comes from, but it's there.)

Here is the entire service interface. Perhaps you can see something else that may be wrong (The first 3 function don't use Linq, and worked okay before I added EntityCollection(of myStaffHoursEntity) as a ServiceKnownType):

Imports System Imports System.Xml Imports SD.LLBLGen.Pro.ORMSupportClasses Imports StaffHoursAdapter.EntityClasses Imports StaffHoursAdapter.HelperClasses Imports StaffHoursAdapter Imports System.ServiceModel

<ServiceKnownType(GetType(StaffHoursEntity)), _ ServiceKnownType(GetType(LocationsEntity)), _ ServiceKnownType(GetType(StaffEntity)), _ ServiceKnownType(GetType(EntityCollection)), _ ServiceKnownType(GetType(MyStaffHoursEntity)), _ ServiceKnownType(GetType(EntityCollection(Of MyStaffHoursEntity))), _ ServiceContract()> _ Public Interface IService1

<OperationContract()> _
Function GetStaff() As IEntityCollection2

<OperationContract()> _
Function GetStaffHoursByStaffID(ByVal staff_id As String) As IEntityCollection2

<OperationContract()> _
Function GetStaffIDUserGroup(ByVal user_id As String, ByVal pwd As String, ByRef staff_id As String, ByRef user_group As Integer) As Integer

<OperationContract()> _
Function GetStaffHoursByStaffID_Linq(ByVal staff_id As String) As IEntityCollection2

End Interface

Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 25-Mar-2009 02:25:49   

Sorry for not using the code blocks. I'm new here.

I just realized I could put my code here, like this!:)
Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 25-Mar-2009 09:29:03   

after I update the service in the client I get design-time compilation errors in the generated reference.vb file and in Service12.xsd file and Service13.xsd file

Reading the above lines confuses me, as far as I know you don't need to do that while coding against the interface, as all you should do is share the interface bewtenn the client and the server. (update the interface in the client if it changes at the service side).

You should't be using the Proxy/tub way .... creating a reference.vb class

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 25-Mar-2009 10:49:44   

I indeed think that what Walaa says is the cause: I think you've added a webservice reference to the service, while that's unnecessary. Please see the WCF example we have on our website: I know it's in C#, but you can see that the client doesn't have a reference to the host. It defines the service to connect to in its config file and for the rest the client programs against the interface.

Frans Bouma | Lead developer LLBLGen Pro
Burt52
User
Posts: 10
Joined: 01-Mar-2009
# Posted on: 25-Mar-2009 15:14:23   

Otis and Walaa

Thank you. I guess I was locked into only one way of looking at WCF (i.e. proxy/stub). Thank you for your patience with me.