locked
WebApi - 'It is not valid to access this object on the current thread' RRS feed

  • Question

  • Hi, 

    I am currently trying to call a WebApi I have created  from a LightSwitch htmlclient screen. Currently, I am still working on the WebApi part. 

    I am calling a position entity which has position number, title, reports to position number etc. This entity in LightSwitch is further connected/related to other entities including employee, fund allocation information entities etc. I am trying to return a specific record related to the currently signed in user.

    Here is code:

    mports System.Net
    Imports System.Web.Http
    Imports LightSwitchApplication
    Imports Microsoft.LightSwitch.Server
    
    Public Class TestController
        Inherits ApiController
    
        ' GET api/<controller>
        Public Function GetUserPosition()
            Using ctx As ServerApplicationContext = ServerApplicationContext.CreateContext()
                Dim CurrentUserID = ctx.Application.User.Name
                Dim reqinf As Position = ctx.DataWorkspace.ApplicationData.Positions.Where(Function(pos) pos.Employee.ActiveDirectoryInformation.USER_ID = CurrentUserID).FirstOrDefault
                Return reqinf
            End Using
        End Function
    
    
    End Class

    When I set a breakpoint for the 'Return reqinf' line and look at the 'autos' window when i get to 'End Function' line-all the 'direct' position entity fields show up in autos window along with the correct values. But I also get red 'x's for the values for Employee, allocated funds and dataworkspace with the error in the subject line above= 'It is not valid to access this object on the current thread'

    In my code above is there away of coding it so that it either doesnt go after the related entities, or goes after them in a way where the related entities are also navigable? Any ideas as to how to prevent error?

    Ideally, I would like to bring back the position title from positions, and name from employees entity etc. If I have the total record being brought back- then I would presumably  be able to pick the specific fields I need in javascript on the html client side of things.

    Thanks for any information provided.

    Thursday, June 12, 2014 4:27 PM

Answers

  • ok- I resolved this on my own by not using the context for all the associated information. Rather I used context only to get current user and then used stored procedure to get the rest of the information. Gere is code approach.

    First the controlleR:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports Microsoft.LightSwitch
    
    Public Class TestController
        Inherits ApiController
    
        ' GET api/<controller>
        Public Function GetUserPositionInfo()
            'Using ctx As ServerApplicationContext = ServerApplicationContext.CreateContext()
            '    Dim CurrentUserID = ctx.Application.User.Name
            '    Dim reqinf As Position = ctx.DataWorkspace.ApplicationData.Positions.Where(Function(pos) pos.Employee.ActiveDirectoryInformation.USER_ID = CurrentUserID).FirstOrDefault
            '    Return reqinf
            'End Using
            Dim PositionResult As Object = Nothing
            Using ctx As ServerApplicationContext = ServerApplicationContext.CreateContext()
                Dim CurrentUserID = ctx.Application.User.Name
                Using conn As New SqlConnection(
                          ConfigurationManager.ConnectionStrings("_IntrinsicData").ConnectionString)
    
                    Dim cmd As New SqlCommand()
                    cmd.Connection = conn
                    cmd.CommandText = "GetCurrentUserInformation"
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.Add("@CurrentUser", SqlDbType.Char)
                    cmd.Parameters("@CurrentUser").Value = CurrentUserID
                    cmd.Connection.Open()
                    Using reader As SqlDataReader =
                               cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    
                        PositionResult = (From dr In reader.Cast(Of IDataRecord)()
                                        Select
                                            pos = dr.GetString(0),
                                            postitle = dr.GetString(1),
                                            persName = dr.GetString(2),
                                            persid = dr.GetString(3)
                                       ).ToList()
    
                    End Using
                End Using
            End Using
    
            Return PositionResult
        End Function
    
    
    End Class
    


    Used reader- dont know if another approach could have been used

    here is the javascript

    myapp.BrowseYourReportsTos.created = function (screen) {
        // Write code here.
        //$.getJSON("../api/positions/", function (data) {
        //    screen.PositionpRTpos = data
        $.getJSON("../api/test/", function (data) {
            console.log("data returned", data);
            $.each(data,function (key,val){
             
                screen.PositionpRTpos = val.pos;
                screen.ManagerName = val.persName;
                screen.PositionTitle = val.postitle;
            });
        });
        };

    since reader was used in controller- even though only one record should ever be returned used $.each . Again dont know if better way when only one record will ever be returned.

    • Marked as answer by lvsund Tuesday, June 17, 2014 10:46 PM
    Tuesday, June 17, 2014 10:46 PM