How to pass typedviews back to a client using WCF

Posts   
 
    
Scaramanga
User
Posts: 57
Joined: 24-Mar-2011
# Posted on: 12-Apr-2011 10:59:47   

This isn't a question, I just thought I'd share how to to this if your having the issues I was.

This was tested using VS2010 .NET (VB) Framework 3.5 & 4 & WPF & Winforms, LLBLGenPro 3.1 & XPSP2 onto IIS6 on Server 2003 (And II7 on Win7 pro) & SQL2000 & 2008.

Passing TypedViews back to the client can sometimes cause issues with wdsl generation for the service, the service behind still runs but you might see the error below when you try and verify the service is running in your browser:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.blah.com:IMyServiceService ----> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.XmlTreeGen.FindTargetNamespace(DataTable table).......

This sometimes occurs when you added the strongly TypedViews as known types to your service contract. To get round this do the following:

In Your Service Interface



Imports emCustomer.EntityClasses
Imports emCustomer.TypedViewClasses

Namespace Customer.Service

    <ServiceContract()> _
    <ServiceKnownType(GetType(CustomerEntity))> _
    <ServiceKnownType(GetType(EntityCollection))> _
    Public Interface ICustomerService

        <OperationContract()> _
        Function GetTopSellingItems() As DataTable

In your Service


Imports emCustomer.EntityClasses
Imports emCustomer.TypedViewClasses

Namespace Customer.Service

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.[Single], ConcurrencyMode:=ConcurrencyMode.Multiple)> _
    Public Class CustomerService

    Implements ICustomerService

        Public Function GetTopSellingItems() As DataTable Implements ICustomerService.GetTopSellingItems

            Dim TypedView = New vTopSaleItemsTypedView()
            Dim dt As New DataTable("vTopSaleItemsTypedView")
            Using adapter = New DataAccessAdapter()
                adapter.FetchTypedView(TypedView.GetFieldsInfo(), dt, Nothing, 0, False)
            End Using

            Return dt

        End Function

In your client


Dim dtTopSaleItems As DataTable = _oWCFService.GetTopSellingItems()

A big thanks to the guys at LLBLGen for their excellent technical support in finding this solution for us.

Hopefully if someone else is running into similar issues then this might help.