0
votes

We have a WPF app that returns data from a WCF Service.

Intermittently the code below will throw a 'Cannot access as disposed object' error - usually after one update sequence has already been made successfully.

Even closing the service and starting a new one at each call doesn't help. Any ideas?

Thanks...

 Private Sub Reports_BalanceSheet_DoWork(sender As Object, e As DoWorkEventArgs)
    Try
        Dim vUpdate As New UpdateDelegate(AddressOf Reports_BalanceSheet_UpdateDB_Delegate)
        Dim vName As String = ReportName

        vService = New Service1Client

        Dim vID As Integer = 0
        If NewRecord = True Then
            Dim vRows As Integer = vService.ReturnScalarInteger("SELECT COUNT(Form_ID) From Balance_Sheet_Templates", Current_HOA_ID)
            If vRows > 0 Then
                vID = NullInteger(vService.ReturnScalarInteger("SELECT MAX(Form_ID) FROM Balance_Sheet_Templates", Current_HOA_ID))
            End If
            vID += 1
            'Save the form name
            strSQL = "INSERT INTO Balance_Sheet_Templates(Form_ID, Category_Type, Category_ID,  Form_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'1', "
            strSQL += "'1', "
            strSQL += "'" & vName & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2466 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If


        Else

            vID = FormID
            strSQL = "DELETE Balance_Sheet_Templates WHERE Form_ID = " & vID
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2455 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        End If




        Dim Update1() As Object = {"Saving the report defaults... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update1})
        For Each Row As DataRow In ReportDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Form_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'1', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & vName & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2417 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update2() As Object = {"Saving the revenue categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update2})
        For Each Row As DataRow In RevenueDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'3', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2434 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update3() As Object = {"Saving the revenue data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update3})
        For Each Row As DataRow In RevenueNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'4', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & SubmitText(Row("NomName")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2453 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update4() As Object = {"Saving the expenses categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update4})
        For Each Row As DataRow In ExpensesDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'5', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2472 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next



        Dim Update5() As Object = {"Saving the expenses data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update5})
        For Each Row As DataRow In ExpensesNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'6', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & SubmitText(Row("NomName")) & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2491 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update6() As Object = {"Saving the assets categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update6})
        For Each Row As DataRow In ResIncomeDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'7', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2509 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update7() As Object = {"Saving the assets data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update7})
        For Each Row As DataRow In ResIncNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'8', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & Row("NomName") & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2529 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update8() As Object = {"Saving the liability categories... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update8})
        For Each Row As DataRow In ResExpensesDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Key_String) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'9', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & SubmitText(Row("Name")) & "')"
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2548 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




        Dim Update9() As Object = {"Saving the liability data... Please wait..."}
        Dispatcher.BeginInvoke(vUpdate, New Object() {Update9})
        For Each Row As DataRow In ResExpNomDT.Rows
            strSQL = "INSERT INTO Balance_Sheet_Templates (Form_ID, Category_Type, Category_ID, Position, Cat_Name_ID, Nom_Code, Nom_Name) VALUES ("
            strSQL += "'" & vID & "', "
            strSQL += "'10', "
            strSQL += "'" & Row("ID") & "', "
            strSQL += "'" & Row("Position") & "', "
            strSQL += "'" & Row("CatID") & "', "
            strSQL += "'" & Row("NomCode") & "', "
            strSQL += "'" & Row("NomName") & "') "
            If vService.InsertDataHOA(strSQL, "Reports_BalanceSheet_Page 2566 " & Current_HOA_Name, Current_HOA_ID) = False Then
                IsError = True
                Dim Error1() As Object = {"There was an error updating the record"}
                Dispatcher.BeginInvoke(vUpdate, New Object() {Error1})
                Exit Sub
            End If
        Next




    Catch ex As Exception
        EmailError(ex, False)
        IsError = True
    Finally
        If Not vService Is Nothing Then
            vService.Close()
            vService = Nothing
        End If
    End Try
End Sub



Private Sub Reports_BalanceSheet_WorkCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
    Try
        If IsError = True Then
            AppBoxError("There was an error updating the record!")
            Exit Sub
        End If
        PageStatusBarLoaded(Reports_BalanceSheet_Grid, "Template was successfully saved...")
        PageStatusBarRightChangeText(Reports_BalanceSheet_Grid, "Template updated...")
        Dim SaveUpdateButton As Button = Reports_BalanceSheet_Grid.FindName("BalanceSheet_SaveUpdateButton")
        If NewRecord = True Then
            SaveUpdateButton.IsEnabled = False
            SaveUpdateButton.Content = ReturnToolBarImageGrey("Record_Insert.png")
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub



Private Sub Reports_BalanceSheet_UpdateDB_Delegate(ByVal x() As Object)
    Try
        PageStatusBarRightChangeText(Reports_BalanceSheet_Grid, x(0).ToString)
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

The error returned

Cannot access a disposed object.

Object name: 'System.ServiceModel.Channels.HttpsChannelFactory`1+HttpsRequestChannel[System.ServiceModel.Channels.IRequestChannel]'.

Server stack trace: at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrNotOpen() at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at HOA_Manager_Client_04.ServiceReference1.IService1.InsertDataHOA(String strSQL, String LineNo, String HOAID) at HOA_Manager_Client_04.ServiceReference1.Service1Client.InsertDataHOA(String strSQL, String LineNo, String HOAID) at HOA_Manager_Client_04.Reports_BalanceSheet_Page.Reports_BalanceSheet_DoWork(Object sender, DoWorkEventArgs e)

1

1 Answers

0
votes

Throughout the app vService is defined in each page

Private strSQL As String
Private vService As Service1Client

and is created as New when required. Something was clearly also using and closing this at the same time so I just created a brand new service

 Private Sub Reports_BalanceSheet_DoWork(sender As Object, e As DoWorkEventArgs)
    Dim vService1 As New Service1Client
    Try
        Dim vUpdate As New UpdateDelegate(AddressOf Reports_BalanceSheet_UpdateDB_Delegate)
        Dim vName As String = ReportName

        ' vService = New Service1Client

and changed every update instance to that - and so far it seems to be working. Odd that this is the only place in the entire app it was throwing this error