VS2013 RIA Service problem RRS feed

  • Question

  • I am trying to use a very small, basic RIA service inside the Lightswitch server project using ServerApplicationContext.  The RIA service appears correct and compiles fine. When I try to add it as a datasource, and I add a reference to Application.Server, I get an error:

    The code for the RIA domain service is below.  This is in a project that was upgraded from 2012.  I can get this same concept to work in c# in a new clean 2013 project, but can't get it to work in this vb project.  I have tried several variations of the GetServerContext() function and the code that calls it, including making it public, private, shared etc. and nothing works.  Same error each time.  Please help!

    Imports LightSwitchApplication
    Imports System.ComponentModel.DataAnnotations
    Imports System.ServiceModel.DomainServices.Server
    Namespace LightSwitchApplication.RIA
        Public Class RIAServiceBase
            Inherits DomainService
            Private Shared Function GetServerContext() As ServerApplicationContext
                Dim serverContext As ServerApplicationContext = DirectCast(ServerApplicationContext.Current, LightSwitchApplication.ServerApplicationContext)
                If serverContext Is Nothing Then
                    serverContext = DirectCast(ServerApplicationContext.CreateContext(), LightSwitchApplication.ServerApplicationContext)
                End If
                Return serverContext
            End Function
            Protected ReadOnly Property Context As ServerApplicationContext
                    Return GetServerContext()
                End Get
            End Property
            Protected Overrides Function Count(Of T)(query As IQueryable(Of T)) As Integer
                Return query.Count()
            End Function
            <Query(IsDefault:=True)> _
            Public Function GetRIAItems() As IQueryable(Of RIAItem)
                Return From s In Context.DataWorkspace.Data.Items
                       Select New RIAItem With {
                           .Id = s.ItemID,
                           .ItemNumber = s.ItemNumber
            End Function
        End Class
        Public Class RIAItem
            <Key(), Editable(False)> _
            Public Property Id As Integer
            Public Property ItemNumber As String
        End Class
    End Namespace

    Friday, May 23, 2014 3:09 PM

All replies

  • The bit that worries me is that you seem to get this exception as soon as you add the datasource. From your other experimentations we know that the concept works on a brand new project done in C#.

    Although not the answer to your question, you should dispose the context if you call .CreateContext() so you may want to revamp that aspect of the code. That is not the source of your problem, but should be resolved anyway.

    Some suggestions:

    1. What happens if you do a new sample project to test this using VB rather than C# - does it also work?

    2. How about commenting out all references to the Context in your code above and returning a dummy array of RIAItem entities in the GetRIAItems() query method to eliminate the whole Context issue. If that works then it means you should zoom in on the Context issue.

    Regards, Xander. My Blog

    • Edited by novascape Friday, May 23, 2014 10:50 PM
    Friday, May 23, 2014 10:50 PM
  • Now I'm worried too!  Aside from needing to add code to dispose the context, the code above works perfectly fine in a new 2013 vb project.  I will try it with a dummy array to see if the problem is the context, but I think something else is going on.  The error occurs right after I add the reference to Application.Server.  It never shows the class as an available RIA Service Class.  It errors after adding the reference and then closes the add datasource wizard.   Not good.
    Friday, May 23, 2014 11:37 PM
  • There is a small chance that this could also be an inherent LS bug. If you can create a small repro example project then I would think that the LS team would be interested in looking into it for you. But this will only be at the point where you've eliminated your own code as the possible cause.

    Regards, Xander. My Blog

    Friday, May 23, 2014 11:42 PM
  • Mind you, I just re-read your post and noticed that you say it does work perfectly in a new VB project too. So that would definitely point towards something weird with your current project.

    Perhaps you need to painstakingly compare all libraries in the existing project against that of a new project to see whether there are version differences somewhere.

    Regards, Xander. My Blog

    • Edited by novascape Friday, May 23, 2014 11:46 PM
    Friday, May 23, 2014 11:46 PM
  • The only way I could think to repro it would be to create a project in 2012.4, upgrade to latest tooling, and then try to create a RIA Service per above.  The project where this is not working has been upgraded several times through the life cycle of VS2012 and now in 2013, and it is a larger project with around 60 tables.  To get it to upgrade I had to change out a lot of references.  Once that was done, it compiles with no errors or warnings and runs fine.  I should also mention that I can add a traditional RIA Service (in a separate project, using its own connection string per Michael Washington's blog) with no problems.
    Friday, May 23, 2014 11:50 PM